mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-28 12:37:58 +00:00
Fix parsing json into optional types
Does not yet handle parsing missing values as `None` yet though. Closes #25.
This commit is contained in:
@@ -392,6 +392,24 @@ impl<Iter: Iterator<Item=u8>> de::Deserializer for Deserializer<Iter> {
|
|||||||
{
|
{
|
||||||
self.parse_value(visitor)
|
self.parse_value(visitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||||
|
where V: de::Visitor,
|
||||||
|
{
|
||||||
|
self.parse_whitespace();
|
||||||
|
|
||||||
|
if self.eof() {
|
||||||
|
return Err(self.error(ErrorCode::EOFWhileParsingValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.ch_is(b'n') {
|
||||||
|
try!(self.parse_ident(b"ull"));
|
||||||
|
visitor.visit_none()
|
||||||
|
} else {
|
||||||
|
visitor.visit_some(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SeqVisitor<'a, Iter: 'a> {
|
struct SeqVisitor<'a, Iter: 'a> {
|
||||||
|
|||||||
@@ -78,6 +78,18 @@ impl de::Deserialize for Value {
|
|||||||
Ok(Value::String(value))
|
Ok(Value::String(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_none<E>(&mut self) -> Result<Value, E> {
|
||||||
|
Ok(Value::Null)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Value, D::Error>
|
||||||
|
where D: de::Deserializer,
|
||||||
|
{
|
||||||
|
de::Deserialize::deserialize(deserializer)
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
fn visit_seq<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
||||||
where V: de::SeqVisitor,
|
where V: de::SeqVisitor,
|
||||||
|
|||||||
@@ -691,11 +691,10 @@ fn test_parse_struct() {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_option() {
|
fn test_parse_option() {
|
||||||
test_parse_ok(&[
|
test_parse_ok(&[
|
||||||
("null", None),
|
("null", None::<String>),
|
||||||
("\"jodhpurs\"", Some("jodhpurs".to_string())),
|
("\"jodhpurs\"", Some("jodhpurs".to_string())),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -706,21 +705,18 @@ fn test_parse_option() {
|
|||||||
x: Option<isize>,
|
x: Option<isize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
let value: Foo = from_str("{}").unwrap();
|
let value: Foo = from_str("{}").unwrap();
|
||||||
assert_eq!(value, Foo { x: None });
|
assert_eq!(value, Foo { x: None });
|
||||||
|
*/
|
||||||
|
|
||||||
let value: Foo = from_str("{ \"x\": 5 }").unwrap();
|
test_parse_ok(&[
|
||||||
assert_eq!(value, Foo { x: Some(5) });
|
("{\"x\": null}", Foo { x: None }),
|
||||||
}
|
("{\"x\": 5}", Foo { x: Some(5) }),
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_json_deserialize_option() {
|
|
||||||
test_json_deserialize_ok(&[
|
|
||||||
None,
|
|
||||||
Some("jodhpurs".to_string()),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_enum() {
|
fn test_parse_enum() {
|
||||||
test_parse_ok(&[
|
test_parse_ok(&[
|
||||||
|
|||||||
Reference in New Issue
Block a user