196
196
//! # }
197
197
//! ```
198
198
199
- use std:: io:: Write ;
200
- use std:: collections:: { BTreeMap , HashMap } ;
201
- use std:: { fmt, result, mem } ;
199
+ use std:: result;
202
200
203
201
mod codegen;
204
202
mod parser;
@@ -210,10 +208,6 @@ pub mod short;
210
208
pub mod object;
211
209
pub mod number;
212
210
213
- use short:: Short ;
214
- use number:: Number ;
215
- use object:: Object ;
216
-
217
211
pub use error:: Error ;
218
212
pub use value:: JsonValue ;
219
213
pub use value:: JsonValue :: Null ;
@@ -246,58 +240,9 @@ pub use Error as JsonError;
246
240
pub use Result as JsonResult ;
247
241
248
242
pub use parser:: parse;
249
- use codegen:: { Generator , PrettyGenerator , DumpGenerator , WriterGenerator } ;
250
243
251
244
pub type Array = Vec < JsonValue > ;
252
245
253
- impl JsonValue {
254
- /// Prints out the value as JSON string.
255
- pub fn dump ( & self ) -> String {
256
- let mut gen = DumpGenerator :: new ( ) ;
257
- gen. write_json ( self ) ;
258
- gen. consume ( )
259
- }
260
-
261
- /// Pretty prints out the value as JSON string. Takes an argument that's
262
- /// number of spaces to indent new blocks with.
263
- pub fn pretty ( & self , spaces : u16 ) -> String {
264
- let mut gen = PrettyGenerator :: new ( spaces) ;
265
- gen. write_json ( self ) ;
266
- gen. consume ( )
267
- }
268
-
269
- /// Dumps the JSON as byte stream into an instance of `std::io::Write`.
270
- pub fn to_writer < W : Write > ( & self , writer : & mut W ) {
271
- let mut gen = WriterGenerator :: new ( writer) ;
272
- gen. write_json ( self ) ;
273
- }
274
- }
275
-
276
- /// Implements formatting
277
- ///
278
- /// ```
279
- /// # use json;
280
- /// let data = json::parse(r#"{"url":"https://github.com/"}"#).unwrap();
281
- /// println!("{}", data);
282
- /// println!("{:#}", data);
283
- /// ```
284
- impl fmt:: Display for JsonValue {
285
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
286
- if f. alternate ( ) {
287
- f. write_str ( & self . pretty ( 4 ) )
288
- } else {
289
- match * self {
290
- JsonValue :: Short ( ref value) => value. fmt ( f) ,
291
- JsonValue :: String ( ref value) => value. fmt ( f) ,
292
- JsonValue :: Number ( ref value) => value. fmt ( f) ,
293
- JsonValue :: Boolean ( ref value) => value. fmt ( f) ,
294
- JsonValue :: Null => f. write_str ( "null" ) ,
295
- _ => f. write_str ( & self . dump ( ) )
296
- }
297
- }
298
- }
299
- }
300
-
301
246
/// Convenience for `JsonValue::from(value)`
302
247
pub fn from < T > ( value : T ) -> JsonValue where T : Into < JsonValue > {
303
248
value. into ( )
@@ -349,221 +294,3 @@ macro_rules! object {
349
294
} )
350
295
}
351
296
352
- macro_rules! implement_extras {
353
- ( $from: ty) => {
354
- impl From <Option <$from>> for JsonValue {
355
- fn from( val: Option <$from>) -> JsonValue {
356
- match val {
357
- Some ( value) => value. into( ) ,
358
- None => Null ,
359
- }
360
- }
361
- }
362
-
363
- impl From <Vec <$from>> for JsonValue {
364
- fn from( mut val: Vec <$from>) -> JsonValue {
365
- JsonValue :: Array (
366
- val. drain( ..)
367
- . map( |value| value. into( ) )
368
- . collect( )
369
- )
370
- }
371
- }
372
-
373
- impl From <Vec <Option <$from>>> for JsonValue {
374
- fn from( mut val: Vec <Option <$from>>) -> JsonValue {
375
- JsonValue :: Array (
376
- val. drain( ..)
377
- . map( |item| item. into( ) )
378
- . collect( )
379
- )
380
- }
381
- }
382
- }
383
- }
384
-
385
- macro_rules! implement_eq {
386
- ( $to: ident, $from: ty) => {
387
- impl PartialEq <$from> for JsonValue {
388
- fn eq( & self , other: & $from) -> bool {
389
- match * self {
390
- JsonValue :: $to( ref value) => value == other,
391
- _ => false
392
- }
393
- }
394
- }
395
-
396
- impl <' a> PartialEq <$from> for & ' a JsonValue {
397
- fn eq( & self , other: & $from) -> bool {
398
- match * * self {
399
- JsonValue :: $to( ref value) => value == other,
400
- _ => false
401
- }
402
- }
403
- }
404
-
405
- impl PartialEq <JsonValue > for $from {
406
- fn eq( & self , other: & JsonValue ) -> bool {
407
- match * other {
408
- JsonValue :: $to( ref value) => value == self ,
409
- _ => false
410
- }
411
- }
412
- }
413
- }
414
- }
415
-
416
- macro_rules! implement {
417
- ( $to: ident, $from: ty as num) => {
418
- impl From <$from> for JsonValue {
419
- fn from( val: $from) -> JsonValue {
420
- JsonValue :: $to( val. into( ) )
421
- }
422
- }
423
-
424
- implement_eq!( $to, $from) ;
425
- implement_extras!( $from) ;
426
- } ;
427
- ( $to: ident, $from: ty) => {
428
- impl From <$from> for JsonValue {
429
- fn from( val: $from) -> JsonValue {
430
- JsonValue :: $to( val)
431
- }
432
- }
433
-
434
- implement_eq!( $to, $from) ;
435
- implement_extras!( $from) ;
436
- }
437
- }
438
-
439
- impl < ' a > From < & ' a str > for JsonValue {
440
- fn from ( val : & ' a str ) -> JsonValue {
441
- if val. len ( ) <= short:: MAX_LEN {
442
- JsonValue :: Short ( unsafe { Short :: from_slice ( val) } )
443
- } else {
444
- JsonValue :: String ( val. into ( ) )
445
- }
446
- }
447
- }
448
-
449
- impl < ' a > From < Option < & ' a str > > for JsonValue {
450
- fn from ( val : Option < & ' a str > ) -> JsonValue {
451
- match val {
452
- Some ( value) => value. into ( ) ,
453
- None => Null ,
454
- }
455
- }
456
- }
457
-
458
- impl From < HashMap < String , JsonValue > > for JsonValue {
459
- fn from ( mut val : HashMap < String , JsonValue > ) -> JsonValue {
460
- let mut object = Object :: with_capacity ( val. len ( ) ) ;
461
-
462
- for ( key, value) in val. drain ( ) {
463
- object. insert ( & key, value) ;
464
- }
465
-
466
- JsonValue :: Object ( object)
467
- }
468
- }
469
-
470
- impl From < Option < HashMap < String , JsonValue > > > for JsonValue {
471
- fn from ( val : Option < HashMap < String , JsonValue > > ) -> JsonValue {
472
- match val {
473
- Some ( value) => value. into ( ) ,
474
- None => Null ,
475
- }
476
- }
477
- }
478
-
479
- impl From < BTreeMap < String , JsonValue > > for JsonValue {
480
- fn from ( mut val : BTreeMap < String , JsonValue > ) -> JsonValue {
481
- let mut object = Object :: with_capacity ( val. len ( ) ) ;
482
-
483
- for ( key, value) in val. iter_mut ( ) {
484
- // Since BTreeMap has no `drain` available, we can use
485
- // the mutable iterator and replace all values by nulls,
486
- // taking ownership and transfering it to the new `Object`.
487
- let value = mem:: replace ( value, Null ) ;
488
- object. insert ( key, value) ;
489
- }
490
-
491
- JsonValue :: Object ( object)
492
- }
493
- }
494
-
495
- impl From < Option < BTreeMap < String , JsonValue > > > for JsonValue {
496
- fn from ( val : Option < BTreeMap < String , JsonValue > > ) -> JsonValue {
497
- match val {
498
- Some ( value) => value. into ( ) ,
499
- None => Null ,
500
- }
501
- }
502
- }
503
-
504
- impl From < Option < JsonValue > > for JsonValue {
505
- fn from ( val : Option < JsonValue > ) -> JsonValue {
506
- match val {
507
- Some ( value) => value,
508
- None => Null ,
509
- }
510
- }
511
- }
512
-
513
- impl < ' a > PartialEq < & ' a str > for JsonValue {
514
- fn eq ( & self , other : & & str ) -> bool {
515
- match * self {
516
- JsonValue :: Short ( ref value) => value == * other,
517
- JsonValue :: String ( ref value) => value == * other,
518
- _ => false
519
- }
520
- }
521
- }
522
-
523
- impl < ' a > PartialEq < JsonValue > for & ' a str {
524
- fn eq ( & self , other : & JsonValue ) -> bool {
525
- match * other {
526
- JsonValue :: Short ( ref value) => value == * self ,
527
- JsonValue :: String ( ref value) => value == * self ,
528
- _ => false
529
- }
530
- }
531
- }
532
-
533
- impl PartialEq < str > for JsonValue {
534
- fn eq ( & self , other : & str ) -> bool {
535
- match * self {
536
- JsonValue :: Short ( ref value) => value == other,
537
- JsonValue :: String ( ref value) => value == other,
538
- _ => false
539
- }
540
- }
541
- }
542
-
543
- impl < ' a > PartialEq < JsonValue > for str {
544
- fn eq ( & self , other : & JsonValue ) -> bool {
545
- match * other {
546
- JsonValue :: Short ( ref value) => value == self ,
547
- JsonValue :: String ( ref value) => value == self ,
548
- _ => false
549
- }
550
- }
551
- }
552
-
553
- implement ! ( String , String ) ;
554
- implement ! ( Number , isize as num) ;
555
- implement ! ( Number , usize as num) ;
556
- implement ! ( Number , i8 as num) ;
557
- implement ! ( Number , i16 as num) ;
558
- implement ! ( Number , i32 as num) ;
559
- implement ! ( Number , i64 as num) ;
560
- implement ! ( Number , u8 as num) ;
561
- implement ! ( Number , u16 as num) ;
562
- implement ! ( Number , u32 as num) ;
563
- implement ! ( Number , u64 as num) ;
564
- implement ! ( Number , f32 as num) ;
565
- implement ! ( Number , f64 as num) ;
566
- implement ! ( Number , Number ) ;
567
- implement ! ( Object , Object ) ;
568
- implement ! ( Array , Array ) ;
569
- implement ! ( Boolean , bool ) ;
0 commit comments