#json #json-parser #parse-json

jzon

Continuation of json-rust, a JSON implementation in Rust

1 unstable release

0.12.5 Aug 25, 2023

#806 in Encoding

Download history 39932/week @ 2024-07-29 22672/week @ 2024-08-05 47592/week @ 2024-08-12 46093/week @ 2024-08-19 18228/week @ 2024-08-26 24769/week @ 2024-09-02 25083/week @ 2024-09-09 30214/week @ 2024-09-16 29392/week @ 2024-09-23 30360/week @ 2024-09-30 32314/week @ 2024-10-07 24108/week @ 2024-10-14 30301/week @ 2024-10-21 37334/week @ 2024-10-28 39283/week @ 2024-11-04 29141/week @ 2024-11-11

137,264 downloads per month
Used in 17 crates (4 directly)

MIT/Apache

150KB
3.5K SLoC

jzon-rs

Adoption and continuation of maciejhirsz/json-rust. Big shout-out to its creator Maciej Hirsz.

Rust library to parse and serialize JSON with ease.

Changelog - Complete Documentation - Cargo - Repository

Why?

JSON is a very loose format where anything goes - arrays can hold mixed types, object keys can change types between API calls or not include some keys under some conditions. Mapping that to idiomatic Rust structs introduces friction.

This crate intends to avoid that friction.

let parsed = jzon::parse(r#"

{
    "code": 200,
    "success": true,
    "payload": {
        "features": [
            "awesome",
            "easyAPI",
            "lowLearningCurve"
        ]
    }
}

"#).unwrap();

let instantiated = object!{
    // quotes on keys are optional
    "code": 200,
    success: true,
    payload: {
        features: [
            "awesome",
            "easyAPI",
            "lowLearningCurve"
        ]
    }
};

assert_eq!(parsed, instantiated);

First class citizen

Using macros and indexing, it's easy to work with the data.

let mut data = object!{
    foo: false,
    bar: null,
    answer: 42,
    list: [null, "world", true]
};

// Partial equality is implemented for most raw types:
assert!(data["foo"] == false);

// And it's type aware, `null` and `false` are different values:
assert!(data["bar"] != false);

// But you can use any Rust number types:
assert!(data["answer"] == 42);
assert!(data["answer"] == 42.0);
assert!(data["answer"] == 42isize);

// Access nested structures, arrays and objects:
assert!(data["list"][0].is_null());
assert!(data["list"][1] == "world");
assert!(data["list"][2] == true);

// Error resilient - accessing properties that don't exist yield null:
assert!(data["this"]["does"]["not"]["exist"].is_null());

// Mutate by assigning:
data["list"][0] = "Hello".into();

// Use the `dump` method to serialize the data:
assert_eq!(data.dump(), r#"{"foo":false,"bar":null,"answer":42,"list":["Hello","world",true]}"#);

// Or pretty print it out:
println!("{:#}", data);

Installation

Just add it to your Cargo.toml file:

[dependencies]
jzon = "*"

Then import it in your main.rs / lib.rs file:

#[macro_use]
extern crate jzon;

Performance and Conformance

There used to be a statement here saying that performance is not the main goal of this crate. It is definitely one of them now.

While this crate doesn't provide a way to parse JSON to native Rust structs, it does a lot to optimize its performance for DOM parsing, stringifying and manipulation. It does very well in benchmarks, in some cases it can even outperform parsing to structs.

This crate implements the standard according to the RFC 7159 and ECMA-404 documents. For the best interoperability numbers are treated stored as 64bit precision mantissa with 16 bit decimal exponent for floating point representation.

License

This crate is distributed under the terms of both the MIT license and the Apache License (Version 2.0). Choose whichever one works best for you.

See LICENSE-APACHE and LICENSE-MIT for details.

No runtime deps