@@ -74,51 +74,102 @@ When using `ToJson` the `Encodable` trait implementation is not mandatory.
7474
7575## Using Autoserialization
7676
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.
7979
8080```rust
8181extern crate serialize;
8282use serialize::json;
8383
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 {
8687 data_int: u8,
8788 data_str: String,
8889 data_vector: Vec<u8>,
8990}
9091
9192fn 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+ };
9498
9599 // Serialize using `json::encode`
96100 let encoded = json::encode(&object);
97101
98102 // Deserialize using `json::decode`
99- let decoded: TestStruct1 = json::decode(encoded.as_slice()).unwrap();
103+ let decoded: TestStruct = json::decode(encoded.as_slice()).unwrap();
100104}
101105```
102106
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+ }
104131
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
106153
107154```rust
155+ extern crate serialize;
108156use std::collections::TreeMap;
109157use serialize::json::ToJson;
110158use serialize::json;
111159
160+ // Only generate `Decodable` trait implementation
112161#[deriving(Decodable)]
113- pub struct TestStruct1 {
162+ pub struct TestStruct {
114163 data_int: u8,
115164 data_str: String,
116165 data_vector: Vec<u8>,
117166}
118167
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 {
121171 let mut d = TreeMap::new();
172+ // All standard types implement `to_json()`, so use it
122173 d.insert("data_int".to_string(), self.data_int.to_json());
123174 d.insert("data_str".to_string(), self.data_str.to_json());
124175 d.insert("data_vector".to_string(), self.data_vector.to_json());
@@ -128,12 +179,16 @@ impl ToJson for TestStruct1 {
128179
129180fn main() {
130181 // 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();
134189
135190 // 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();
137192}
138193```
139194
0 commit comments