1
1
//! 
2
2
//!
3
- //! # JSON in Rust
3
+ //! # json-rust
4
4
//!
5
- //! Parse and serialize JSON with ease.
5
+ //! Parse and serialize [ JSON](http://json.org/) with ease.
6
6
//!
7
7
//! **[Complete Documentation](http://terhix.com/doc/json/) - [Cargo](https://crates.io/crates/json) - [Repository](https://github.com/maciejhirsz/json-rust)**
8
8
//!
9
- //! # Why?
9
+ //! ## Why?
10
10
//!
11
11
//! JSON is a very loose format where anything goes - arrays can hold mixed
12
12
//! types, object keys can change types between API calls or not include
139
139
//! data.push("foo");
140
140
//! data.push(false);
141
141
//!
142
- //! assert_eq!(json::stringify( data), "[10,\ "foo\ ",false]");
142
+ //! assert_eq!(data.dump( ), r# "[10,"foo",false]"# );
143
143
//! ```
144
144
//!
145
145
//! Putting fields on objects:
150
150
//! data["answer"] = 42.into();
151
151
//! data["foo"] = "bar".into();
152
152
//!
153
- //! assert_eq!(json::stringify( data), "{\ "answer\ ":42,\ "foo\":\ "bar\ "}");
153
+ //! assert_eq!(data.dump( ), r#"{ "answer":42,"foo": "bar"}"# );
154
154
//! ```
155
155
//!
156
156
//! `array!` macro:
159
159
//! # #[macro_use] extern crate json;
160
160
//! # fn main() {
161
161
//! let data = array!["foo", "bar", 100, true, json::Null];
162
- //! assert_eq!(json::stringify( data), "[\ "foo\",\ "bar\ ",100,true,null]");
162
+ //! assert_eq!(data.dump( ), r#"[ "foo", "bar",100,true,null]"# );
163
163
//! # }
164
164
//! ```
165
165
//!
174
174
//! "canJSON" => true
175
175
//! };
176
176
//! assert_eq!(
177
- //! json::stringify( data),
177
+ //! data.dump( ),
178
178
//! // Because object is internally using a BTreeMap,
179
179
//! // the key order is alphabetical
180
- //! "{\ "age\ ":30,\ "canJSON\ ":true,\ "name\":\ "John Doe\ "}"
180
+ //! r#"{ "age":30,"canJSON":true,"name": "John Doe"}"#
181
181
//! );
182
182
//! # }
183
183
//! ```
@@ -296,27 +296,27 @@ macro_rules! object {
296
296
297
297
macro_rules! implement_extras {
298
298
( $from: ty) => {
299
- impl Into < JsonValue > for Option <$from> {
300
- fn into ( self ) -> JsonValue {
301
- match self {
299
+ impl From < Option <$from>> for JsonValue {
300
+ fn from ( val : Option <$from> ) -> JsonValue {
301
+ match val {
302
302
Some ( value) => value. into( ) ,
303
303
None => Null ,
304
304
}
305
305
}
306
306
}
307
307
308
- impl Into < JsonValue > for Vec <$from> {
309
- fn into ( mut self ) -> JsonValue {
310
- JsonValue :: Array ( self . drain( ..)
308
+ impl From < Vec <$from>> for JsonValue {
309
+ fn from ( mut val : Vec <$from> ) -> JsonValue {
310
+ JsonValue :: Array ( val . drain( ..)
311
311
. map( |value| value. into( ) )
312
312
. collect:: <Vec <JsonValue >>( )
313
313
)
314
314
}
315
315
}
316
316
317
- impl Into < JsonValue > for Vec <Option <$from>> {
318
- fn into ( mut self ) -> JsonValue {
319
- JsonValue :: Array ( self . drain( ..)
317
+ impl From < Vec <Option <$from>>> for JsonValue {
318
+ fn from ( mut val : Vec < Option <$from>> ) -> JsonValue {
319
+ JsonValue :: Array ( val . drain( ..)
320
320
. map( |item| item. into( ) )
321
321
. collect:: <Vec <JsonValue >>( )
322
322
)
@@ -327,64 +327,64 @@ macro_rules! implement_extras {
327
327
328
328
macro_rules! implement {
329
329
( $to: ident, $from: ty as $wanted: ty) => {
330
- impl Into < JsonValue > for $from {
331
- fn into ( self ) -> JsonValue {
332
- JsonValue :: $to( self as $wanted)
330
+ impl From <$from > for JsonValue {
331
+ fn from ( val : $from ) -> JsonValue {
332
+ JsonValue :: $to( val as $wanted)
333
333
}
334
334
}
335
335
336
336
implement_extras!( $from) ;
337
337
} ;
338
338
( $to: ident, $from: ty) => {
339
- impl Into < JsonValue > for $from {
340
- fn into ( self ) -> JsonValue {
341
- JsonValue :: $to( self )
339
+ impl From <$from > for JsonValue {
340
+ fn from ( val : $from ) -> JsonValue {
341
+ JsonValue :: $to( val )
342
342
}
343
343
}
344
344
345
345
implement_extras!( $from) ;
346
346
}
347
347
}
348
348
349
- impl < ' a > Into < JsonValue > for & ' a str {
350
- fn into ( self ) -> JsonValue {
351
- JsonValue :: String ( self . to_string ( ) )
349
+ impl < ' a > From < & ' a str > for JsonValue {
350
+ fn from ( val : & ' a str ) -> JsonValue {
351
+ JsonValue :: String ( val . to_string ( ) )
352
352
}
353
353
}
354
354
355
- impl < ' a > Into < JsonValue > for Option < & ' a str > {
356
- fn into ( self ) -> JsonValue {
357
- match self {
355
+ impl < ' a > From < Option < & ' a str > > for JsonValue {
356
+ fn from ( val : Option < & ' a str > ) -> JsonValue {
357
+ match val {
358
358
Some ( value) => value. into ( ) ,
359
359
None => Null ,
360
360
}
361
361
}
362
362
}
363
363
364
- impl Into < JsonValue > for HashMap < String , JsonValue > {
365
- fn into ( mut self ) -> JsonValue {
364
+ impl From < HashMap < String , JsonValue > > for JsonValue {
365
+ fn from ( mut val : HashMap < String , JsonValue > ) -> JsonValue {
366
366
let mut object = BTreeMap :: new ( ) ;
367
367
368
- for ( key, value) in self . drain ( ) {
368
+ for ( key, value) in val . drain ( ) {
369
369
object. insert ( key, value) ;
370
370
}
371
371
372
372
JsonValue :: Object ( object)
373
373
}
374
374
}
375
375
376
- impl Into < JsonValue > for Option < HashMap < String , JsonValue > > {
377
- fn into ( self ) -> JsonValue {
378
- match self {
376
+ impl From < Option < HashMap < String , JsonValue > > > for JsonValue {
377
+ fn from ( val : Option < HashMap < String , JsonValue > > ) -> JsonValue {
378
+ match val {
379
379
Some ( value) => value. into ( ) ,
380
380
None => Null ,
381
381
}
382
382
}
383
383
}
384
384
385
- impl Into < JsonValue > for Option < JsonValue > {
386
- fn into ( self ) -> JsonValue {
387
- match self {
385
+ impl From < Option < JsonValue > > for JsonValue {
386
+ fn from ( val : Option < JsonValue > ) -> JsonValue {
387
+ match val {
388
388
Some ( value) => value,
389
389
None => Null ,
390
390
}
0 commit comments