chrono::naive::serde::ts_seconds_option

Function deserialize

Source
pub fn deserialize<'de, D>(d: D) -> Result<Option<NaiveDateTime>, D::Error>
where D: Deserializer<'de>,
Expand description

Deserialize a NaiveDateTime from a second timestamp or none

Intended for use with serdes deserialize_with attribute.

ยงExample:

use chrono::naive::serde::ts_seconds_option::deserialize as from_tsopt;
#[derive(Debug, PartialEq, Deserialize)]
struct S {
    #[serde(deserialize_with = "from_tsopt")]
    time: Option<NaiveDateTime>,
}

let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
let expected = DateTime::from_timestamp(1431684000, 0).unwrap().naive_utc();
assert_eq!(my_s, S { time: Some(expected) });