Skip to content

Commit

Permalink
Call deserialization methods directly if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed May 1, 2022
1 parent 81b2bed commit f9398b9
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 36 deletions.
48 changes: 40 additions & 8 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ impl Serialize for Date {

impl<'a> Deserialize<'a> for Date {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(2, Visitor::<Self>(PhantomData))
}
}
}
// endregion date
Expand All @@ -118,7 +122,11 @@ impl Serialize for Duration {

impl<'a> Deserialize<'a> for Duration {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(2, Visitor::<Self>(PhantomData))
}
}
}
// endregion Duration
Expand Down Expand Up @@ -161,7 +169,11 @@ impl Serialize for OffsetDateTime {

impl<'a> Deserialize<'a> for OffsetDateTime {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(9, Visitor::<Self>(PhantomData))
}
}
}
// endregion OffsetDateTime
Expand Down Expand Up @@ -199,7 +211,11 @@ impl Serialize for PrimitiveDateTime {

impl<'a> Deserialize<'a> for PrimitiveDateTime {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(6, Visitor::<Self>(PhantomData))
}
}
}
// endregion PrimitiveDateTime
Expand Down Expand Up @@ -233,7 +249,11 @@ impl Serialize for Time {

impl<'a> Deserialize<'a> for Time {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(4, Visitor::<Self>(PhantomData))
}
}
}
// endregion Time
Expand Down Expand Up @@ -270,7 +290,11 @@ impl Serialize for UtcOffset {

impl<'a> Deserialize<'a> for UtcOffset {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_tuple(3, Visitor::<Self>(PhantomData))
}
}
}
// endregion UtcOffset
Expand All @@ -291,7 +315,11 @@ impl Serialize for Weekday {

impl<'a> Deserialize<'a> for Weekday {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_u8(Visitor::<Self>(PhantomData))
}
}
}
// endregion Weekday
Expand All @@ -312,7 +340,11 @@ impl Serialize for Month {

impl<'a> Deserialize<'a> for Month {
fn deserialize<D: Deserializer<'a>>(deserializer: D) -> Result<Self, D::Error> {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
if cfg!(feature = "serde-human-readable") && deserializer.is_human_readable() {
deserializer.deserialize_any(Visitor::<Self>(PhantomData))
} else {
deserializer.deserialize_u8(Visitor::<Self>(PhantomData))
}
}
}
// endregion Month
4 changes: 2 additions & 2 deletions src/serde/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<'a> de::Visitor<'a> for Visitor<Option<well_known::Rfc2822>> {
deserializer: D,
) -> Result<Option<OffsetDateTime>, D::Error> {
deserializer
.deserialize_any(Visitor::<well_known::Rfc2822>(PhantomData))
.deserialize_option(Visitor::<well_known::Rfc2822>(PhantomData))
.map(Some)
}

Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'a> de::Visitor<'a> for Visitor<Option<well_known::Rfc3339>> {
deserializer: D,
) -> Result<Option<OffsetDateTime>, D::Error> {
deserializer
.deserialize_any(Visitor::<well_known::Rfc3339>(PhantomData))
.deserialize_option(Visitor::<well_known::Rfc3339>(PhantomData))
.map(Some)
}

Expand Down
Loading

0 comments on commit f9398b9

Please sign in to comment.