You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found the example about this exanple about custom date formatting.
I want to apply exactly the same but with an Option<DateTime<Utc>>.
I found this stackoverflow thread and try to combine one deserialize_with and default attributes but no success:
fndatefmt<'de,D>(deserializer:D) -> Result<DateTime<Utc>,D::Error>whereD:Deserializer<'de>,{let s = String::deserialize(deserializer)?;Utc.datetime_from_str(&s,FORMAT).map_err(serde::de::Error::custom)}structMyStruct{#[serde(default)]#[serde(deserialize_with = "datefmt")]expiration_date:Option<DateTime<Utc>>,}
But I have the following error:
#[derive(Deserialize, Debug)]
| -----------
| |
| expected enum `std::option::Option`, found struct `chrono::datetime::DateTime`
| match arm with an incompatible type
| in this macro invocation
|
= note: expected type `std::option::Option<chrono::datetime::DateTime<_>>`
found type `chrono::datetime::DateTime<_>`
How to handle the case of Option<T> where T has custom serialise/deserialize helpers ?
The text was updated successfully, but these errors were encountered:
This is not well supported right now (but tracked in #723). You need to provide a separate deserialize_with function that produces Option<T>. I would write this as:
I found the example about this exanple about custom date formatting.
I want to apply exactly the same but with an
Option<DateTime<Utc>>
.I found this stackoverflow thread and try to combine one
deserialize_with
anddefault
attributes but no success:But I have the following error:
How to handle the case of
Option<T>
whereT
has custom serialise/deserialize helpers ?The text was updated successfully, but these errors were encountered: