Skip to content

Commit 9d56db1

Browse files
Add serde support to document type. (#2616)
## Description Implements serde support for `Document`. ## Testing Test checks whether the serialized/de-serialized data matches with the expected value ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 1e2c03c commit 9d56db1

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

rust-runtime/aws-smithy-types/src/document.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use crate::Number;
77
use std::borrow::Cow;
88
use std::collections::HashMap;
99

10+
#[cfg(any(
11+
all(aws_sdk_unstable, feature = "serde-deserialize"),
12+
all(aws_sdk_unstable, feature = "serde-serialize")
13+
))]
14+
use serde;
15+
1016
/* ANCHOR: document */
1117

1218
/// Document Type
@@ -16,6 +22,21 @@ use std::collections::HashMap;
1622
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
1723
/// The serialization format of a document is an implementation detail of a protocol.
1824
#[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+
)]
1940
pub enum Document {
2041
/// JSON object
2142
Object(HashMap<String, Document>),
@@ -207,3 +228,60 @@ impl From<Number> for Document {
207228
Document::Number(value)
208229
}
209230
}
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+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"null": null,
3+
"true": true,
4+
"pos_int": 1,
5+
"false": false,
6+
"map": {
7+
"array": [
8+
{
9+
"pos_int": 1,
10+
"float": 0.30000000000000004,
11+
"neg_int": -1,
12+
"hello": "world",
13+
"false": false,
14+
"true": true
15+
},
16+
"hello-world",
17+
true,
18+
false
19+
],
20+
"pos_int": 1,
21+
"float": 0.30000000000000004,
22+
"neg_int": -1,
23+
"hello": "world",
24+
"false": false,
25+
"true": true
26+
},
27+
"float": 0.30000000000000004,
28+
"neg_int": -1,
29+
"hello": "world",
30+
"array": [
31+
{
32+
"pos_int": 1,
33+
"float": 0.30000000000000004,
34+
"neg_int": -1,
35+
"hello": "world",
36+
"false": false,
37+
"true": true
38+
},
39+
"hello-world",
40+
true,
41+
false
42+
]
43+
}

0 commit comments

Comments
 (0)