|
15 | 15 | //! some keys under some conditions. Mapping that to idiomatic Rust structs |
16 | 16 | //! introduces friction. |
17 | 17 | //! |
18 | | -//! This crate intends to avoid that friction by extensively using static dispatch |
19 | | -//! and hiding type information behind enums, while still giving you all the |
20 | | -//! guarantees of safe Rust code. |
| 18 | +//! This crate intends to avoid that friction. |
21 | 19 | //! |
22 | | -//! ``` |
23 | | -//! let data = json::parse(r#" |
| 20 | +//! ```rust |
| 21 | +//! # #[macro_use] extern crate json; |
| 22 | +//! # fn main() { |
| 23 | +//! let parsed = json::parse(r#" |
24 | 24 | //! |
25 | 25 | //! { |
26 | 26 | //! "code": 200, |
|
36 | 36 | //! |
37 | 37 | //! "#).unwrap(); |
38 | 38 | //! |
39 | | -//! assert!(data["code"] == 200); |
40 | | -//! assert!(data["success"] == true); |
41 | | -//! assert!(data["payload"]["features"].is_array()); |
42 | | -//! assert!(data["payload"]["features"][0] == "awesome"); |
43 | | -//! assert!(data["payload"]["features"].contains("easyAPI")); |
| 39 | +//! let instantiated = object!{ |
| 40 | +//! "code" => 200, |
| 41 | +//! "success" => true, |
| 42 | +//! "payload" => object!{ |
| 43 | +//! "features" => array![ |
| 44 | +//! "awesome", |
| 45 | +//! "easyAPI", |
| 46 | +//! "lowLearningCurve" |
| 47 | +//! ] |
| 48 | +//! } |
| 49 | +//! }; |
44 | 50 | //! |
45 | | -//! // Error resilient: non-existent values default to null |
46 | | -//! assert!(data["this"]["does"]["not"]["exist"].is_null()); |
| 51 | +//! assert_eq!(parsed, instantiated); |
| 52 | +//! # } |
47 | 53 | //! ``` |
48 | 54 | //! |
49 | | -//! ## Create JSON data without defining structs |
| 55 | +//! ## First class citizen |
50 | 56 | //! |
51 | | -//! ``` |
52 | | -//! #[macro_use] |
53 | | -//! extern crate json; |
| 57 | +//! Using macros and easy indexing, it's easy to work with the data. |
54 | 58 | //! |
55 | | -//! fn main() { |
56 | | -//! let data = object!{ |
57 | | -//! "a" => "bar", |
58 | | -//! "b" => array![1, false, "foo"] |
59 | | -//! }; |
| 59 | +//! ```rust |
| 60 | +//! # #[macro_use] extern crate json; |
| 61 | +//! # fn main() { |
| 62 | +//! let mut data = object!{ |
| 63 | +//! "foo" => false, |
| 64 | +//! "bar" => json::Null, |
| 65 | +//! "answer" => 42, |
| 66 | +//! "list" => array![json::Null, "world", true] |
| 67 | +//! }; |
60 | 68 | //! |
61 | | -//! assert_eq!(data.dump(), r#"{"a":"bar","b":[1,false,"foo"]}"#); |
62 | | -//! } |
63 | | -//! ``` |
| 69 | +//! // Partial equality is implemented for most raw types: |
| 70 | +//! assert!(data["foo"] == false); |
64 | 71 | //! |
65 | | -//! ## Mutate simply by assigning new values |
| 72 | +//! // And it's type aware, `null` and `false` are different values: |
| 73 | +//! assert!(data["bar"] != false); |
66 | 74 | //! |
67 | | -//! ``` |
68 | | -//! let mut data = json::parse(r#" |
| 75 | +//! // But you can use any Rust number types: |
| 76 | +//! assert!(data["answer"] == 42); |
| 77 | +//! assert!(data["answer"] == 42.0); |
| 78 | +//! assert!(data["answer"] == 42isize); |
69 | 79 | //! |
70 | | -//! { |
71 | | -//! "name": "Bob", |
72 | | -//! "isAwesome": false |
73 | | -//! } |
| 80 | +//! // Access nested structures, arrays and objects: |
| 81 | +//! assert!(data["list"][0].is_null()); |
| 82 | +//! assert!(data["list"][1] == "world"); |
| 83 | +//! assert!(data["list"][2] == true); |
74 | 84 | //! |
75 | | -//! "#).unwrap(); |
| 85 | +//! // Error resilient - accessing properties that don't exist yield null: |
| 86 | +//! assert!(data["this"]["does"]["not"]["exist"].is_null()); |
76 | 87 | //! |
77 | | -//! data["isAwesome"] = true.into(); |
78 | | -//! data["likes"] = "Rust".into(); |
| 88 | +//! // Mutate by assigning: |
| 89 | +//! data["list"][0] = "Hello".into(); |
79 | 90 | //! |
80 | | -//! assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#); |
| 91 | +//! // Use the `dump` method to serialize the data: |
| 92 | +//! assert_eq!(data.dump(), r#"{"answer":42,"bar":null,"foo":false,"list":["Hello","world",true]}"#); |
81 | 93 | //! |
82 | | -//! // Pretty print the output |
| 94 | +//! // Or pretty print it out: |
83 | 95 | //! println!("{:#}", data); |
| 96 | +//! # } |
84 | 97 | //! ``` |
85 | 98 | //! |
86 | 99 | //! ## Serialize with `json::stringify(value)` |
|
0 commit comments