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
+26
View File
@@ -543,6 +543,7 @@ impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
}
if self.de.eof() {
println!("here3");
return Err(self.de.error(ErrorCode::EOFWhileParsingValue));
}
@@ -573,6 +574,31 @@ impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
Err(self.de.error(ErrorCode::TrailingCharacters))
}
}
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V, Error>
where V: de::Deserialize,
{
// See if the type can deserialize from a unit.
struct UnitDeserializer;
impl de::Deserializer for UnitDeserializer {
type Error = Error;
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
where V: de::Visitor,
{
visitor.visit_unit()
}
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
where V: de::Visitor,
{
visitor.visit_none()
}
}
Ok(try!(de::Deserialize::deserialize(&mut UnitDeserializer)))
}
}
struct EnumVisitor<'a, Iter: 'a> {