serde: Remove time module
This commit is contained in:
parent
54cf81e9ab
commit
1562cd1d3a
@ -3,6 +3,7 @@
|
||||
Breaking changes:
|
||||
|
||||
* Remove the `empty` module from the public API
|
||||
* Remove the `time` module
|
||||
|
||||
Improvements:
|
||||
|
||||
|
@ -15,7 +15,6 @@ mod raw;
|
||||
pub mod single_element_seq;
|
||||
mod strings;
|
||||
pub mod test;
|
||||
pub mod time;
|
||||
pub mod urlencoded;
|
||||
|
||||
pub use buf::{json_to_buf, slice_to_buf};
|
||||
|
@ -1,6 +0,0 @@
|
||||
//! De-/serialization functions for `std::time::SystemTime` objects
|
||||
|
||||
pub mod ms_since_unix_epoch;
|
||||
pub mod opt_ms_since_unix_epoch;
|
||||
pub mod opt_s_since_unix_epoch;
|
||||
pub mod s_since_unix_epoch;
|
@ -1,87 +0,0 @@
|
||||
//! De-/serialization functions for `std::time::SystemTime` objects represented as milliseconds
|
||||
//! since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
||||
|
||||
use std::{
|
||||
convert::TryInto,
|
||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use js_int::UInt;
|
||||
use serde::{
|
||||
de::{self, Deserialize, Deserializer},
|
||||
ser::{self, Serialize, Serializer},
|
||||
};
|
||||
|
||||
/// Serialize a SystemTime.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn serialize<S>(time: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?;
|
||||
let uint: UInt = time_since_epoch.as_millis().try_into().map_err(ser::Error::custom)?;
|
||||
|
||||
uint.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserializes a SystemTime.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<SystemTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let millis = UInt::deserialize(deserializer)?;
|
||||
UNIX_EPOCH
|
||||
.checked_add(Duration::from_millis(millis.into()))
|
||||
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use matches::assert_matches;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SystemTimeTest {
|
||||
#[serde(with = "super")]
|
||||
timestamp: SystemTime,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize() {
|
||||
let json = json!({ "timestamp": 3000 });
|
||||
|
||||
assert_matches!(
|
||||
serde_json::from_value::<SystemTimeTest>(json),
|
||||
Ok(SystemTimeTest { timestamp })
|
||||
if timestamp == UNIX_EPOCH + Duration::from_millis(3000)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issue_446() {
|
||||
let json = json!({ "timestamp": 15_159_743_990_000_u64 });
|
||||
|
||||
assert_matches!(
|
||||
serde_json::from_value::<SystemTimeTest>(json),
|
||||
Ok(SystemTimeTest { timestamp })
|
||||
if timestamp == UNIX_EPOCH + Duration::from_millis(15_159_743_990_000)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
let request = SystemTimeTest { timestamp: UNIX_EPOCH + Duration::new(2, 0) };
|
||||
assert_matches!(
|
||||
serde_json::to_value(&request),
|
||||
Ok(value) if value == json!({ "timestamp": 2000 })
|
||||
);
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
//! De-/serialization functions for `Option<std::time::SystemTime>` objects represented as
|
||||
//! milliseconds since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within
|
||||
//! bounds.
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use js_int::UInt;
|
||||
use serde::{
|
||||
de::{self, Deserialize, Deserializer},
|
||||
ser::{Serialize, Serializer},
|
||||
};
|
||||
|
||||
/// Serialize an `Option<SystemTime>`.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn serialize<S>(opt_time: &Option<SystemTime>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match opt_time {
|
||||
Some(time) => super::ms_since_unix_epoch::serialize(time, serializer),
|
||||
None => Option::<UInt>::serialize(&None, serializer),
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserializes an `Option<SystemTime>`.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SystemTime>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Option::<UInt>::deserialize(deserializer)?
|
||||
.map(|millis| {
|
||||
UNIX_EPOCH
|
||||
.checked_add(Duration::from_millis(millis.into()))
|
||||
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SystemTimeTest {
|
||||
#[serde(with = "super", default, skip_serializing_if = "Option::is_none")]
|
||||
timestamp: Option<SystemTime>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_some() {
|
||||
let json = json!({ "timestamp": 3000 });
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::from_millis(3000)) },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_none_by_absence() {
|
||||
let json = json!({});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: None },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_none_by_null() {
|
||||
let json = json!({ "timestamp": null });
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: None },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_some() {
|
||||
let request = SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::new(2, 0)) };
|
||||
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_none() {
|
||||
let request = SystemTimeTest { timestamp: None };
|
||||
assert_eq!(serde_json::to_value(&request).unwrap(), json!({}));
|
||||
}
|
||||
}
|
@ -1,98 +0,0 @@
|
||||
//! De-/serialization functions for `Option<std::time::SystemTime>` objects represented as
|
||||
//! seconds since the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within
|
||||
//! bounds.
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use js_int::UInt;
|
||||
use serde::{
|
||||
de::{self, Deserialize, Deserializer},
|
||||
ser::{Serialize, Serializer},
|
||||
};
|
||||
|
||||
/// Serialize an `Option<SystemTime>`.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn serialize<S>(opt_time: &Option<SystemTime>, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
match opt_time {
|
||||
Some(time) => super::s_since_unix_epoch::serialize(time, serializer),
|
||||
None => Option::<UInt>::serialize(&None, serializer),
|
||||
}
|
||||
}
|
||||
|
||||
/// Deserializes an `Option<SystemTime>`.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<SystemTime>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Option::<UInt>::deserialize(deserializer)?
|
||||
.map(|secs| {
|
||||
UNIX_EPOCH
|
||||
.checked_add(Duration::from_secs(secs.into()))
|
||||
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SystemTimeTest {
|
||||
#[serde(with = "super", default, skip_serializing_if = "Option::is_none")]
|
||||
timestamp: Option<SystemTime>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_some() {
|
||||
let json = json!({ "timestamp": 3000 });
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::from_secs(3000)) },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_none_by_absence() {
|
||||
let json = json!({});
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: None },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_none_by_null() {
|
||||
let json = json!({ "timestamp": null });
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: None },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_some() {
|
||||
let request = SystemTimeTest { timestamp: Some(UNIX_EPOCH + Duration::new(2000, 0)) };
|
||||
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize_none() {
|
||||
let request = SystemTimeTest { timestamp: None };
|
||||
assert_eq!(serde_json::to_value(&request).unwrap(), json!({}));
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
//! De-/serialization functions for `std::time::SystemTime` objects represented as seconds since
|
||||
//! the UNIX epoch. Delegates to `js_int::UInt` to ensure integer size is within bounds.
|
||||
|
||||
use std::{
|
||||
convert::TryInto,
|
||||
time::{Duration, SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use js_int::UInt;
|
||||
use serde::{
|
||||
de::{self, Deserialize, Deserializer},
|
||||
ser::{self, Serialize, Serializer},
|
||||
};
|
||||
|
||||
/// Serialize a SystemTime.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn serialize<S>(time: &SystemTime, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let time_since_epoch = time.duration_since(UNIX_EPOCH).map_err(ser::Error::custom)?;
|
||||
let uint: UInt = time_since_epoch.as_secs().try_into().map_err(ser::Error::custom)?;
|
||||
|
||||
uint.serialize(serializer)
|
||||
}
|
||||
|
||||
/// Deserializes a SystemTime.
|
||||
///
|
||||
/// Will fail if integer is greater than the maximum integer that can be unambiguously represented
|
||||
/// by an f64.
|
||||
pub fn deserialize<'de, D>(deserializer: D) -> Result<SystemTime, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let secs = UInt::deserialize(deserializer)?;
|
||||
UNIX_EPOCH
|
||||
.checked_add(Duration::from_secs(secs.into()))
|
||||
.ok_or_else(|| de::Error::custom("input too large for SystemTime"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
||||
struct SystemTimeTest {
|
||||
#[serde(with = "super")]
|
||||
timestamp: SystemTime,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize() {
|
||||
let json = json!({ "timestamp": 3000 });
|
||||
|
||||
assert_eq!(
|
||||
serde_json::from_value::<SystemTimeTest>(json).unwrap(),
|
||||
SystemTimeTest { timestamp: UNIX_EPOCH + Duration::from_secs(3000) },
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serialize() {
|
||||
let request = SystemTimeTest { timestamp: UNIX_EPOCH + Duration::new(2000, 0) };
|
||||
assert_eq!(serde_json::to_value(&request).unwrap(), json!({ "timestamp": 2000 }));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user