#json #serialization #macro #automated #procedural #proc-macro #update

rusty_json_serialization

为Rust提供自动化JSON序列化的进程宏

3个稳定版本

1.2.2 2024年7月29日
1.2.1 2024年7月17日
1.0.0 2024年6月25日

#2010进程宏

Download history 122/week @ 2024-06-23 6/week @ 2024-06-30 3/week @ 2024-07-07 218/week @ 2024-07-14 5/week @ 2024-07-21 170/week @ 2024-07-28

每月396次下载
用于 rusty_json

MIT 许可证

11KB
167

Rusty Json

一个轻量级且高效的Rust库,用于创建、读取和写入JSON

"代码文档将在后续更新中提供。"

用法

基本

  • JSON对象
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数组
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]]

或者

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值
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(())
}

额外

  • 解析器
let raw_json = r#"
    {
        "name": "Ammar Dev",
        "age": 99,
        "email": "[email protected]",
        "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": "[email protected]", "isEmployed": false}
  • 格式化器
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"
  ]
}
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

实现

  • JSON实体(手动)
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 main() -> Result<(), Box<dyn Error>> {

    let u = User {
        name: "Ammar Dev".to_string(),
        age: 22,
    };


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

    Ok(())
}
{"name": "Ammar Dev", "age": 22}
  • JSON实体(自动) [需要: serializing功能]
use std::error::Error;
use rusty_json::extra::JsonEntity;

#[derive(JsonEntity, Clone)]
struct User {
    name: String,
    age: i32,
}

fn main() -> Result<(), Box<dyn Error>> {

    let u = User {
        name: "Ammar Dev".to_string(),
        age: 22,
    };

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

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

版权

Ammar Dev版权所有
本项目采用 MIT许可证

依赖

~260–700KB
~17K SLoC