Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serde with _option macro bug. #429

Merged
merged 2 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ pub mod serde {
where S: ser::Serializer
{
match *opt {
Some(ref dt) => ts_nanoseconds::serialize(dt, serializer),
Some(ref dt) => serializer.serialize_some(&dt.timestamp_nanos()),
None => serializer.serialize_none(),
}
}
Expand Down Expand Up @@ -1475,7 +1475,7 @@ pub mod serde {
where S: ser::Serializer
{
match *opt {
Some(ref dt) => ts_milliseconds::serialize(dt, serializer),
Some(ref dt) => serializer.serialize_some(&dt.timestamp_millis()),
None => serializer.serialize_none(),
}
}
Expand Down Expand Up @@ -1762,7 +1762,7 @@ pub mod serde {
where S: ser::Serializer
{
match *opt {
Some(ref dt) => ts_seconds::serialize(dt, serializer),
Some(ref dt) => serializer.serialize_some(&dt.timestamp()),
None => serializer.serialize_none(),
}
}
Expand Down
32 changes: 29 additions & 3 deletions src/naive/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,7 @@ pub mod serde {
}
}

#[cfg(test)] extern crate serde_derive;
#[cfg(test)] extern crate serde_json;
#[cfg(test)] extern crate bincode;

Expand All @@ -2166,18 +2167,43 @@ pub mod serde {
super::test_decodable_json(|input| self::serde_json::from_str(&input));
}

// Bincode is relevant to test separately from JSON because
// it is not self-describing.
#[test]
fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because
// it is not self-describing.
use naive::NaiveDate;
use self::bincode::{Infinite, serialize, deserialize};
use naive::NaiveDate;

let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90);
let encoded = serialize(&dt, Infinite).unwrap();
let decoded: NaiveDateTime = deserialize(&encoded).unwrap();
assert_eq!(dt, decoded);
}

#[test]
fn test_serde_bincode_optional() {
use ::serde::ts_nanoseconds_option;
use self::serde_derive::{Deserialize, Serialize};
use prelude::*;
use self::bincode::{Infinite, serialize, deserialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Test {
one: Option<i64>,
#[serde(with = "ts_nanoseconds_option")]
two: Option<DateTime<Utc>>,
}

let expected = Test {
one: Some(1),
two: Some(Utc.ymd(1970, 1, 1).and_hms(0, 1, 1)),
};
let bytes: Vec<u8> = serialize(&expected, Infinite).unwrap();
let actual = deserialize::<Test>(&(bytes)).unwrap();

assert_eq!(expected, actual);

}
}

#[cfg(test)]
Expand Down