Allow json missing fields to be treated as a null

Closes #34.
This commit is contained in:
Erick Tryzelaar
2015-03-12 20:44:16 -07:00
parent 5dd53e7ea3
commit dc87288f48
5 changed files with 82 additions and 4 deletions
+24 -1
View File
@@ -643,7 +643,7 @@ fn test_parse_object() {
fn test_parse_struct() {
test_parse_err::<Outer>(&[
("[]", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
("{}", Error::MissingFieldError("inner")),
("{}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
("{\"inner\": true}", Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)),
]);
@@ -759,3 +759,26 @@ fn test_multiline_errors() {
("{\n \"foo\":\n \"bar\"", Error::SyntaxError(ErrorCode::EOFWhileParsingObject, 3, 8)),
]);
}
#[test]
fn test_missing_field() {
#[derive(Debug, PartialEq)]
#[derive_deserialize]
struct Foo {
x: Option<u32>,
}
let value: Foo = from_str("{}").unwrap();
assert_eq!(value, Foo { x: None });
let value: Foo = from_str("{\"x\": 5}").unwrap();
assert_eq!(value, Foo { x: Some(5) });
let value: Foo = from_value(Value::Object(treemap!())).unwrap();
assert_eq!(value, Foo { x: None });
let value: Foo = from_value(Value::Object(treemap!(
"x".to_string() => Value::I64(5)
))).unwrap();
assert_eq!(value, Foo { x: Some(5) });
}