When trying to serialize Option<chrono::DateTime<Utc>>
I'm encountering an error:
error[E0308]: mismatched types
--> src/main.rs:39:14
|
39 | #[derive(Serialize, Debug)]
| ^^^^^^^^^ expected struct `DateTime`, found enum `std::option::Option`
|
= note: expected reference `&DateTime<Utc>`
found reference `&'__a std::option::Option<DateTime<Utc>>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Code (Playground):
use chrono::{serde::ts_seconds, DateTime, NaiveDate, Utc};
use serde::Serialize;
fn main() {
let test_struct = TestStruct {
a: 2.45,
date: Some(DateTime::from_utc(
NaiveDate::from_ymd(2000, 1, 1).and_hms(1, 1, 1),
Utc,
)),
};
let string = serde_json::to_string(&test_struct).unwrap();
println!("default: {}", string);
#[derive(Serialize, Debug)]
struct TestStruct {
pub a: f32,
#[serde(with = "ts_seconds")]
pub date: Option<DateTime<Utc>>,
}
}
Looking at chrono::ts_seconds
and serde_with I have no idea where to move forward here.
I would really appreciate any help with this.
Chrono already has a function for Option<DateTime<Utc>>
, namely chrono::serde::ts_seconds_option
.
#[derive(Serialize, Debug)]
struct TestStruct {
pub a: f32,
#[serde(with = "ts_seconds_option")]
pub date: Option<DateTime<Utc>>,
}
The solution with serde_with
looks like this:
#[serde_as]
#[derive(Serialize, Debug)]
struct TestStruct {
pub a: f32,
#[serde_as(as = "Option<DurationSeconds<i64>>")]
pub date: Option<DateTime<Utc>>,
}
You can write your own wrapper and combine it with serialize_with
and skip_serializing_if
:
pub fn serialize_dt<S>(
dt: &Option<DateTime<Utc>>,
serializer: S
) -> Result<S::Ok, S::Error>
where
S: Serializer {
match dt {
Some(dt) => ts_seconds::serialize(dt, serializer),
_ => unreachable!(),
}
}
#[derive(Serialize, Debug)]
struct TestStruct {
pub a: f32,
#[serde(serialize_with = "serialize_dt", skip_serializing_if = "Option::is_none")]
pub date: Option<DateTime<Utc>>,
}
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With