@@ -7,6 +7,12 @@ use crate::Number;
7
7
use std:: borrow:: Cow ;
8
8
use std:: collections:: HashMap ;
9
9
10
+ #[ cfg( any(
11
+ all( aws_sdk_unstable, feature = "serde-deserialize" ) ,
12
+ all( aws_sdk_unstable, feature = "serde-serialize" )
13
+ ) ) ]
14
+ use serde;
15
+
10
16
/* ANCHOR: document */
11
17
12
18
/// Document Type
@@ -16,6 +22,21 @@ use std::collections::HashMap;
16
22
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
17
23
/// The serialization format of a document is an implementation detail of a protocol.
18
24
#[ derive( Clone , Debug , PartialEq ) ]
25
+ #[ cfg_attr(
26
+ all( aws_sdk_unstable, feature = "serde-serialize" ) ,
27
+ derive( serde:: Serialize )
28
+ ) ]
29
+ #[ cfg_attr(
30
+ all( aws_sdk_unstable, feature = "serde-deserialize" ) ,
31
+ derive( serde:: Deserialize )
32
+ ) ]
33
+ #[ cfg_attr(
34
+ any(
35
+ all( aws_sdk_unstable, feature = "serde-deserialize" ) ,
36
+ all( aws_sdk_unstable, feature = "serde-serialize" )
37
+ ) ,
38
+ serde( untagged)
39
+ ) ]
19
40
pub enum Document {
20
41
/// JSON object
21
42
Object ( HashMap < String , Document > ) ,
@@ -207,3 +228,60 @@ impl From<Number> for Document {
207
228
Document :: Number ( value)
208
229
}
209
230
}
231
+
232
+ /* ANCHOR END: document */
233
+
234
+ #[ cfg( test) ]
235
+ mod test {
236
+ /// checks if a) serialization of json suceeds and b) it is compatible with serde_json
237
+ #[ test]
238
+ #[ cfg( all(
239
+ aws_sdk_unstable,
240
+ feature = "serde-serialize" ,
241
+ feature = "serde-deserialize"
242
+ ) ) ]
243
+ fn serialize_json ( ) {
244
+ use crate :: Document ;
245
+ use crate :: Number ;
246
+ use std:: collections:: HashMap ;
247
+ let mut map: HashMap < String , Document > = HashMap :: new ( ) ;
248
+ // string
249
+ map. insert ( "hello" . into ( ) , "world" . to_string ( ) . into ( ) ) ;
250
+ // numbers
251
+ map. insert ( "pos_int" . into ( ) , Document :: Number ( Number :: PosInt ( 1 ) . into ( ) ) ) ;
252
+ map. insert (
253
+ "neg_int" . into ( ) ,
254
+ Document :: Number ( Number :: NegInt ( -1 ) . into ( ) ) ,
255
+ ) ;
256
+ map. insert (
257
+ "float" . into ( ) ,
258
+ Document :: Number ( Number :: Float ( 0.1 + 0.2 ) . into ( ) ) ,
259
+ ) ;
260
+ // booleans
261
+ map. insert ( "true" . into ( ) , true . into ( ) ) ;
262
+ map. insert ( "false" . into ( ) , false . into ( ) ) ;
263
+ // check if array with different datatypes would succeed
264
+ map. insert (
265
+ "array" . into ( ) ,
266
+ vec ! [
267
+ map. clone( ) . into( ) ,
268
+ "hello-world" . to_string( ) . into( ) ,
269
+ true . into( ) ,
270
+ false . into( ) ,
271
+ ]
272
+ . into ( ) ,
273
+ ) ;
274
+ // map
275
+ map. insert ( "map" . into ( ) , map. clone ( ) . into ( ) ) ;
276
+ // null
277
+ map. insert ( "null" . into ( ) , Document :: Null ) ;
278
+ let obj = Document :: Object ( map) ;
279
+ // comparing string isnt going to work since there is no gurantee for the ordering of the keys
280
+ let target_file = include_str ! ( "../test_data/serialize_document.json" ) ;
281
+ let json: Result < serde_json:: Value , _ > = serde_json:: from_str ( target_file) ;
282
+ // serializer
283
+ assert_eq ! ( serde_json:: to_value( & obj) . unwrap( ) , json. unwrap( ) ) ;
284
+ let doc: Result < Document , _ > = serde_json:: from_str ( target_file) ;
285
+ assert_eq ! ( obj, doc. unwrap( ) ) ;
286
+ }
287
+ }
0 commit comments