4444//! assert!(data["this"]["does"]["not"]["exist"].is_null());
4545//! ```
4646//!
47- //! ## Easily create JSON data without defining structs
47+ //! ## Create JSON data without defining structs
4848//!
4949//! ```
5050//! #[macro_use]
6060//! }
6161//! ```
6262//!
63+ //! ## Mutate simply by assigning new values
64+ //!
65+ //! ```
66+ //! let mut data = json::parse(r#"
67+ //!
68+ //! {
69+ //! "name": "Bob",
70+ //! "isAwesome": false
71+ //! }
72+ //!
73+ //! "#).unwrap();
74+ //!
75+ //! data["isAwesome"] = true.into();
76+ //! data["likes"] = "Rust".into();
77+ //!
78+ //! assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);
79+ //!
80+ //! // Pretty print the output
81+ //! println!("{:#}", data);
82+ //! ```
83+ //!
6384//! ## Serialize with `json::stringify(value)`
6485//!
6586//! Primitives:
126147//! ```
127148//! let mut data = json::JsonValue::new_object();
128149//!
129- //! data.put( "answer", 42 );
130- //! data.put( "foo", "bar");
150+ //! data[ "answer"] = 42.into( );
151+ //! data[ "foo"] = "bar".into( );
131152//!
132153//! assert_eq!(json::stringify(data), "{\"answer\":42,\"foo\":\"bar\"}");
133154//! ```
@@ -177,6 +198,7 @@ use codegen::Generator;
177198
178199use std:: collections:: HashMap ;
179200use std:: collections:: BTreeMap ;
201+ use std:: fmt;
180202
181203pub type Array = Vec < JsonValue > ;
182204pub type Object = BTreeMap < String , JsonValue > ;
@@ -198,6 +220,30 @@ impl JsonValue {
198220 }
199221}
200222
223+ /// Implements formatting
224+ ///
225+ /// ```
226+ /// # use json;
227+ /// let data = json::parse(r#"{"url":"https://github.com/"}"#).unwrap();
228+ /// println!("{}", data);
229+ /// println!("{:#}", data);
230+ /// ```
231+ impl fmt:: Display for JsonValue {
232+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
233+ if f. alternate ( ) {
234+ f. write_str ( & self . pretty ( 4 ) )
235+ } else {
236+ match * self {
237+ JsonValue :: String ( ref value) => value. fmt ( f) ,
238+ JsonValue :: Number ( ref value) => value. fmt ( f) ,
239+ JsonValue :: Boolean ( ref value) => value. fmt ( f) ,
240+ JsonValue :: Null => f. write_str ( "null" ) ,
241+ _ => f. write_str ( & self . dump ( ) )
242+ }
243+ }
244+ }
245+ }
246+
201247#[ deprecated( since="0.5.0" , note="Use `value.dump(0)` instead" ) ]
202248pub fn stringify_ref ( root : & JsonValue ) -> String {
203249 root. dump ( )
0 commit comments