- Notifications
You must be signed in to change notification settings - Fork2
JSON implementation in Rust
License
Apache-2.0, MIT licenses found
Licenses found
rustadopt/jzon-rs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Adoption and continuation ofmaciejhirsz/json-rust.Big shout-out to its creatorMaciej Hirsz.
Rust library to parse and serializeJSON with ease.
JSON is a very loose format where anything goes - arrays can hold mixedtypes, object keys can change types between API calls or not includesome keys under some conditions. Mapping that to idiomatic Rust structsintroduces 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);
Using macros and indexing, it's easy to work with the data.
letmut 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);
Just add it to yourCargo.toml
file:
[dependencies]jzon ="*"
Then import it in yourmain.rs
/lib.rs
file:
#[macro_use]externcrate jzon;
There used to be a statement here saying that performance is not the main goal of thiscrate. It is definitely one of them now.
While this crate doesn't provide a way to parse JSON to native Rust structs, it does alot to optimize its performance for DOM parsing, stringifying and manipulation. It doesvery well in benchmarks, in some cases itcan even outperform parsing to structs.
This crate implements the standard according to theRFC 7159 andECMA-404documents. For the best interoperability numbers are treated stored as 64bit precisionmantissa with 16 bit decimal exponent for floating point representation.
This crate is distributed under the terms of both the MIT licenseand the Apache License (Version 2.0). Choose whichever one works best for you.
SeeLICENSE-APACHE andLICENSE-MIT for details.
About
JSON implementation in Rust
Resources
License
Apache-2.0, MIT licenses found
Licenses found
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.