chrono::naive::serde::ts_microseconds_option

Function serialize

Source
pub fn serialize<S>(
    opt: &Option<NaiveDateTime>,
    serializer: S,
) -> Result<S::Ok, S::Error>
where S: Serializer,
Expand description

Serialize a datetime into an integer number of microseconds since the epoch or none

Intended for use with serdes serialize_with attribute.

ยงExample:

use chrono::naive::serde::ts_microseconds_option::serialize as to_micro_tsopt;
#[derive(Serialize)]
struct S {
    #[serde(serialize_with = "to_micro_tsopt")]
    time: Option<NaiveDateTime>,
}

let my_s = S {
    time: Some(
        NaiveDate::from_ymd_opt(2018, 5, 17)
            .unwrap()
            .and_hms_micro_opt(02, 04, 59, 918355)
            .unwrap(),
    ),
};
let as_string = serde_json::to_string(&my_s)?;
assert_eq!(as_string, r#"{"time":1526522699918355}"#);