@@ -16,6 +16,7 @@ export to_str;
16
16
export from_reader;
17
17
export from_str;
18
18
export eq;
19
+ export to_json;
19
20
20
21
export num;
21
22
export string;
@@ -498,6 +499,110 @@ fn eq(value0: json, value1: json) -> bool {
498
499
}
499
500
}
500
501
502
+ iface to_json { fn to_json( ) -> json; }
503
+
504
+ impl of to_json for json {
505
+ fn to_json ( ) -> json { self }
506
+ }
507
+
508
+ impl of to_json for i8 {
509
+ fn to_json ( ) -> json { num ( self as float ) }
510
+ }
511
+
512
+ impl of to_json for i16 {
513
+ fn to_json ( ) -> json { num ( self as float ) }
514
+ }
515
+
516
+ impl of to_json for i32 {
517
+ fn to_json ( ) -> json { num ( self as float ) }
518
+ }
519
+
520
+ impl of to_json for i64 {
521
+ fn to_json ( ) -> json { num ( self as float ) }
522
+ }
523
+
524
+ impl of to_json for u8 {
525
+ fn to_json ( ) -> json { num ( self as float ) }
526
+ }
527
+
528
+ impl of to_json for u16 {
529
+ fn to_json ( ) -> json { num ( self as float ) }
530
+ }
531
+
532
+ impl of to_json for u32 {
533
+ fn to_json ( ) -> json { num ( self as float ) }
534
+ }
535
+
536
+ impl of to_json for u64 {
537
+ fn to_json ( ) -> json { num ( self as float ) }
538
+ }
539
+
540
+ impl of to_json for float {
541
+ fn to_json ( ) -> json { num ( self ) }
542
+ }
543
+
544
+ impl of to_json for f32 {
545
+ fn to_json ( ) -> json { num ( self as float ) }
546
+ }
547
+
548
+ impl of to_json for f64 {
549
+ fn to_json ( ) -> json { num ( self as float ) }
550
+ }
551
+
552
+ impl of to_json for ( ) {
553
+ fn to_json ( ) -> json { null }
554
+ }
555
+
556
+ impl of to_json for bool {
557
+ fn to_json ( ) -> json { boolean ( self ) }
558
+ }
559
+
560
+ impl of to_json for str {
561
+ fn to_json ( ) -> json { string ( self ) }
562
+ }
563
+
564
+ impl < A : to_json copy, B : to_json copy> of to_json for ( A , B ) {
565
+ fn to_json ( ) -> json {
566
+ let ( a, b) = self ;
567
+ list ( [ a. to_json ( ) , b. to_json ( ) ] )
568
+ }
569
+ }
570
+
571
+ impl < A : to_json copy, B : to_json copy, C : to_json copy>
572
+ of to_json for ( A , B , C ) {
573
+ fn to_json ( ) -> json {
574
+ let ( a, b, c) = self ;
575
+ list ( [ a. to_json ( ) , b. to_json ( ) , c. to_json ( ) ] )
576
+ }
577
+ }
578
+
579
+ impl < A : to_json > of to_json for [ A ] {
580
+ fn to_json ( ) -> json { list ( self . map { |elt| elt. to_json ( ) } ) }
581
+ }
582
+
583
+ impl < A : to_json copy> of to_json for hashmap < str , A > {
584
+ fn to_json ( ) -> json {
585
+ let d = map:: str_hash ( ) ;
586
+ for self . each( ) { |key, value|
587
+ d. insert( key, value. to_json( ) ) ;
588
+ }
589
+ dict( d)
590
+ }
591
+ }
592
+
593
+ impl < A : to_json > of to_json for option < A > {
594
+ fn to_json ( ) -> json {
595
+ alt self {
596
+ none { null }
597
+ some( value) { value. to_json ( ) }
598
+ }
599
+ }
600
+ }
601
+
602
+ impl of to_str:: to_str for json {
603
+ fn to_str ( ) -> str { to_str ( self ) }
604
+ }
605
+
501
606
#[ cfg( test) ]
502
607
mod tests {
503
608
fn mk_dict ( items : [ ( str , json ) ] ) -> json {
0 commit comments