-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse nulls as None on nested serde structs (#473)
When parsing a structure nested inside an untagged enum serde treats keys with a null value as Unit instead of calling visit_none. This causes an error of "invalid type: null, expected an RFC3339-formatted `Option<OffsetDateTime>`". This change just adds an implementation for visit_unit so the value will be parsed as None as expected.
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use serde_test::{assert_tokens, Configure, Token}; | ||
use time::serde::rfc2822; | ||
use time::OffsetDateTime; | ||
use time_macros::datetime; | ||
|
||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] | ||
struct Test { | ||
#[serde(with = "rfc2822")] | ||
dt: OffsetDateTime, | ||
#[serde(with = "rfc2822::option")] | ||
option_dt: Option<OffsetDateTime>, | ||
} | ||
|
||
#[test] | ||
fn serialize_deserialize() { | ||
let value = Test { | ||
dt: datetime!(2000-01-01 00:00:00 UTC), | ||
option_dt: Some(datetime!(2000-01-01 00:00:00 UTC)), | ||
}; | ||
assert_tokens( | ||
&value.compact(), | ||
&[ | ||
Token::Struct { | ||
name: "Test", | ||
len: 2, | ||
}, | ||
Token::Str("dt"), | ||
Token::BorrowedStr("Sat, 01 Jan 2000 00:00:00 +0000"), | ||
Token::Str("option_dt"), | ||
Token::Some, | ||
Token::BorrowedStr("Sat, 01 Jan 2000 00:00:00 +0000"), | ||
Token::StructEnd, | ||
], | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_json() { | ||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] | ||
#[serde(untagged)] | ||
enum Wrapper { | ||
A(Test), | ||
} | ||
assert_eq!( | ||
serde_json::from_str::<Wrapper>( | ||
r#"{"dt": "Sat, 01 Jan 2000 00:00:00 +0000", "option_dt": null}"# | ||
) | ||
.unwrap(), | ||
Wrapper::A(Test { | ||
dt: datetime!(2000-01-01 00:00:00 UTC), | ||
option_dt: None, | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters