Merge pull request #89 from dtolnay/bincode

Support deserializing from bincode
This commit is contained in:
Kang Seonghoon 2016-08-17 09:45:45 +09:00 committed by GitHub
commit 6cc5d18696
5 changed files with 65 additions and 6 deletions

View File

@ -22,3 +22,4 @@ serde = { version = "<0.9", optional = true }
[dev-dependencies]
serde_json = { version = ">=0.7.0" }
bincode = { version = "0.6", features = ["serde"], default-features = false }

View File

@ -496,7 +496,7 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(DateTimeVisitor)
deserializer.deserialize_str(DateTimeVisitor)
}
}
@ -504,7 +504,7 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC))
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&UTC))
}
}
@ -512,11 +512,12 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(DateTimeVisitor).map(|dt| dt.with_timezone(&Local))
deserializer.deserialize_str(DateTimeVisitor).map(|dt| dt.with_timezone(&Local))
}
}
#[cfg(test)] extern crate serde_json;
#[cfg(test)] extern crate bincode;
#[test]
fn test_serde_serialize() {
@ -537,6 +538,20 @@ mod serde {
assert!(from_str(r#""2014-07-32T12:34:06Z""#).is_err());
}
#[test]
fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because
// it is not self-describing.
use self::bincode::SizeLimit;
use self::bincode::serde::{serialize, deserialize};
let dt = UTC.ymd(2014, 7, 24).and_hms(12, 34, 6);
let encoded = serialize(&dt, SizeLimit::Infinite).unwrap();
let decoded: DateTime<UTC> = deserialize(&encoded).unwrap();
assert_eq!(dt, decoded);
assert_eq!(dt.offset(), decoded.offset());
}
}
#[cfg(test)]

View File

@ -1591,11 +1591,12 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(NaiveDateVisitor)
deserializer.deserialize_str(NaiveDateVisitor)
}
}
#[cfg(test)] extern crate serde_json;
#[cfg(test)] extern crate bincode;
#[test]
fn test_serde_serialize() {
@ -1647,6 +1648,19 @@ mod serde {
assert!(from_str(r#"{"ymdf":20}"#).is_err()); // :(
assert!(from_str(r#"null"#).is_err());
}
#[test]
fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because
// it is not self-describing.
use self::bincode::SizeLimit;
use self::bincode::serde::{serialize, deserialize};
let d = NaiveDate::from_ymd(2014, 7, 24);
let encoded = serialize(&d, SizeLimit::Infinite).unwrap();
let decoded: NaiveDate = deserialize(&encoded).unwrap();
assert_eq!(d, decoded);
}
}
#[cfg(test)]

View File

@ -1442,11 +1442,12 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(NaiveDateTimeVisitor)
deserializer.deserialize_str(NaiveDateTimeVisitor)
}
}
#[cfg(test)] extern crate serde_json;
#[cfg(test)] extern crate bincode;
#[test]
fn test_serde_serialize() {
@ -1529,6 +1530,20 @@ mod serde {
assert!(from_str(r#"{"date":{"ymdf":20},"time":{"secs":0,"frac":0}}"#).is_err()); // :(
assert!(from_str(r#"null"#).is_err());
}
#[test]
fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because
// it is not self-describing.
use naive::date::NaiveDate;
use self::bincode::SizeLimit;
use self::bincode::serde::{serialize, deserialize};
let dt = NaiveDate::from_ymd(2016, 7, 8).and_hms_milli(9, 10, 48, 90);
let encoded = serialize(&dt, SizeLimit::Infinite).unwrap();
let decoded: NaiveDateTime = deserialize(&encoded).unwrap();
assert_eq!(dt, decoded);
}
}
#[cfg(test)]

View File

@ -1339,11 +1339,12 @@ mod serde {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: de::Deserializer
{
deserializer.deserialize(NaiveTimeVisitor)
deserializer.deserialize_str(NaiveTimeVisitor)
}
}
#[cfg(test)] extern crate serde_json;
#[cfg(test)] extern crate bincode;
#[test]
fn test_serde_serialize() {
@ -1411,6 +1412,19 @@ mod serde {
assert!(from_str(r#"{"secs":0,"frac":0}"#).is_err()); // :(
assert!(from_str(r#"null"#).is_err());
}
#[test]
fn test_serde_bincode() {
// Bincode is relevant to test separately from JSON because
// it is not self-describing.
use self::bincode::SizeLimit;
use self::bincode::serde::{serialize, deserialize};
let t = NaiveTime::from_hms_nano(3, 5, 7, 98765432);
let encoded = serialize(&t, SizeLimit::Infinite).unwrap();
let decoded: NaiveTime = deserialize(&encoded).unwrap();
assert_eq!(t, decoded);
}
}
#[cfg(test)]