Update the json examples to use the builder

This commit is contained in:
Erick Tryzelaar 2014-11-14 09:24:32 -08:00
parent 7dee5a64d5
commit db17fe4f3c

View File

@ -116,8 +116,7 @@ extern crate serde_macros;
extern crate serde; extern crate serde;
use std::collections::TreeMap; use std::collections::TreeMap;
use serde::json; use serde::json::{ToJson, Value};
use serde::json::ToJson;
pub struct MyStruct { pub struct MyStruct {
attr1: u8, attr1: u8,
@ -125,7 +124,7 @@ pub struct MyStruct {
} }
impl ToJson for MyStruct { impl ToJson for MyStruct {
fn to_json( &self ) -> json::Value { fn to_json( &self ) -> Value {
let mut d = TreeMap::new(); let mut d = TreeMap::new();
d.insert("attr1".to_string(), self.attr1.to_json()); d.insert("attr1".to_string(), self.attr1.to_json());
d.insert("attr2".to_string(), self.attr2.to_json()); d.insert("attr2".to_string(), self.attr2.to_json());
@ -134,9 +133,40 @@ impl ToJson for MyStruct {
} }
fn main() { fn main() {
let test2: MyStruct = MyStruct {attr1: 1, attr2:"test".to_string()}; let test = MyStruct {attr1: 1, attr2:"test".to_string()};
let tjson: json::Value = test2.to_json(); let json: Value = test.to_json();
let json_str: String = tjson.to_string(); let json_str: String = json.to_string();
}
```
Or you can use the helper type `ObjectBuilder`:
```rust
#![feature(phase)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
use serde::json::{ObjectBuilder, ToJson, Value};
pub struct MyStruct {
attr1: u8,
attr2: String,
}
impl ToJson for MyStruct {
fn to_json( &self ) -> Value {
ObjectBuilder::new()
.insert("attr1", &self.attr1)
.insert("attr2", &self.attr2)
.unwrap()
}
}
fn main() {
let test = MyStruct {attr1: 1, attr2:"test".to_string()};
let json: Value = test.to_json();
let json_str: String = json.to_string();
} }
``` ```
@ -220,7 +250,6 @@ Example of `ToJson` trait implementation for TestStruct1.
extern crate serde_macros; extern crate serde_macros;
extern crate serde; extern crate serde;
use std::collections::TreeMap;
use serde::json::ToJson; use serde::json::ToJson;
use serde::json; use serde::json;
use serde::Deserialize; use serde::Deserialize;
@ -235,24 +264,24 @@ pub struct TestStruct1 {
impl ToJson for TestStruct1 { impl ToJson for TestStruct1 {
fn to_json( &self ) -> json::Value { fn to_json( &self ) -> json::Value {
let mut d = TreeMap::new(); json::builder::ObjectBuilder::new()
d.insert("data_int".to_string(), self.data_int.to_json()); .insert("data_int", &self.data_int)
d.insert("data_str".to_string(), self.data_str.to_json()); .insert("data_str", &self.data_str)
d.insert("data_vector".to_string(), self.data_vector.to_json()); .insert("data_vector", &self.data_vector)
d.to_json() .unwrap()
} }
} }
fn main() { fn main() {
// Serialization using our impl of to_json // Serialization using our impl of to_json
let test2: TestStruct1 = TestStruct1 { let test: TestStruct1 = TestStruct1 {
data_int: 1, data_int: 1,
data_str: "toto".to_string(), data_str: "toto".to_string(),
data_vector: vec![2,3,4,5], data_vector: vec![2,3,4,5],
}; };
let tjson: json::Value = test2.to_json(); let json: json::Value = test.to_json();
let json_str: String = tjson.to_string().into_string(); let json_str: String = json.to_string().into_string();
// Deserialize like before. // Deserialize like before.
@ -280,7 +309,8 @@ use de;
use ser::Serialize; use ser::Serialize;
use ser; use ser;
pub use json::value::{Value, ToJson}; pub use self::value::{Value, ToJson};
pub use self::builder::{ListBuilder, ObjectBuilder};
pub mod builder; pub mod builder;
pub mod value; pub mod value;