serde_with::chrono::naive::serde::ts_milliseconds_option

Function deserialize

source
pub fn deserialize<'de, D>(
    d: D,
) -> Result<Option<NaiveDateTime>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,
Available on crate feature chrono only.
Expand description

Deserialize a NaiveDateTime from a millisecond timestamp or none

Intended for use with serdes deserialize_with attribute.

ยงExample:

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

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

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