2015-07-23 10:07:49 -05:00
|
|
|
use std::default;
|
2015-08-27 22:01:09 -05:00
|
|
|
|
2016-01-10 21:34:48 -06:00
|
|
|
use token::{
|
|
|
|
Error,
|
|
|
|
Token,
|
|
|
|
assert_tokens,
|
|
|
|
assert_ser_tokens,
|
|
|
|
assert_de_tokens,
|
|
|
|
assert_de_tokens_error
|
|
|
|
};
|
2015-04-02 00:14:28 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
struct Default {
|
|
|
|
a1: i32,
|
|
|
|
#[serde(default)]
|
|
|
|
a2: i32,
|
|
|
|
}
|
|
|
|
|
2016-01-10 21:34:48 -06:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-01-11 12:20:07 -06:00
|
|
|
#[serde(deny_unknown_fields)]
|
2016-01-10 21:34:48 -06:00
|
|
|
struct DisallowUnknown {
|
|
|
|
a1: i32,
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:14:28 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-02-08 10:00:38 -06:00
|
|
|
#[serde(rename="Superhero")]
|
|
|
|
struct RenameStruct {
|
2015-04-02 00:14:28 -05:00
|
|
|
a1: i32,
|
|
|
|
#[serde(rename="a3")]
|
|
|
|
a2: i32,
|
|
|
|
}
|
|
|
|
|
2016-02-08 10:03:46 -06:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-02-08 10:00:38 -06:00
|
|
|
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
|
|
|
|
struct RenameStructSerializeDeserialize {
|
2016-02-08 12:35:42 -06:00
|
|
|
a1: i32,
|
|
|
|
#[serde(rename(serialize="a4", deserialize="a5"))]
|
|
|
|
a2: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-02-08 10:00:38 -06:00
|
|
|
#[serde(rename="Superhero")]
|
2016-02-08 12:35:42 -06:00
|
|
|
enum RenameEnum {
|
|
|
|
#[serde(rename="bruce_wayne")]
|
|
|
|
Batman,
|
2016-02-08 10:00:38 -06:00
|
|
|
#[serde(rename="clark_kent")]
|
|
|
|
Superman(i8),
|
|
|
|
#[serde(rename="diana_prince")]
|
|
|
|
WonderWoman(i8, i8),
|
|
|
|
#[serde(rename="barry_allan")]
|
|
|
|
Flash {
|
|
|
|
#[serde(rename="b")]
|
|
|
|
a: i32,
|
|
|
|
},
|
2016-02-08 12:35:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
2016-02-08 10:00:38 -06:00
|
|
|
#[serde(rename(serialize="SuperheroSer", deserialize="SuperheroDe"))]
|
|
|
|
enum RenameEnumSerializeDeserialize<A> {
|
|
|
|
#[serde(rename(serialize="dick_grayson", deserialize="jason_todd"))]
|
|
|
|
Robin {
|
2016-02-08 12:35:42 -06:00
|
|
|
a: i8,
|
|
|
|
#[serde(rename(serialize="c", deserialize="d"))]
|
|
|
|
b: A,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:07:49 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
struct SkipSerializingFields<A: default::Default> {
|
|
|
|
a: i8,
|
|
|
|
#[serde(skip_serializing, default)]
|
|
|
|
b: A,
|
|
|
|
}
|
2015-05-01 11:53:59 -05:00
|
|
|
|
2015-09-07 18:44:56 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
struct SkipSerializingIfEmptyFields<A: default::Default> {
|
|
|
|
a: i8,
|
|
|
|
#[serde(skip_serializing_if_empty, default)]
|
|
|
|
b: Vec<A>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
struct SkipSerializingIfNoneFields<A: default::Default> {
|
|
|
|
a: i8,
|
|
|
|
#[serde(skip_serializing_if_none, default)]
|
|
|
|
b: Option<A>,
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:14:28 -05:00
|
|
|
#[test]
|
|
|
|
fn test_default() {
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_de_tokens(
|
|
|
|
&Default { a1: 1, a2: 2 },
|
|
|
|
vec![
|
|
|
|
Token::StructStart("Default", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a2"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Default { a1: 1, a2: 0 },
|
|
|
|
vec![
|
|
|
|
Token::StructStart("Default", Some(1)),
|
2015-04-02 00:14:28 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
2015-04-02 00:14:28 -05:00
|
|
|
}
|
|
|
|
|
2016-01-10 21:34:48 -06:00
|
|
|
#[test]
|
|
|
|
fn test_ignore_unknown() {
|
|
|
|
// 'Default' allows unknown. Basic smoke test of ignore...
|
|
|
|
assert_de_tokens(
|
|
|
|
&Default { a1: 1, a2: 2},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("Default", Some(5)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("whoops1"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("whoops2"),
|
|
|
|
Token::SeqStart(Some(1)),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a2"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("whoops3"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<DisallowUnknown>(
|
|
|
|
vec![
|
|
|
|
Token::StructStart("DisallowUnknown", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("whoops"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
Error::UnknownFieldError("whoops".to_owned())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 00:14:28 -05:00
|
|
|
#[test]
|
2016-02-08 10:00:38 -06:00
|
|
|
fn test_rename_struct() {
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_tokens(
|
2016-02-08 10:00:38 -06:00
|
|
|
&RenameStruct { a1: 1, a2: 2 },
|
2015-08-27 22:01:09 -05:00
|
|
|
vec![
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::StructStart("Superhero", Some(2)),
|
2015-08-27 22:01:09 -05:00
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
2015-04-02 00:14:28 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a3"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
2015-04-02 00:14:28 -05:00
|
|
|
}
|
2015-04-21 09:57:36 -05:00
|
|
|
|
2016-02-08 10:03:46 -06:00
|
|
|
#[test]
|
2016-02-08 10:00:38 -06:00
|
|
|
fn test_rename_struct_serialize_deserialize() {
|
2016-02-08 12:35:42 -06:00
|
|
|
assert_ser_tokens(
|
2016-02-08 10:00:38 -06:00
|
|
|
&RenameStructSerializeDeserialize { a1: 1, a2: 2 },
|
2016-02-08 12:35:42 -06:00
|
|
|
&[
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::StructStart("SuperheroSer", Some(2)),
|
2016-02-08 12:35:42 -06:00
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a4"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2016-02-08 10:00:38 -06:00
|
|
|
&RenameStructSerializeDeserialize { a1: 1, a2: 2 },
|
2016-02-08 12:35:42 -06:00
|
|
|
vec![
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::StructStart("SuperheroDe", Some(2)),
|
2016-02-08 12:35:42 -06:00
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a1"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a5"),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2016-02-08 10:00:38 -06:00
|
|
|
fn test_rename_enum() {
|
|
|
|
assert_tokens(
|
|
|
|
&RenameEnum::Batman,
|
|
|
|
vec![
|
|
|
|
Token::EnumUnit("Superhero", "bruce_wayne"),
|
2016-02-08 12:35:42 -06:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2016-02-08 10:00:38 -06:00
|
|
|
assert_tokens(
|
|
|
|
&RenameEnum::Superman(0),
|
2016-02-08 10:03:46 -06:00
|
|
|
vec![
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::EnumNewtype("Superhero", "clark_kent"),
|
|
|
|
Token::I8(0),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&RenameEnum::WonderWoman(0, 1),
|
|
|
|
vec![
|
|
|
|
Token::EnumSeqStart("Superhero", "diana_prince", Some(2)),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I8(0),
|
|
|
|
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::SeqEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&RenameEnum::Flash { a: 1 },
|
|
|
|
vec![
|
|
|
|
Token::EnumMapStart("Superhero", "barry_allan", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
2016-02-08 12:35:42 -06:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_serialize_deserialize() {
|
|
|
|
assert_ser_tokens(
|
2016-02-08 10:00:38 -06:00
|
|
|
&RenameEnumSerializeDeserialize::Robin {
|
2016-02-08 12:35:42 -06:00
|
|
|
a: 0,
|
|
|
|
b: String::new(),
|
|
|
|
},
|
|
|
|
&[
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::EnumMapStart("SuperheroSer", "dick_grayson", Some(2)),
|
2016-02-08 12:35:42 -06:00
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(0),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::Str(""),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2016-02-08 10:00:38 -06:00
|
|
|
&RenameEnumSerializeDeserialize::Robin {
|
2016-02-08 12:35:42 -06:00
|
|
|
a: 0,
|
|
|
|
b: String::new(),
|
|
|
|
},
|
|
|
|
vec![
|
2016-02-08 10:00:38 -06:00
|
|
|
Token::EnumMapStart("SuperheroDe", "jason_todd", Some(2)),
|
2016-02-08 12:35:42 -06:00
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(0),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("d"),
|
|
|
|
Token::Str(""),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
2016-02-08 10:03:46 -06:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-23 10:07:49 -05:00
|
|
|
#[test]
|
|
|
|
fn test_skip_serializing_fields() {
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&SkipSerializingFields {
|
|
|
|
a: 1,
|
|
|
|
b: 2,
|
|
|
|
},
|
|
|
|
&[
|
|
|
|
Token::StructStart("SkipSerializingFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&SkipSerializingFields {
|
|
|
|
a: 1,
|
|
|
|
b: 0,
|
|
|
|
},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("SkipSerializingFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
2015-07-23 10:07:49 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
2015-07-23 10:07:49 -05:00
|
|
|
}
|
2015-09-07 18:44:56 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_skip_serializing_fields_if_empty() {
|
|
|
|
assert_ser_tokens(
|
|
|
|
&SkipSerializingIfEmptyFields::<i32> {
|
|
|
|
a: 1,
|
|
|
|
b: vec![],
|
|
|
|
},
|
|
|
|
&[
|
|
|
|
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&SkipSerializingIfEmptyFields::<i32> {
|
|
|
|
a: 1,
|
|
|
|
b: vec![],
|
|
|
|
},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("SkipSerializingIfEmptyFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_ser_tokens(
|
|
|
|
&SkipSerializingIfEmptyFields {
|
|
|
|
a: 1,
|
|
|
|
b: vec![2],
|
|
|
|
},
|
|
|
|
&[
|
|
|
|
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::SeqStart(Some(1)),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&SkipSerializingIfEmptyFields {
|
|
|
|
a: 1,
|
|
|
|
b: vec![2],
|
|
|
|
},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("SkipSerializingIfEmptyFields", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::SeqStart(Some(1)),
|
|
|
|
Token::SeqSep,
|
|
|
|
Token::I32(2),
|
|
|
|
Token::SeqEnd,
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_skip_serializing_fields_if_none() {
|
|
|
|
assert_ser_tokens(
|
|
|
|
&SkipSerializingIfNoneFields::<i32> {
|
|
|
|
a: 1,
|
|
|
|
b: None,
|
|
|
|
},
|
|
|
|
&[
|
|
|
|
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&SkipSerializingIfNoneFields::<i32> {
|
|
|
|
a: 1,
|
|
|
|
b: None,
|
|
|
|
},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("SkipSerializingIfNoneFields", Some(1)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_ser_tokens(
|
|
|
|
&SkipSerializingIfNoneFields {
|
|
|
|
a: 1,
|
|
|
|
b: Some(2),
|
|
|
|
},
|
|
|
|
&[
|
|
|
|
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::Option(true),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&SkipSerializingIfNoneFields {
|
|
|
|
a: 1,
|
|
|
|
b: Some(2),
|
|
|
|
},
|
|
|
|
vec![
|
|
|
|
Token::StructStart("SkipSerializingIfNoneFields", Some(2)),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
|
|
|
|
Token::MapSep,
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::Option(true),
|
|
|
|
Token::I32(2),
|
|
|
|
|
|
|
|
Token::MapEnd,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|