@@ -74,51 +74,102 @@ When using `ToJson` the `Encodable` trait implementation is not mandatory.
74
74
75
75
## Using Autoserialization
76
76
77
- Create a struct called TestStruct1 and serialize and deserialize it to and from JSON
78
- using the serialization API, using the derived serialization code.
77
+ Create a struct called `TestStruct` and serialize and deserialize it to and from JSON using the
78
+ serialization API, using the derived serialization code.
79
79
80
80
```rust
81
81
extern crate serialize;
82
82
use serialize::json;
83
83
84
- #[deriving(Decodable, Encodable)] //generate Decodable, Encodable impl.
85
- pub struct TestStruct1 {
84
+ // Automatically generate `Decodable` and `Encodable` trait implementations
85
+ #[deriving(Decodable, Encodable)]
86
+ pub struct TestStruct {
86
87
data_int: u8,
87
88
data_str: String,
88
89
data_vector: Vec<u8>,
89
90
}
90
91
91
92
fn main() {
92
- let object = TestStruct1
93
- {data_int: 1, data_str:"toto".to_string(), data_vector:vec![2,3,4,5]};
93
+ let object = TestStruct {
94
+ data_int: 1,
95
+ data_str: "toto".to_string(),
96
+ data_vector: vec![2,3,4,5],
97
+ };
94
98
95
99
// Serialize using `json::encode`
96
100
let encoded = json::encode(&object);
97
101
98
102
// Deserialize using `json::decode`
99
- let decoded: TestStruct1 = json::decode(encoded.as_slice()).unwrap();
103
+ let decoded: TestStruct = json::decode(encoded.as_slice()).unwrap();
100
104
}
101
105
```
102
106
103
- ## Using `ToJson`
107
+ ## Using the `ToJson` trait
108
+
109
+ The examples above use the `ToJson` trait to generate the JSON string, which required
110
+ for custom mappings.
111
+
112
+ ### Simple example of `ToJson` usage
113
+
114
+ ```rust
115
+ extern crate serialize;
116
+ use serialize::json::ToJson;
117
+ use serialize::json;
118
+
119
+ // A custom data structure
120
+ struct ComplexNum {
121
+ a: f64,
122
+ b: f64,
123
+ }
124
+
125
+ // JSON value representation
126
+ impl ToJson for ComplexNum {
127
+ fn to_json(&self) -> json::Json {
128
+ json::String(format!("{}+{}i", self.a, self.b))
129
+ }
130
+ }
104
131
105
- This example uses the `ToJson` trait to generate the JSON string.
132
+ // Only generate `Encodable` trait implementation
133
+ #[deriving(Encodable)]
134
+ pub struct ComplexNumRecord {
135
+ uid: u8,
136
+ dsc: String,
137
+ val: json::Json,
138
+ }
139
+
140
+ fn main() {
141
+ let num = ComplexNum { a: 0.0001, b: 12.539 };
142
+ let data: String = json::encode(&ComplexNumRecord{
143
+ uid: 1,
144
+ dsc: "test".to_string(),
145
+ val: num.to_json(),
146
+ });
147
+ println!("data: {}", data);
148
+ // data: {"uid":1,"dsc":"test","val":"0.0001+12.539j"};
149
+ }
150
+ ```
151
+
152
+ ### Verbose example of `ToJson` usage
106
153
107
154
```rust
155
+ extern crate serialize;
108
156
use std::collections::TreeMap;
109
157
use serialize::json::ToJson;
110
158
use serialize::json;
111
159
160
+ // Only generate `Decodable` trait implementation
112
161
#[deriving(Decodable)]
113
- pub struct TestStruct1 {
162
+ pub struct TestStruct {
114
163
data_int: u8,
115
164
data_str: String,
116
165
data_vector: Vec<u8>,
117
166
}
118
167
119
- impl ToJson for TestStruct1 {
120
- fn to_json( &self ) -> json::Json {
168
+ // Specify encoding method manually
169
+ impl ToJson for TestStruct {
170
+ fn to_json(&self) -> json::Json {
121
171
let mut d = TreeMap::new();
172
+ // All standard types implement `to_json()`, so use it
122
173
d.insert("data_int".to_string(), self.data_int.to_json());
123
174
d.insert("data_str".to_string(), self.data_str.to_json());
124
175
d.insert("data_vector".to_string(), self.data_vector.to_json());
@@ -128,12 +179,16 @@ impl ToJson for TestStruct1 {
128
179
129
180
fn main() {
130
181
// Serialize using `ToJson`
131
- let test2 = TestStruct1 {data_int: 1, data_str:"toto".to_string(), data_vector:vec![2,3,4,5]};
132
- let tjson: json::Json = test2.to_json();
133
- let json_str: String = tjson.to_string();
182
+ let input_data = TestStruct {
183
+ data_int: 1,
184
+ data_str: "toto".to_string(),
185
+ data_vector: vec![2,3,4,5],
186
+ };
187
+ let json_obj: json::Json = input_data.to_json();
188
+ let json_str: String = json_obj.to_string();
134
189
135
190
// Deserialize like before
136
- let decoded: TestStruct1 = json::decode(json_str.as_slice()).unwrap();
191
+ let decoded: TestStruct = json::decode(json_str.as_slice()).unwrap();
137
192
}
138
193
```
139
194
0 commit comments