尝试对
代码(Playground):
看着
Option<chrono::DateTime<Utc>>
进行序列化时,遇到了一个错误: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)
代码(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>>,
}
}
看着
chrono::ts_seconds
和 serde_with。我该怎么办?
DateTime<Utc>
而不是Unix时间戳。 - simanacci