Module nt_time::serde_with::iso_8601::option
source · Available on crate features
serde
and serde-human-readable
only.Expand description
Use the well-known ISO 8601 format when serializing and deserializing an
Option<FileTime>
.
Use this module in combination with Serde’s with
attribute.
If the large-dates
feature is not enabled, the maximum date and time is
“9999-12-31 23:59:59.999999999 UTC”.
Examples
use nt_time::{
serde::{Deserialize, Serialize},
serde_with::iso_8601,
FileTime,
};
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct DateTime(#[serde(with = "iso_8601::option")] Option<FileTime>);
let json = serde_json::to_string(&DateTime(Some(FileTime::UNIX_EPOCH))).unwrap();
assert_eq!(json, r#""+001970-01-01T00:00:00.000000000Z""#);
assert_eq!(
serde_json::from_str::<DateTime>(&json).unwrap(),
DateTime(Some(FileTime::UNIX_EPOCH))
);
let json = serde_json::to_string(&DateTime(None)).unwrap();
assert_eq!(json, "null");
assert_eq!(
serde_json::from_str::<DateTime>(&json).unwrap(),
DateTime(None)
);
Functions
- Deserializes an
Option<FileTime>
from the given Serde deserializer. - Serializes an
Option<FileTime>
into the given Serde serializer.