allow the deserializer to optionally handle missing fields

This allows json to deserialize missing values as a `null`.
This commit is contained in:
Erick Tryzelaar
2014-08-18 07:37:44 -07:00
parent c6d28afb6f
commit aff53e8dd4
7 changed files with 80 additions and 70 deletions
+8 -3
View File
@@ -191,7 +191,9 @@ pub trait Deserializer<E>: Iterator<Result<Token, E>> {
/// Called when a `Deserializable` structure did not deserialize a field
/// named `field`.
fn missing_field_error(&mut self, field: &'static str) -> E;
fn missing_field<
T: Deserializable
>(&mut self, field: &'static str) -> Result<T, E>;
/// Called when a deserializable has decided to not consume this token.
fn ignore_field(&mut self, _token: Token) -> Result<(), E> {
@@ -1161,8 +1163,11 @@ mod tests {
SyntaxError
}
fn missing_field_error(&mut self, _field: &'static str) -> Error {
IncompleteValue
#[inline]
fn missing_field<
T: Deserializable
>(&mut self, _field: &'static str) -> Result<T, Error> {
Err(SyntaxError)
}
}