Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse nulls as None on nested serde structs #473

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/serde/visitor.rs
Expand Up @@ -299,6 +299,10 @@ macro_rules! well_known {
fn visit_none<E: de::Error>(self) -> Result<Option<OffsetDateTime>, E> {
Ok(None)
}

fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}
}
};
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/serde/mod.rs
Expand Up @@ -5,6 +5,7 @@ use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOf
mod error_conditions;
mod iso8601;
mod macros;
mod rfc2822;
mod rfc3339;
mod timestamps;

Expand Down
55 changes: 55 additions & 0 deletions tests/integration/serde/rfc2822.rs
@@ -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,
})
);
}
19 changes: 18 additions & 1 deletion tests/integration/serde/rfc3339.rs
Expand Up @@ -15,7 +15,7 @@ struct Test {
}

#[test]
fn serialize() {
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)),
Expand Down Expand Up @@ -81,3 +81,20 @@ fn serialize() {
"The offset_second component cannot be formatted into the requested format.",
);
}

#[test]
fn parse_json() {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(untagged)]
enum Wrapper {
A(Test),
}
assert_eq!(
serde_json::from_str::<Wrapper>("{\"dt\": \"2000-01-01T00:00:00Z\", \"option_dt\": null}")
.unwrap(),
Wrapper::A(Test {
dt: datetime!(2000-01-01 00:00:00 UTC),
option_dt: None,
})
);
}