2015-03-08 13:05:01 -05:00
|
|
|
#![feature(custom_derive, plugin)]
|
2015-02-20 10:21:15 -06:00
|
|
|
#![plugin(serde2_macros)]
|
|
|
|
|
|
|
|
extern crate serde2;
|
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use serde2::json::{self, Value};
|
|
|
|
|
|
|
|
macro_rules! btreemap {
|
|
|
|
() => {
|
|
|
|
BTreeMap::new()
|
|
|
|
};
|
|
|
|
($($key:expr => $value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
$(map.insert($key, $value);)+
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:14:20 -06:00
|
|
|
/*
|
2015-02-20 10:21:15 -06:00
|
|
|
trait Trait {
|
|
|
|
type Type;
|
|
|
|
}
|
2015-02-25 10:14:20 -06:00
|
|
|
*/
|
2015-02-20 10:21:15 -06:00
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
#[derive(Debug, PartialEq)]
|
2015-03-03 22:29:59 -06:00
|
|
|
#[derive_serialize]
|
2015-03-07 23:40:53 -06:00
|
|
|
#[derive_deserialize]
|
2015-03-03 22:29:59 -06:00
|
|
|
struct NamedUnit;
|
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
#[derive(Debug, PartialEq)]
|
2015-02-20 10:21:15 -06:00
|
|
|
#[derive_serialize]
|
|
|
|
//#[derive_deserialize]
|
2015-03-06 21:11:47 -06:00
|
|
|
enum Enum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
2015-02-20 10:21:15 -06:00
|
|
|
Unit,
|
|
|
|
Seq(
|
|
|
|
i8,
|
2015-03-06 21:11:47 -06:00
|
|
|
B,
|
2015-02-20 10:21:15 -06:00
|
|
|
&'a C,
|
2015-03-06 21:11:47 -06:00
|
|
|
//B::Type,
|
|
|
|
&'a mut D,
|
|
|
|
//<D as Trait>::Type,
|
2015-02-20 10:21:15 -06:00
|
|
|
),
|
|
|
|
Map {
|
|
|
|
a: i8,
|
2015-03-06 21:11:47 -06:00
|
|
|
b: B,
|
|
|
|
c: &'a C,
|
|
|
|
//d: C::Type,
|
|
|
|
e: &'a mut D,
|
|
|
|
//f: <D as Trait>::Type,
|
2015-02-20 10:21:15 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:50:57 -06:00
|
|
|
#[test]
|
2015-03-06 21:11:47 -06:00
|
|
|
fn test_named_unit() {
|
|
|
|
let named_unit = NamedUnit;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_unit).unwrap(),
|
|
|
|
"null".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_unit),
|
|
|
|
Value::Null
|
|
|
|
);
|
|
|
|
|
|
|
|
let v = json::from_str("null").unwrap();
|
|
|
|
assert_eq!(v, named_unit);
|
|
|
|
|
|
|
|
let v = json::from_value(Value::Null).unwrap();
|
|
|
|
assert_eq!(v, named_unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-08 13:39:20 -05:00
|
|
|
fn test_ser_named_tuple() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[derive_serialize]
|
|
|
|
struct NamedTuple<'a, 'b, A: 'a, B: 'b, C>(&'a A, &'b mut B, C);
|
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
let a = 5;
|
|
|
|
let mut b = 6;
|
|
|
|
let c = 7;
|
|
|
|
let named_tuple = NamedTuple(&a, &mut b, c);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_tuple).unwrap(),
|
|
|
|
"[5,6,7]"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_tuple),
|
|
|
|
Value::Array(vec![Value::I64(5), Value::I64(6), Value::I64(7)])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 13:39:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_tuple() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct NamedTuple<A, B, C>(A, B, C);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::from_str("[1,2,3]").unwrap(),
|
|
|
|
NamedTuple(1, 2, 3)
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::from_str("[1,2,3]").unwrap(),
|
|
|
|
Value::Array(vec![
|
|
|
|
Value::I64(1),
|
|
|
|
Value::I64(2),
|
|
|
|
Value::I64(3),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:14:13 -06:00
|
|
|
#[test]
|
2015-03-08 20:46:41 -05:00
|
|
|
fn test_ser_named_map() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[derive_serialize]
|
|
|
|
struct NamedMap<'a, 'b, A: 'a, B: 'b, C> {
|
|
|
|
a: &'a A,
|
|
|
|
b: &'b mut B,
|
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:14:13 -06:00
|
|
|
let a = 5;
|
|
|
|
let mut b = 6;
|
|
|
|
let c = 7;
|
|
|
|
let named_map = NamedMap {
|
|
|
|
a: &a,
|
|
|
|
b: &mut b,
|
|
|
|
c: c,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_map).unwrap(),
|
|
|
|
"{\"a\":5,\"b\":6,\"c\":7}"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_map),
|
|
|
|
Value::Object(btreemap![
|
|
|
|
"a".to_string() => Value::I64(5),
|
|
|
|
"b".to_string() => Value::I64(6),
|
|
|
|
"c".to_string() => Value::I64(7)
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 20:46:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_map() {
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
#[derive_deserialize]
|
|
|
|
struct NamedMap<A, B, C> {
|
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
|
|
|
|
let v = NamedMap {
|
|
|
|
a: 5,
|
|
|
|
b: 6,
|
|
|
|
c: 7,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::from_str("{\"a\":5,\"b\":6,\"c\":7}").unwrap(),
|
|
|
|
v
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::from_value(Value::Object(btreemap![
|
|
|
|
"a".to_string() => Value::I64(5),
|
|
|
|
"b".to_string() => Value::I64(6),
|
|
|
|
"c".to_string() => Value::I64(7)
|
|
|
|
])).unwrap(),
|
|
|
|
v
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
#[test]
|
|
|
|
fn test_enum_unit() {
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&Enum::Unit::<u32, u32, u32>).unwrap(),
|
|
|
|
"{\"Unit\":[]}"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&Enum::Unit::<u32, u32, u32>),
|
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Unit".to_string() => Value::Array(vec![]))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_seq() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let mut e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&Enum::Seq(
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
&c,
|
|
|
|
//d,
|
|
|
|
&mut e,
|
|
|
|
//f,
|
|
|
|
)).unwrap(),
|
|
|
|
"{\"Seq\":[1,2,3,5]}".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&Enum::Seq(
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
&c,
|
|
|
|
//d,
|
|
|
|
&mut e,
|
|
|
|
//e,
|
|
|
|
)),
|
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Seq".to_string() => Value::Array(vec![
|
|
|
|
Value::I64(1),
|
|
|
|
Value::I64(2),
|
|
|
|
Value::I64(3),
|
|
|
|
//Value::I64(4),
|
|
|
|
Value::I64(5),
|
|
|
|
//Value::I64(6),
|
|
|
|
])
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_map() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let mut e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&Enum::Map {
|
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: &c,
|
|
|
|
//d: d,
|
|
|
|
e: &mut e,
|
|
|
|
//f: f,
|
|
|
|
}).unwrap(),
|
|
|
|
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&Enum::Map {
|
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: &c,
|
|
|
|
//d: d,
|
|
|
|
e: &mut e,
|
|
|
|
//f: f,
|
|
|
|
}),
|
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Map".to_string() => Value::Object(btreemap![
|
|
|
|
"a".to_string() => Value::I64(1),
|
|
|
|
"b".to_string() => Value::I64(2),
|
|
|
|
"c".to_string() => Value::I64(3),
|
|
|
|
//"d".to_string() => Value::I64(4)
|
|
|
|
"e".to_string() => Value::I64(5)
|
|
|
|
//"f".to_string() => Value::I64(6)
|
|
|
|
])
|
|
|
|
))
|
|
|
|
);
|
2015-03-03 11:50:57 -06:00
|
|
|
}
|