Parse and serialize JSON with ease.
Complete Documentation - Cargo - Repository
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 by extensively using static dispatch and hiding type information behind enums, while still giving you all the guarantees of safe Rust code.
```rust let data = json::parse(r#"
{ "code": 200, "success": true, "payload": { "features": [ "awesome", "easyAPI", "lowLearningCurve" ] } }
"#).unwrap();
assert!(data["code"] == 200); assert!(data["success"] == true)); assert!(data["payload"]["features"].is_array()); assert!(data["payload"]["features"][0] == "awesome"); assert!(data["payload"]["features"].contains("easyAPI"));
// Error resilient: non-existent values default to null assert!(data["this"]["does"]["not"]["exist"].is_null()); ```
```rust
extern crate json;
fn main() { let data = object!{ "a" => "bar", "b" => array![1, false, "foo"] };
assert_eq!(data.dump(), r#"{"a":"bar","b":[1,false,"foo"]}"#);
} ```
```rust let mut data = json::parse(r#"
{ "name": "Bob", "isAwesome": false }
"#).unwrap();
data["isAwesome"] = true.into(); data["likes"] = "Rust".into();
assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);
// Pretty print the output println!("{:#}", data); ```
Just add it to your Cargo.toml
file:
toml
[dependencies]
json = "*"
Then import it in your main.rs
/ lib.rs
file:
```rust
extern crate json; ```