nt_time::serde_with::unix_time::milliseconds

Module option

Source
Available on crate feature serde only.
Expand description

Use Unix time in milliseconds when serializing and deserializing an Option<FileTime>.

Use this module in combination with Serde’s with attribute.

§Examples

use nt_time::{
    serde::{Deserialize, Serialize},
    serde_with::unix_time,
    FileTime,
};

#[derive(Deserialize, Serialize)]
struct Time {
    #[serde(with = "unix_time::milliseconds::option")]
    time: Option<FileTime>,
}

let ft = Time {
    time: Some(FileTime::NT_TIME_EPOCH),
};
let json = serde_json::to_string(&ft).unwrap();
assert_eq!(json, r#"{"time":-11644473600000}"#);

let ft: Time = serde_json::from_str(&json).unwrap();
assert_eq!(ft.time, Some(FileTime::NT_TIME_EPOCH));

let ft = Time { time: None };
let json = serde_json::to_string(&ft).unwrap();
assert_eq!(json, r#"{"time":null}"#);

let ft: Time = serde_json::from_str(&json).unwrap();
assert_eq!(ft.time, None);

Functions§