2015-03-05 21:49:17 -06:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2015-07-22 10:06:47 -05:00
|
|
|
use serde_json::value::Value;
|
|
|
|
use serde_json::builder::{ArrayBuilder, ObjectBuilder};
|
2015-03-05 21:49:17 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_array_builder() {
|
|
|
|
let value = ArrayBuilder::new().unwrap();
|
|
|
|
assert_eq!(value, Value::Array(Vec::new()));
|
|
|
|
|
|
|
|
let value = ArrayBuilder::new()
|
|
|
|
.push(1)
|
|
|
|
.push(2)
|
|
|
|
.push(3)
|
|
|
|
.unwrap();
|
2015-03-18 22:51:48 -05:00
|
|
|
assert_eq!(value, Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))));
|
2015-03-05 21:49:17 -06:00
|
|
|
|
|
|
|
let value = ArrayBuilder::new()
|
|
|
|
.push_array(|bld| bld.push(1).push(2).push(3))
|
|
|
|
.unwrap();
|
2015-03-18 22:51:48 -05:00
|
|
|
assert_eq!(value, Value::Array(vec!(Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))))));
|
2015-03-05 21:49:17 -06:00
|
|
|
|
|
|
|
let value = ArrayBuilder::new()
|
|
|
|
.push_object(|bld|
|
|
|
|
bld
|
|
|
|
.insert("a".to_string(), 1)
|
|
|
|
.insert("b".to_string(), 2))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
2015-03-18 22:51:48 -05:00
|
|
|
map.insert("a".to_string(), Value::U64(1));
|
|
|
|
map.insert("b".to_string(), Value::U64(2));
|
2015-03-05 21:49:17 -06:00
|
|
|
assert_eq!(value, Value::Array(vec!(Value::Object(map))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_object_builder() {
|
|
|
|
let value = ObjectBuilder::new().unwrap();
|
|
|
|
assert_eq!(value, Value::Object(BTreeMap::new()));
|
|
|
|
|
|
|
|
let value = ObjectBuilder::new()
|
|
|
|
.insert("a".to_string(), 1)
|
|
|
|
.insert("b".to_string(), 2)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut map = BTreeMap::new();
|
2015-03-18 22:51:48 -05:00
|
|
|
map.insert("a".to_string(), Value::U64(1));
|
|
|
|
map.insert("b".to_string(), Value::U64(2));
|
2015-03-05 21:49:17 -06:00
|
|
|
assert_eq!(value, Value::Object(map));
|
|
|
|
}
|