1
1
//! Serialize a Rust data structure into JSON data
2
2
3
- use core:: fmt;
3
+ use core:: { fmt, str } ;
4
4
5
5
use serde:: ser;
6
6
use serde:: ser:: SerializeStruct as _;
@@ -425,23 +425,21 @@ impl<'a, 'b: 'a> ser::Serializer for &'a mut Serializer<'b> {
425
425
426
426
/// Serializes the given data structure as a string of JSON text
427
427
#[ cfg( feature = "heapless" ) ]
428
- pub fn to_string < B , T > ( value : & T ) -> Result < String < B > >
428
+ pub fn to_string < T , const N : usize > ( value : & T ) -> Result < String < N > >
429
429
where
430
- B : heapless:: ArrayLength < u8 > ,
431
430
T : ser:: Serialize + ?Sized ,
432
431
{
433
- Ok ( unsafe { String :: from_utf8_unchecked ( to_vec ( value) ?) } )
432
+ Ok ( unsafe { str :: from_utf8_unchecked ( & to_vec :: < T , N > ( value) ?) } . into ( ) )
434
433
}
435
434
436
435
/// Serializes the given data structure as a JSON byte vector
437
436
#[ cfg( feature = "heapless" ) ]
438
- pub fn to_vec < B , T > ( value : & T ) -> Result < Vec < u8 , B > >
437
+ pub fn to_vec < T , const N : usize > ( value : & T ) -> Result < Vec < u8 , N > >
439
438
where
440
- B : heapless:: ArrayLength < u8 > ,
441
439
T : ser:: Serialize + ?Sized ,
442
440
{
443
- let mut buf = Vec :: < u8 , B > :: new ( ) ;
444
- buf. resize_default ( B :: to_usize ( ) ) ?;
441
+ let mut buf = Vec :: < u8 , N > :: new ( ) ;
442
+ buf. resize_default ( N ) ?;
445
443
let len = to_slice ( value, & mut buf) ?;
446
444
buf. truncate ( len) ;
447
445
Ok ( buf)
@@ -521,17 +519,15 @@ impl ser::SerializeMap for Unreachable {
521
519
mod tests {
522
520
use serde_derive:: Serialize ;
523
521
524
- use heapless:: consts:: U128 ;
525
-
526
- type N = U128 ;
522
+ const N : usize = 128 ;
527
523
528
524
#[ test]
529
525
fn array ( ) {
530
526
let buf = & mut [ 0u8 ; 128 ] ;
531
527
let len = crate :: to_slice ( & [ 0 , 1 , 2 ] , buf) . unwrap ( ) ;
532
528
assert_eq ! ( len, 7 ) ;
533
529
assert_eq ! ( & buf[ ..len] , b"[0,1,2]" ) ;
534
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & [ 0 , 1 , 2 ] ) . unwrap( ) , "[0,1,2]" ) ;
530
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & [ 0 , 1 , 2 ] ) . unwrap( ) , "[0,1,2]" ) ;
535
531
}
536
532
537
533
#[ test]
@@ -541,7 +537,7 @@ mod tests {
541
537
assert_eq ! ( len, 4 ) ;
542
538
assert_eq ! ( & buf[ ..len] , b"true" ) ;
543
539
544
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & true ) . unwrap( ) , "true" ) ;
540
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & true ) . unwrap( ) , "true" ) ;
545
541
}
546
542
547
543
#[ test]
@@ -555,83 +551,83 @@ mod tests {
555
551
}
556
552
557
553
assert_eq ! (
558
- & * crate :: to_string:: <N , _ >( & Type :: Boolean ) . unwrap( ) ,
554
+ & * crate :: to_string:: <_ , N >( & Type :: Boolean ) . unwrap( ) ,
559
555
r#""boolean""#
560
556
) ;
561
557
562
558
assert_eq ! (
563
- & * crate :: to_string:: <N , _ >( & Type :: Number ) . unwrap( ) ,
559
+ & * crate :: to_string:: <_ , N >( & Type :: Number ) . unwrap( ) ,
564
560
r#""number""#
565
561
) ;
566
562
}
567
563
568
564
#[ test]
569
565
fn str ( ) {
570
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "hello" ) . unwrap( ) , r#""hello""# ) ;
571
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "" ) . unwrap( ) , r#""""# ) ;
566
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "hello" ) . unwrap( ) , r#""hello""# ) ;
567
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "" ) . unwrap( ) , r#""""# ) ;
572
568
573
569
// Characters unescaped if possible
574
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "ä" ) . unwrap( ) , r#""ä""# ) ;
575
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "৬" ) . unwrap( ) , r#""৬""# ) ;
576
- // assert_eq!(&*crate::to_string::<N, _ >("\u{A0}").unwrap(), r#"" ""#); // non-breaking space
577
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "ℝ" ) . unwrap( ) , r#""ℝ""# ) ; // 3 byte character
578
- assert_eq ! ( & * crate :: to_string:: <N , _ >( "💣" ) . unwrap( ) , r#""💣""# ) ; // 4 byte character
570
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "ä" ) . unwrap( ) , r#""ä""# ) ;
571
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "৬" ) . unwrap( ) , r#""৬""# ) ;
572
+ // assert_eq!(&*crate::to_string::<_, N >("\u{A0}").unwrap(), r#"" ""#); // non-breaking space
573
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "ℝ" ) . unwrap( ) , r#""ℝ""# ) ; // 3 byte character
574
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( "💣" ) . unwrap( ) , r#""💣""# ) ; // 4 byte character
579
575
580
576
// " and \ must be escaped
581
577
assert_eq ! (
582
- & * crate :: to_string:: <N , _ >( "foo\" bar" ) . unwrap( ) ,
578
+ & * crate :: to_string:: <_ , N >( "foo\" bar" ) . unwrap( ) ,
583
579
r#""foo\"bar""#
584
580
) ;
585
581
assert_eq ! (
586
- & * crate :: to_string:: <N , _ >( "foo\\ bar" ) . unwrap( ) ,
582
+ & * crate :: to_string:: <_ , N >( "foo\\ bar" ) . unwrap( ) ,
587
583
r#""foo\\bar""#
588
584
) ;
589
585
590
586
// \b, \t, \n, \f, \r must be escaped in their two-character escaping
591
587
assert_eq ! (
592
- & * crate :: to_string:: <N , _ >( " \u{0008} " ) . unwrap( ) ,
588
+ & * crate :: to_string:: <_ , N >( " \u{0008} " ) . unwrap( ) ,
593
589
r#"" \b ""#
594
590
) ;
595
591
assert_eq ! (
596
- & * crate :: to_string:: <N , _ >( " \u{0009} " ) . unwrap( ) ,
592
+ & * crate :: to_string:: <_ , N >( " \u{0009} " ) . unwrap( ) ,
597
593
r#"" \t ""#
598
594
) ;
599
595
assert_eq ! (
600
- & * crate :: to_string:: <N , _ >( " \u{000A} " ) . unwrap( ) ,
596
+ & * crate :: to_string:: <_ , N >( " \u{000A} " ) . unwrap( ) ,
601
597
r#"" \n ""#
602
598
) ;
603
599
assert_eq ! (
604
- & * crate :: to_string:: <N , _ >( " \u{000C} " ) . unwrap( ) ,
600
+ & * crate :: to_string:: <_ , N >( " \u{000C} " ) . unwrap( ) ,
605
601
r#"" \f ""#
606
602
) ;
607
603
assert_eq ! (
608
- & * crate :: to_string:: <N , _ >( " \u{000D} " ) . unwrap( ) ,
604
+ & * crate :: to_string:: <_ , N >( " \u{000D} " ) . unwrap( ) ,
609
605
r#"" \r ""#
610
606
) ;
611
607
612
608
// U+0000 through U+001F is escaped using six-character \u00xx uppercase hexadecimal escape sequences
613
609
assert_eq ! (
614
- & * crate :: to_string:: <N , _ >( " \u{0000} " ) . unwrap( ) ,
610
+ & * crate :: to_string:: <_ , N >( " \u{0000} " ) . unwrap( ) ,
615
611
r#"" \u0000 ""#
616
612
) ;
617
613
assert_eq ! (
618
- & * crate :: to_string:: <N , _ >( " \u{0001} " ) . unwrap( ) ,
614
+ & * crate :: to_string:: <_ , N >( " \u{0001} " ) . unwrap( ) ,
619
615
r#"" \u0001 ""#
620
616
) ;
621
617
assert_eq ! (
622
- & * crate :: to_string:: <N , _ >( " \u{0007} " ) . unwrap( ) ,
618
+ & * crate :: to_string:: <_ , N >( " \u{0007} " ) . unwrap( ) ,
623
619
r#"" \u0007 ""#
624
620
) ;
625
621
assert_eq ! (
626
- & * crate :: to_string:: <N , _ >( " \u{000e} " ) . unwrap( ) ,
622
+ & * crate :: to_string:: <_ , N >( " \u{000e} " ) . unwrap( ) ,
627
623
r#"" \u000E ""#
628
624
) ;
629
625
assert_eq ! (
630
- & * crate :: to_string:: <N , _ >( " \u{001D} " ) . unwrap( ) ,
626
+ & * crate :: to_string:: <_ , N >( " \u{001D} " ) . unwrap( ) ,
631
627
r#"" \u001D ""#
632
628
) ;
633
629
assert_eq ! (
634
- & * crate :: to_string:: <N , _ >( " \u{001f} " ) . unwrap( ) ,
630
+ & * crate :: to_string:: <_ , N >( " \u{001f} " ) . unwrap( ) ,
635
631
r#"" \u001F ""#
636
632
) ;
637
633
}
@@ -644,7 +640,7 @@ mod tests {
644
640
}
645
641
646
642
assert_eq ! (
647
- & * crate :: to_string:: <N , _ >( & Led { led: true } ) . unwrap( ) ,
643
+ & * crate :: to_string:: <_ , N >( & Led { led: true } ) . unwrap( ) ,
648
644
r#"{"led":true}"#
649
645
) ;
650
646
}
@@ -657,22 +653,22 @@ mod tests {
657
653
}
658
654
659
655
assert_eq ! (
660
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: 127 } ) . unwrap( ) ,
656
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: 127 } ) . unwrap( ) ,
661
657
r#"{"temperature":127}"#
662
658
) ;
663
659
664
660
assert_eq ! (
665
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: 20 } ) . unwrap( ) ,
661
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: 20 } ) . unwrap( ) ,
666
662
r#"{"temperature":20}"#
667
663
) ;
668
664
669
665
assert_eq ! (
670
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: -17 } ) . unwrap( ) ,
666
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: -17 } ) . unwrap( ) ,
671
667
r#"{"temperature":-17}"#
672
668
) ;
673
669
674
670
assert_eq ! (
675
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: -128 } ) . unwrap( ) ,
671
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: -128 } ) . unwrap( ) ,
676
672
r#"{"temperature":-128}"#
677
673
) ;
678
674
}
@@ -685,20 +681,20 @@ mod tests {
685
681
}
686
682
687
683
assert_eq ! (
688
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: -20. } ) . unwrap( ) ,
684
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: -20. } ) . unwrap( ) ,
689
685
r#"{"temperature":-20.0}"#
690
686
) ;
691
687
692
688
assert_eq ! (
693
- & * crate :: to_string:: <N , _ >( & Temperature {
689
+ & * crate :: to_string:: <_ , N >( & Temperature {
694
690
temperature: -20345.
695
691
} )
696
692
. unwrap( ) ,
697
693
r#"{"temperature":-20345.0}"#
698
694
) ;
699
695
700
696
assert_eq ! (
701
- & * crate :: to_string:: <N , _ >( & Temperature {
697
+ & * crate :: to_string:: <_ , N >( & Temperature {
702
698
temperature: -2.3456789012345e-23
703
699
} )
704
700
. unwrap( ) ,
@@ -714,7 +710,7 @@ mod tests {
714
710
}
715
711
716
712
assert_eq ! (
717
- crate :: to_string:: <N , _ >( & Property {
713
+ crate :: to_string:: <_ , N >( & Property {
718
714
description: Some ( "An ambient temperature sensor" ) ,
719
715
} )
720
716
. unwrap( ) ,
@@ -723,7 +719,7 @@ mod tests {
723
719
724
720
// XXX Ideally this should produce "{}"
725
721
assert_eq ! (
726
- crate :: to_string:: <N , _ >( & Property { description: None } ) . unwrap( ) ,
722
+ crate :: to_string:: <_ , N >( & Property { description: None } ) . unwrap( ) ,
727
723
r#"{"description":null}"#
728
724
) ;
729
725
}
@@ -736,7 +732,7 @@ mod tests {
736
732
}
737
733
738
734
assert_eq ! (
739
- & * crate :: to_string:: <N , _ >( & Temperature { temperature: 20 } ) . unwrap( ) ,
735
+ & * crate :: to_string:: <_ , N >( & Temperature { temperature: 20 } ) . unwrap( ) ,
740
736
r#"{"temperature":20}"#
741
737
) ;
742
738
}
@@ -746,7 +742,7 @@ mod tests {
746
742
#[ derive( Serialize ) ]
747
743
struct Empty { }
748
744
749
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & Empty { } ) . unwrap( ) , r#"{}"# ) ;
745
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & Empty { } ) . unwrap( ) , r#"{}"# ) ;
750
746
751
747
#[ derive( Serialize ) ]
752
748
struct Tuple {
@@ -755,23 +751,23 @@ mod tests {
755
751
}
756
752
757
753
assert_eq ! (
758
- & * crate :: to_string:: <N , _ >( & Tuple { a: true , b: false } ) . unwrap( ) ,
754
+ & * crate :: to_string:: <_ , N >( & Tuple { a: true , b: false } ) . unwrap( ) ,
759
755
r#"{"a":true,"b":false}"#
760
756
) ;
761
757
}
762
758
763
759
#[ test]
764
760
fn test_unit ( ) {
765
761
let a = ( ) ;
766
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & a) . unwrap( ) , r#"null"# ) ;
762
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & a) . unwrap( ) , r#"null"# ) ;
767
763
}
768
764
769
765
#[ test]
770
766
fn test_newtype_struct ( ) {
771
767
#[ derive( Serialize ) ]
772
768
struct A ( pub u32 ) ;
773
769
let a = A ( 54 ) ;
774
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & a) . unwrap( ) , r#"54"# ) ;
770
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & a) . unwrap( ) , r#"54"# ) ;
775
771
}
776
772
777
773
#[ test]
@@ -782,7 +778,7 @@ mod tests {
782
778
}
783
779
let a = A :: A ( 54 ) ;
784
780
785
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & a) . unwrap( ) , r#"{"A":54}"# ) ;
781
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & a) . unwrap( ) , r#"{"A":54}"# ) ;
786
782
}
787
783
788
784
#[ test]
@@ -794,15 +790,15 @@ mod tests {
794
790
let a = A :: A { x : 54 , y : 720 } ;
795
791
796
792
assert_eq ! (
797
- & * crate :: to_string:: <N , _ >( & a) . unwrap( ) ,
793
+ & * crate :: to_string:: <_ , N >( & a) . unwrap( ) ,
798
794
r#"{"A":{"x":54,"y":720}}"#
799
795
) ;
800
796
}
801
797
802
798
#[ test]
803
799
fn test_serialize_bytes ( ) {
804
800
use core:: fmt:: Write ;
805
- use heapless:: { consts :: U48 , String } ;
801
+ use heapless:: String ;
806
802
807
803
pub struct SimpleDecimal ( f32 ) ;
808
804
@@ -811,19 +807,19 @@ mod tests {
811
807
where
812
808
S : serde:: Serializer ,
813
809
{
814
- let mut aux: String < U48 > = String :: new ( ) ;
810
+ let mut aux: String < { N } > = String :: new ( ) ;
815
811
write ! ( aux, "{:.2}" , self . 0 ) . unwrap ( ) ;
816
812
serializer. serialize_bytes ( & aux. as_bytes ( ) )
817
813
}
818
814
}
819
815
820
816
let sd1 = SimpleDecimal ( 1.55555 ) ;
821
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & sd1) . unwrap( ) , r#"1.56"# ) ;
817
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & sd1) . unwrap( ) , r#"1.56"# ) ;
822
818
823
819
let sd2 = SimpleDecimal ( 0.000 ) ;
824
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & sd2) . unwrap( ) , r#"0.00"# ) ;
820
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & sd2) . unwrap( ) , r#"0.00"# ) ;
825
821
826
822
let sd3 = SimpleDecimal ( 22222.777777 ) ;
827
- assert_eq ! ( & * crate :: to_string:: <N , _ >( & sd3) . unwrap( ) , r#"22222.78"# ) ;
823
+ assert_eq ! ( & * crate :: to_string:: <_ , N >( & sd3) . unwrap( ) , r#"22222.78"# ) ;
828
824
}
829
825
}
0 commit comments