mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-25 16:17:55 +00:00
allow the deserializer to optionally handle missing fields
This allows json to deserialize missing values as a `null`.
This commit is contained in:
+7
-5
@@ -240,11 +240,6 @@ mod deserializer {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
@@ -254,6 +249,13 @@ mod deserializer {
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -236,11 +236,6 @@ mod deserializer {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
@@ -250,6 +245,13 @@ mod deserializer {
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -360,11 +360,6 @@ mod deserializer {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
@@ -374,6 +369,13 @@ mod deserializer {
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-10
@@ -304,11 +304,6 @@ mod deserializer {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
@@ -318,6 +313,13 @@ mod deserializer {
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct U8Deserializer {
|
||||
@@ -374,11 +376,6 @@ mod deserializer {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field_error(&mut self, _field: &'static str) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn unexpected_name_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
@@ -388,6 +385,13 @@ mod deserializer {
|
||||
fn conversion_error(&mut self, _token: de::Token) -> Error {
|
||||
SyntaxError
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, Error> {
|
||||
Err(SyntaxError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+27
-4
@@ -663,8 +663,13 @@ impl de::Deserializer<ParserError> for JsonDeserializer {
|
||||
SyntaxError(InvalidSyntax, 0, 0)
|
||||
}
|
||||
|
||||
fn missing_field_error(&mut self, field: &'static str) -> ParserError {
|
||||
SyntaxError(MissingField(field), 0, 0)
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
de::Deserializable::deserialize_token(self, de::Null)
|
||||
}
|
||||
|
||||
// Special case treating options as a nullable value.
|
||||
@@ -2039,8 +2044,13 @@ impl<T: Iterator<char>> de::Deserializer<ParserError> for Parser<T> {
|
||||
SyntaxError(InvalidSyntax, self.line, self.col)
|
||||
}
|
||||
|
||||
fn missing_field_error(&mut self, field: &'static str) -> ParserError {
|
||||
SyntaxError(MissingField(field), self.line, self.col)
|
||||
#[inline]
|
||||
fn missing_field<
|
||||
T: de::Deserializable
|
||||
>(&mut self, _field: &'static str) -> Result<T, ParserError> {
|
||||
// JSON can represent `null` values as a missing value, so this isn't
|
||||
// necessarily an error.
|
||||
de::Deserializable::deserialize_token(self, de::Null)
|
||||
}
|
||||
|
||||
// Special case treating options as a nullable value.
|
||||
@@ -3066,6 +3076,19 @@ mod tests {
|
||||
("null", None),
|
||||
("\"jodhpurs\"", Some("jodhpurs".to_string())),
|
||||
]);
|
||||
|
||||
#[deriving(PartialEq, Show)]
|
||||
#[deriving_serializable]
|
||||
#[deriving_deserializable]
|
||||
struct Foo {
|
||||
x: Option<int>,
|
||||
}
|
||||
|
||||
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) });
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user