@@ -15,44 +15,26 @@ use itertools::Itertools as _;
1515#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Serialize , Deserialize ) ]
1616#[ serde( from = "Value" ) ]
1717#[ serde( into = "Value" ) ]
18- pub struct SortedJson ( String ) ;
18+ pub ( crate ) struct SortedJson ( String ) ;
1919
2020impl SortedJson {
2121 /// If you pass in an array, it will not be sorted.
22- pub fn serialize < T : Serialize > ( item : T ) -> Self {
22+ pub ( crate ) fn serialize < T : Serialize > ( item : T ) -> Self {
2323 SortedJson ( serde_json:: to_string ( & item) . unwrap ( ) )
2424 }
2525
26- /// Assumes that `item` is already JSON encoded
27- ///
28- /// TODO: remove this, and use SortedJson everywhere JSON is rendered
29- pub fn preserialized ( item : String ) -> Self {
30- SortedJson ( item)
31- }
32-
3326 /// Serializes and sorts
34- pub fn array < T : Borrow < SortedJson > , I : IntoIterator < Item =T > > ( items : I ) -> Self {
27+ pub ( crate ) fn array < T : Borrow < SortedJson > , I : IntoIterator < Item =T > > ( items : I ) -> Self {
3528 let items = items. into_iter ( )
3629 . sorted_unstable_by ( |a, b| a. borrow ( ) . cmp ( & b. borrow ( ) ) )
3730 . format_with ( "," , |item, f| f ( item. borrow ( ) ) ) ;
3831 SortedJson ( format ! ( "[{}]" , items) )
3932 }
4033
41- pub fn array_unsorted < T : Borrow < SortedJson > , I : IntoIterator < Item =T > > ( items : I ) -> Self {
34+ pub ( crate ) fn array_unsorted < T : Borrow < SortedJson > , I : IntoIterator < Item =T > > ( items : I ) -> Self {
4235 let items = items. into_iter ( ) . format_with ( "," , |item, f| f ( item. borrow ( ) ) ) ;
4336 SortedJson ( format ! ( "[{items}]" ) )
4437 }
45-
46- pub fn object < K , V , I > ( items : I ) -> Self
47- where K : Borrow < SortedJson > ,
48- V : Borrow < SortedJson > ,
49- I : IntoIterator < Item =( K , V ) > ,
50- {
51- let items = items. into_iter ( )
52- . sorted_unstable_by ( |a, b| a. 0 . borrow ( ) . cmp ( & b. 0 . borrow ( ) ) )
53- . format_with ( "," , |( k, v) , f| f ( & format_args ! ( "{}:{}" , k. borrow( ) , v. borrow( ) ) ) ) ;
54- SortedJson ( format ! ( "{{{}}}" , items) )
55- }
5638}
5739
5840impl fmt:: Display for SortedJson {
@@ -80,8 +62,16 @@ mod tests {
8062 fn check ( json : SortedJson , serialized : & str ) {
8163 assert_eq ! ( json. to_string( ) , serialized) ;
8264 assert_eq ! ( serde_json:: to_string( & json) . unwrap( ) , serialized) ;
65+
8366 let json = json. to_string ( ) ;
8467 let json: SortedJson = serde_json:: from_str ( & json) . unwrap ( ) ;
68+
69+ assert_eq ! ( json. to_string( ) , serialized) ;
70+ assert_eq ! ( serde_json:: to_string( & json) . unwrap( ) , serialized) ;
71+
72+ let json = serde_json:: to_string ( & json) . unwrap ( ) ;
73+ let json: SortedJson = serde_json:: from_str ( & json) . unwrap ( ) ;
74+
8575 assert_eq ! ( json. to_string( ) , serialized) ;
8676 assert_eq ! ( serde_json:: to_string( & json) . unwrap( ) , serialized) ;
8777 }
@@ -100,6 +90,13 @@ mod tests {
10090 check ( json, serialized) ;
10191 }
10292
93+ #[ test]
94+ fn string ( ) {
95+ let json = SortedJson :: serialize ( "he\" llo" ) ;
96+ let serialized = r#""he\"llo""# ;
97+ check ( json, serialized) ;
98+ }
99+
103100 #[ test]
104101 fn serialize_array ( ) {
105102 let json = SortedJson :: serialize ( [ 3 , 1 , 2 ] ) ;
@@ -116,6 +113,17 @@ mod tests {
116113 check ( json, serialized) ;
117114 }
118115
116+ #[ test]
117+ fn nested_array ( ) {
118+ let a = SortedJson :: serialize ( 3 ) ;
119+ let b = SortedJson :: serialize ( 2 ) ;
120+ let c = SortedJson :: serialize ( 1 ) ;
121+ let d = SortedJson :: serialize ( [ 1 , 3 , 2 ] ) ;
122+ let json = SortedJson :: array ( [ a, b, c, d] ) ;
123+ let serialized = r#"[1,2,3,[1,3,2]]"# ;
124+ check ( json, serialized) ;
125+ }
126+
119127 #[ test]
120128 fn array_unsorted ( ) {
121129 let items = [ "c" , "a" , "b" ] ;
@@ -124,15 +132,4 @@ mod tests {
124132 let json = SortedJson :: array_unsorted ( items) ;
125133 check ( json, serialized) ;
126134 }
127-
128- #[ test]
129- fn object ( ) {
130- let items = [ ( "c" , 1 ) , ( "a" , 10 ) , ( "b" , 3 ) ] ;
131- let serialized = r#"{"a":10,"b":3,"c":1}"# ;
132- let items: Vec < ( SortedJson , SortedJson ) > = items. into_iter ( )
133- . map ( |( k, v) | ( SortedJson :: serialize ( k) , SortedJson :: serialize ( v) ) )
134- . collect ( ) ;
135- let json = SortedJson :: object ( items) ;
136- check ( json, serialized) ;
137- }
138135}
0 commit comments