@@ -15,44 +15,26 @@ use itertools::Itertools as _;
15
15
#[ derive( Debug , Clone , PartialEq , Eq , PartialOrd , Ord , Serialize , Deserialize ) ]
16
16
#[ serde( from = "Value" ) ]
17
17
#[ serde( into = "Value" ) ]
18
- pub struct SortedJson ( String ) ;
18
+ pub ( crate ) struct SortedJson ( String ) ;
19
19
20
20
impl SortedJson {
21
21
/// 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 {
23
23
SortedJson ( serde_json:: to_string ( & item) . unwrap ( ) )
24
24
}
25
25
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
-
33
26
/// 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 {
35
28
let items = items. into_iter ( )
36
29
. sorted_unstable_by ( |a, b| a. borrow ( ) . cmp ( & b. borrow ( ) ) )
37
30
. format_with ( "," , |item, f| f ( item. borrow ( ) ) ) ;
38
31
SortedJson ( format ! ( "[{}]" , items) )
39
32
}
40
33
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 {
42
35
let items = items. into_iter ( ) . format_with ( "," , |item, f| f ( item. borrow ( ) ) ) ;
43
36
SortedJson ( format ! ( "[{items}]" ) )
44
37
}
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
- }
56
38
}
57
39
58
40
impl fmt:: Display for SortedJson {
@@ -80,8 +62,16 @@ mod tests {
80
62
fn check ( json : SortedJson , serialized : & str ) {
81
63
assert_eq ! ( json. to_string( ) , serialized) ;
82
64
assert_eq ! ( serde_json:: to_string( & json) . unwrap( ) , serialized) ;
65
+
83
66
let json = json. to_string ( ) ;
84
67
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
+
85
75
assert_eq ! ( json. to_string( ) , serialized) ;
86
76
assert_eq ! ( serde_json:: to_string( & json) . unwrap( ) , serialized) ;
87
77
}
@@ -100,6 +90,13 @@ mod tests {
100
90
check ( json, serialized) ;
101
91
}
102
92
93
+ #[ test]
94
+ fn string ( ) {
95
+ let json = SortedJson :: serialize ( "he\" llo" ) ;
96
+ let serialized = r#""he\"llo""# ;
97
+ check ( json, serialized) ;
98
+ }
99
+
103
100
#[ test]
104
101
fn serialize_array ( ) {
105
102
let json = SortedJson :: serialize ( [ 3 , 1 , 2 ] ) ;
@@ -116,6 +113,17 @@ mod tests {
116
113
check ( json, serialized) ;
117
114
}
118
115
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
+
119
127
#[ test]
120
128
fn array_unsorted ( ) {
121
129
let items = [ "c" , "a" , "b" ] ;
@@ -124,15 +132,4 @@ mod tests {
124
132
let json = SortedJson :: array_unsorted ( items) ;
125
133
check ( json, serialized) ;
126
134
}
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
- }
138
135
}
0 commit comments