rusty_json

Crates.iorusty_json
lib.rsrusty_json
version2.0.5
sourcesrc
created_at2024-05-29 21:12:38.564279
updated_at2024-07-29 09:41:54.568779
descriptionA lightweight and straightforward JSON library for Rust, providing basic JSON functionality, serialization, and manipulation.
homepage
repositoryhttps://github.com/ammardevz/rusty_json
max_upload_size
id1256187
size92,319
AMMAR DEV (ammardevz)

documentation

README

Rusty Json

A lightweight and straightforward JSON library for Rust, providing basic JSON functionality, serialization, and manipulation.

Note: If you need a highly optimized and widely used JSON crate, consider using serde_json. While Rusty Json is designed to be simple and lightweight, it is still in its early stages and may not be as performant or feature-rich as serde_json.

Usage

Base

  • Json Object
let mut json_obj = JsonObject::new();
json_obj.set("Name", "Ammar Dev");
json_obj.set("msg", "Github");
json_obj.set("number", 234);

println!("{}", json_obj);
{"msg": "Github", "Name": "Ammar Dev", "number": 234}
  • Json Array
let mut json_arr = JsonArray::new();
json_arr.push("Value 1");
json_arr.push(324);
json_arr.push(vec![1, 2]);

println!("{}", json_arr);
["Value 1", 324, [1, 2]]

OR

let mut json_arr: JsonArray = vec![1, 2].into();
json_arr.push("Value 1");
json_arr.push("Value 2");

println!("{}", json_arr);
[1, 2, "Value 1", "Value 2"]
  • Json Value
fn main() -> Result<(), Box<dyn Error>> {
    // Creating JSON values of different types
    let string_value = JsonValue::String("Hello".to_string());
    let number_value_1 = JsonValue::Number(3453f64);
    let number_value_2 = JsonValue::Number(4234001f64);
    let bool_value = JsonValue::Boolean(false);
    let null_value = JsonValue::Null;
    let object_value = JsonValue::Object(JsonObject::new());
    let array_value = JsonValue::Array(JsonArray::new());

    // Parsing the JSON values into Rust types
    let parsed_string: String = string_value.parse()?;
    let parsed_number_1: f64 = number_value_1.parse()?;
    let parsed_number_2: i32 = number_value_2.parse()?;
    let parsed_bool: bool = bool_value.parse()?;

    // Printing the parsed values
    println!("Parsed String: {}", parsed_string);
    println!("Parsed Number 1: {}", parsed_number_1);
    println!("Parsed Number 2: {}", parsed_number_2);
    println!("Parsed Bool: {}", parsed_bool);

    // Indicating that the main function completed successfully
    Ok(())
}

Extra

  • Parser
let raw_json = r#"
    {
        "name": "Ammar Dev",
        "age": 99,
        "email": "AmmarDev@example.com",
        "isEmployed": true
    }
"#;

let json_val = JsonParser::parse(raw_json).unwrap(); // Parsed into JsonValue
let mut json_obj = JsonObject::from(json_val); // Cast into Json Object
json_obj.set("isEmployed", false);
println!("{}", json_obj);
{"name": "Ammar Dev", "age": 99, "email": "AmmarDev@example.com", "isEmployed": false}
  • Formatter
let mut json_obj = JsonObject::new();
json_obj.set("name", "Ammar Dev");
json_obj.set("age", 99);

let mut json_arr = JsonArray::from(vec![1, 2]);
json_arr.push("Hi");
json_arr.push("Hello");

json_obj.set("list_example", json_arr);

let formatter = JsonFormatter::default();
println!("{}", formatter.format(json_obj));
{
  "name": "Ammar Dev",
  "age": 99,
  "list_example": [
    1,
    2,
    "Hi",
    "Hello"
  ]
}
  • Macros
let json_obj = json!({
    name: "Ammar Dev",
    Age: 99
}); // will produce JsonObject

let json_arr = json!([
    1,
    2,
    "String 1",
    "String 2"
]); // will produce JsonArray

let json_val = json!("Hello"); // will Produce JsonValue

Implementing

  • Json Entity (manually)
#[derive(Clone)]
struct User {
    name: String,
    age: i32,
}

impl JsonEntity for User {
    fn to_json(&self) -> JsonValue {
        let mut obj = JsonObject::new();
        obj.set("name", &self.name);
        obj.set("age", &self.age);
        JsonValue::Object(obj)
    }

    fn from_json(raw: &str) -> Result<Self, ConversationError> {
        let parsed = JsonParser::parse(raw)?;
        if let JsonValue::Object(obj) = parsed {
            let name = obj.get("name").ok_or_else(|| ConversationError::GenericError("Missing 'name'".to_string()))?
                .parse::<String>()?;
            let age = obj.get("age").ok_or_else(|| ConversationError::GenericError("Missing 'age'".to_string()))?
                .parse::<i32>()?;
            Ok(User { name, age })
        } else {
            Err(ConversationError::GenericError("Expected JSON object".to_string()))
        }
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    let user = User {
        name: "Ammar Dev".to_string(),
        age: 22,
    };

    let json = user.to_json();
    println!("{}", json);

    let json_str = json.to_string(); // Assuming to_string() is implemented
    let parsed_user = User::from_json(&json_str)?;

    println!("{}", parsed_user.to_json());

    Ok(())
}
{"name": "Ammar Dev", "age": 22}
  • Json Entity (Auto) [Requires: serializing feature]
#[derive(JsonEntity, Clone, Debug)] // Derive JsonEntity for automatic serialization and deserialization
struct User {
    name: String,
    age: i32,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create an instance of User
    let user = User {
        name: "Ammar Dev".to_string(),
        age: 22,
    };

    // Serialize User to JSON and print it
    println!("Serialized JSON:\n{}", user.to_json());

    // Example JSON string for deserialization
    let json_str = r#"{"name": "Ammar Dev", "age": 22}"#;

    // Deserialize JSON back to User
    let deserialized_user: User = User::from_json(json_str)?;

    // Print deserialized User
    println!("Deserialized User: {:?}", deserialized_user);

    Ok(())
}
{"name": "Ammar Dev", "age": 22}

Copyrights

All rights reserved for Ammar Dev
This project is licensed with MIT LICENSE

Commit count: 24

cargo fmt