1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
pub use unnest as deserialize;
pub use nest as serialize;
/// Wraps an instance of T for nested serialization and deserializion.
/// You will however need to extract and insert the inner value on, so
/// Its recommended to use the entire module at once instead - see the
/// main crate docs or README.md for that.
///
/// ```rust
/// use serde::{Serialize, Deserialize};
/// use serde_json::{json, from_value, to_string_pretty};
/// use serde_nested_json::NestedJson;
///
/// let as_json = json!({
/// "nestedJson": "{\"baz\":123}"
/// });
///
/// #[derive(Serialize, Deserialize)]
/// struct SomeData {
/// baz: u32
/// }
///
/// #[derive(Serialize, Deserialize)]
/// #[serde(rename_all = "camelCase")]
/// struct MyStruct {
/// nested_json: NestedJson<SomeData>
/// }
///
/// // NestedJson<T> implements From<T>, so you can use t.into()
/// // as well as NestedJson::from(t)
/// impl From<SomeData> for MyStruct {
/// fn from(value: SomeData) -> MyStruct {
/// MyStruct {
/// nested_json: value.into()
/// }
/// }
/// }
///
/// // There's also an associated `into` fn which returns T
/// // NestedJson::from is also available, but I havent found
/// // a way to associate it with with the std::conversion
/// // traits using stable features
/// impl From<MyStruct> for SomeData {
/// fn from(value: MyStruct) -> SomeData {
/// value.nested_json.into()
/// }
/// }
///
/// // NestedJson<T> also implements AsRef<T>:
/// impl AsRef<SomeData> for MyStruct {
/// fn as_ref(&self) -> &SomeData {
/// self.nested_json.as_ref()
/// }
/// }
///
/// let a = SomeData { baz: 32 };
/// let b: MyStruct = a.into();
/// let c: SomeData = b.into();
/// let d: NestedJson<SomeData> = c.into();
/// let e: SomeData = d.into();
///
/// let a = SomeData { baz: 32 };
/// let b = MyStruct::from(a);
/// let c = SomeData::from(b);
/// let d = NestedJson::<SomeData>::from(c);
/// // Sadly not possible without manual implementation:
/// // let e = SomeData::from(d);
///
/// println!("{}", to_string_pretty(&from_value::<MyStruct>(as_json).unwrap()).unwrap());
/// ```
;