6 releases

0.1.5 Sep 22, 2022
0.1.4 Sep 21, 2022

#1909 in Encoding

Download history 200/week @ 2024-06-17 31/week @ 2024-06-24 183/week @ 2024-07-01 158/week @ 2024-07-08 86/week @ 2024-07-15 316/week @ 2024-07-22 422/week @ 2024-07-29 332/week @ 2024-08-05 166/week @ 2024-08-12 95/week @ 2024-08-19 113/week @ 2024-08-26 133/week @ 2024-09-02 71/week @ 2024-09-09 14/week @ 2024-09-16 137/week @ 2024-09-23 122/week @ 2024-09-30

346 downloads per month
Used in 2 crates

MIT license

28KB
704 lines

rust_json

Latest Version

JSON serializer and deserializer written for learning rust.

Feature

Parse json from string

use rust_json::json_parse;

fn example() {
    let j = json_parse(r#"
    {
        "S": [
            1, 
            2.3, 
            {
                "4": {
                    "5": {
                        "6": [
                            null,
                            true, 
                            false
                            ]
                        }
                    }
            }
        ]
    }"#);

    println!("{}", j["S"]);
}

Construct JsonElem with json literal

use rust_json::json;

fn example() {
    let j = json!(
        {
            "S": [
                1.2, 
                "2.3", 
                {
                    "4": {
                        "5": {
                            "6": [
                                null,
                                true, 
                                false
                                ]
                            }
                        }
                }
            ]
        }
    );

    println!("{}", j["S"]);
}

Construct JsonElem with js object literal style

use rust_json::js_object;

fn proc(n: i32) -> i32 {
    n * n + n / 2
}

fn main() {
    let a = true;
    let n = 32;
    let j = js_object!([
        {
            a // 属性的简洁表示 Property Shorthand
        },
        {
            // 使用表达式作为值 Using expression
            proc_n: if n % 2 == 0 { proc(n) + 1 } else { 0 }, 
            [n * 12]: n * 12 // 属性名表达式 Computed Property Names
        }
    ]);
    println!("{}", j);
}

ToJson and FromJson traits

Impl the ToJson and FromJson to serialize and deserialize custom struct. Or you can use rust_json_derive to derive the traits.

Stringify with space

use rust_json::json;

fn example() {
    let j = json!({"a": 12});
    println!("{:#}", j);
    // { 
    //     "a": 12
    // }
    println!("{:3}", j);
    // { 
    //    "a": 12
    // }
}

No runtime deps