From 3c67fdcbf7ab21cb359670c01c6198f584948631 Mon Sep 17 00:00:00 2001 From: Andrei Nesterov Date: Sat, 26 Jan 2019 21:26:39 -0800 Subject: [PATCH] Add Serialize/Deserialize for Option --- src/datetime.rs | 464 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 454 insertions(+), 10 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index 2e553062db..ed023bbb52 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -835,6 +835,18 @@ pub mod serde { use offset::{LocalResult, TimeZone, Utc, FixedOffset}; use serdelib::{ser, de}; + #[doc(hidden)] + #[derive(Debug)] + pub struct SecondsTimestampVisitor; + + #[doc(hidden)] + #[derive(Debug)] + pub struct NanoSecondsTimestampVisitor; + + #[doc(hidden)] + #[derive(Debug)] + pub struct MilliSecondsTimestampVisitor; + // try!-like function to convert a LocalResult into a serde-ish Result fn serde_from(me: LocalResult, ts: &V) -> Result where E: de::Error, @@ -894,7 +906,7 @@ pub mod serde { use {DateTime, Utc}; use offset::TimeZone; - use super::serde_from; + use super::{serde_from, NanoSecondsTimestampVisitor}; /// Serialize a UTC datetime into an integer number of nanoseconds since the epoch /// @@ -968,14 +980,12 @@ pub mod serde { Ok(try!(d.deserialize_i64(NanoSecondsTimestampVisitor))) } - struct NanoSecondsTimestampVisitor; - impl<'de> de::Visitor<'de> for NanoSecondsTimestampVisitor { type Value = DateTime; fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "a unix timestamp in seconds") + write!(formatter, "a unix timestamp in nanoseconds") } /// Deserialize a timestamp in nanoseconds since the epoch @@ -998,6 +1008,152 @@ pub mod serde { } } + /// Ser/de to/from optional timestamps in nanoseconds + /// + /// Intended for use with `serde`'s `with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_nanoseconds_option; + /// #[derive(Deserialize, Serialize)] + /// struct S { + /// #[serde(with = "ts_nanoseconds_option")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let time = Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733)); + /// let my_s = S { + /// time: time.clone(), + /// }; + /// + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918355733}"#); + /// let my_s: S = serde_json::from_str(&as_string)?; + /// assert_eq!(my_s.time, time); + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub mod ts_nanoseconds_option { + use std::fmt; + use serdelib::{ser, de}; + + use {DateTime, Utc}; + + use super::{ts_nanoseconds, NanoSecondsTimestampVisitor}; + + /// Serialize a UTC datetime into an integer number of nanoseconds since the epoch or none + /// + /// Intended for use with `serde`s `serialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_nanoseconds_option::serialize as to_nano_tsopt; + /// #[derive(Serialize)] + /// struct S { + /// #[serde(serialize_with = "to_nano_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s = S { + /// time: Some(Utc.ymd(2018, 5, 17).and_hms_nano(02, 04, 59, 918355733)), + /// }; + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918355733}"#); + /// # Ok(as_string) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn serialize(opt: &Option>, serializer: S) -> Result + where S: ser::Serializer + { + match opt { + Some(dt) => ts_nanoseconds::serialize(dt, serializer), + None => serializer.serialize_none(), + } + } + + /// Deserialize a `DateTime` from a nanosecond timestamp or none + /// + /// Intended for use with `serde`s `deserialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{DateTime, Utc}; + /// use chrono::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt; + /// #[derive(Deserialize)] + /// struct S { + /// #[serde(deserialize_with = "from_nano_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?; + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn deserialize<'de, D>(d: D) -> Result>, D::Error> + where D: de::Deserializer<'de> + { + Ok(try!(d.deserialize_option(OptionNanoSecondsTimestampVisitor))) + } + + struct OptionNanoSecondsTimestampVisitor; + + impl<'de> de::Visitor<'de> for OptionNanoSecondsTimestampVisitor { + type Value = Option>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result + { + formatter.write_str("a unix timestamp in nanoseconds or none") + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_some(self, d: D) -> Result>, D::Error> + where + D: de::Deserializer<'de>, + { + d.deserialize_i64(NanoSecondsTimestampVisitor).map(|val| Some(val)) + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_none(self) -> Result>, E> + where E: de::Error + { + Ok(None) + } + } + } + /// Ser/de to/from timestamps in milliseconds /// /// Intended for use with `serde`s `with` attribute. @@ -1041,7 +1197,7 @@ pub mod serde { use {DateTime, Utc}; use offset::TimeZone; - use super::serde_from; + use super::{serde_from, MilliSecondsTimestampVisitor}; /// Serialize a UTC datetime into an integer number of milliseconds since the epoch /// @@ -1115,8 +1271,6 @@ pub mod serde { Ok(try!(d.deserialize_i64(MilliSecondsTimestampVisitor).map(|dt| dt.with_timezone(&Utc)))) } - struct MilliSecondsTimestampVisitor; - impl<'de> de::Visitor<'de> for MilliSecondsTimestampVisitor { type Value = DateTime; @@ -1145,6 +1299,152 @@ pub mod serde { } } + /// Ser/de to/from optional timestamps in milliseconds + /// + /// Intended for use with `serde`s `with` attribute. + /// + /// # Example + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_milliseconds_option; + /// #[derive(Deserialize, Serialize)] + /// struct S { + /// #[serde(with = "ts_milliseconds_option")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let time = Some(Utc.ymd(2018, 5, 17).and_hms_milli(02, 04, 59, 918)); + /// let my_s = S { + /// time: time.clone(), + /// }; + /// + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918}"#); + /// let my_s: S = serde_json::from_str(&as_string)?; + /// assert_eq!(my_s.time, time); + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub mod ts_milliseconds_option { + use std::fmt; + use serdelib::{ser, de}; + + use {DateTime, Utc}; + + use super::{ts_milliseconds, MilliSecondsTimestampVisitor}; + + /// Serialize a UTC datetime into an integer number of milliseconds since the epoch or none + /// + /// Intended for use with `serde`s `serialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_milliseconds_option::serialize as to_milli_tsopt; + /// #[derive(Serialize)] + /// struct S { + /// #[serde(serialize_with = "to_milli_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s = S { + /// time: Some(Utc.ymd(2018, 5, 17).and_hms_milli(02, 04, 59, 918)), + /// }; + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1526522699918}"#); + /// # Ok(as_string) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn serialize(opt: &Option>, serializer: S) -> Result + where S: ser::Serializer + { + match opt { + Some(dt) => ts_milliseconds::serialize(dt, serializer), + None => serializer.serialize_none(), + } + } + + /// Deserialize a `DateTime` from a millisecond timestamp or none + /// + /// Intended for use with `serde`s `deserialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{DateTime, Utc}; + /// use chrono::serde::ts_milliseconds_option::deserialize as from_milli_tsopt; + /// #[derive(Deserialize)] + /// struct S { + /// #[serde(deserialize_with = "from_milli_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?; + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn deserialize<'de, D>(d: D) -> Result>, D::Error> + where D: de::Deserializer<'de> + { + Ok(try!(d.deserialize_option(OptionMilliSecondsTimestampVisitor).map(|opt| opt.map(|dt| dt.with_timezone(&Utc))))) + } + + struct OptionMilliSecondsTimestampVisitor; + + impl<'de> de::Visitor<'de> for OptionMilliSecondsTimestampVisitor { + type Value = Option>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result + { + formatter.write_str("a unix timestamp in milliseconds or none") + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_some(self, d: D) -> Result>, D::Error> + where + D: de::Deserializer<'de>, + { + d.deserialize_i64(MilliSecondsTimestampVisitor).map(|val| Some(val)) + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_none(self) -> Result>, E> + where E: de::Error + { + Ok(None) + } + } + } + /// Ser/de to/from timestamps in seconds /// /// Intended for use with `serde`'s `with` attribute. @@ -1188,7 +1488,7 @@ pub mod serde { use {DateTime, Utc}; use offset::TimeZone; - use super::serde_from; + use super::{serde_from, SecondsTimestampVisitor}; /// Serialize a UTC datetime into an integer number of seconds since the epoch /// @@ -1262,8 +1562,6 @@ pub mod serde { Ok(try!(d.deserialize_i64(SecondsTimestampVisitor))) } - struct SecondsTimestampVisitor; - impl<'de> de::Visitor<'de> for SecondsTimestampVisitor { type Value = DateTime; @@ -1288,6 +1586,152 @@ pub mod serde { } } + /// Ser/de to/from optional timestamps in seconds + /// + /// Intended for use with `serde`'s `with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_seconds_option; + /// #[derive(Deserialize, Serialize)] + /// struct S { + /// #[serde(with = "ts_seconds_option")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let time = Some(Utc.ymd(2015, 5, 15).and_hms(10, 0, 0)); + /// let my_s = S { + /// time: time.clone(), + /// }; + /// + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1431684000}"#); + /// let my_s: S = serde_json::from_str(&as_string)?; + /// assert_eq!(my_s.time, time); + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub mod ts_seconds_option { + use std::fmt; + use serdelib::{ser, de}; + + use {DateTime, Utc}; + + use super::{ts_seconds, SecondsTimestampVisitor}; + + /// Serialize a UTC datetime into an integer number of seconds since the epoch or none + /// + /// Intended for use with `serde`s `serialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{TimeZone, DateTime, Utc}; + /// use chrono::serde::ts_seconds_option::serialize as to_tsopt; + /// #[derive(Serialize)] + /// struct S { + /// #[serde(serialize_with = "to_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s = S { + /// time: Some(Utc.ymd(2015, 5, 15).and_hms(10, 0, 0)), + /// }; + /// let as_string = serde_json::to_string(&my_s)?; + /// assert_eq!(as_string, r#"{"time":1431684000}"#); + /// # Ok(as_string) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn serialize(opt: &Option>, serializer: S) -> Result + where S: ser::Serializer + { + match opt { + Some(dt) => ts_seconds::serialize(dt, serializer), + None => serializer.serialize_none(), + } + } + + /// Deserialize a `DateTime` from a seconds timestamp or none + /// + /// Intended for use with `serde`s `deserialize_with` attribute. + /// + /// # Example: + /// + /// ```rust + /// # // We mark this ignored so that we can test on 1.13 (which does not + /// # // support custom derive), and run tests with --ignored on beta and + /// # // nightly to actually trigger these. + /// # + /// # #[macro_use] extern crate serde_derive; + /// # #[macro_use] extern crate serde_json; + /// # extern crate chrono; + /// # use chrono::{DateTime, Utc}; + /// use chrono::serde::ts_seconds_option::deserialize as from_tsopt; + /// #[derive(Deserialize)] + /// struct S { + /// #[serde(deserialize_with = "from_tsopt")] + /// time: Option> + /// } + /// + /// # fn example() -> Result { + /// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?; + /// # Ok(my_s) + /// # } + /// # fn main() { example().unwrap(); } + /// ``` + pub fn deserialize<'de, D>(d: D) -> Result>, D::Error> + where D: de::Deserializer<'de> + { + Ok(try!(d.deserialize_option(OptionSecondsTimestampVisitor))) + } + + struct OptionSecondsTimestampVisitor; + + impl<'de> de::Visitor<'de> for OptionSecondsTimestampVisitor { + type Value = Option>; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result + { + formatter.write_str("a unix timestamp in seconds or none") + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_some(self, d: D) -> Result>, D::Error> + where + D: de::Deserializer<'de>, + { + d.deserialize_i64(SecondsTimestampVisitor).map(|val| Some(val)) + } + + /// Deserialize a timestamp in seconds since the epoch + fn visit_none(self) -> Result>, E> + where E: de::Error + { + Ok(None) + } + } + } + impl ser::Serialize for DateTime { /// Serialize into a rfc3339 time string ///