From 985e21003f9b9594764b59ae941169a2dad6eb19 Mon Sep 17 00:00:00 2001 From: Kornel Date: Sun, 5 Aug 2018 23:25:06 +0100 Subject: [PATCH 1/2] Support deserializing from other integer types --- src/value.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/value.rs b/src/value.rs index 21a8205..64f4555 100644 --- a/src/value.rs +++ b/src/value.rs @@ -423,6 +423,22 @@ impl<'de> de::Deserialize<'de> for Value { Ok(Value::Integer(value)) } + fn visit_u64(self, value: u64) -> Result { + if value <= i64::max_value() as u64 { + Ok(Value::Integer(value as i64)) + } else { + Err(de::Error::custom("u64 value was too large")) + } + } + + fn visit_u32(self, value: u32) -> Result { + Ok(Value::Integer(value.into())) + } + + fn visit_i32(self, value: i32) -> Result { + Ok(Value::Integer(value.into())) + } + fn visit_f64(self, value: f64) -> Result { Ok(Value::Float(value)) } From 14338b855684ec124d6c8dc8ecb1c83471764338 Mon Sep 17 00:00:00 2001 From: Kornel Date: Mon, 6 Aug 2018 15:23:32 +0100 Subject: [PATCH 2/2] Test serde_json interoperability --- test-suite/tests/serde.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test-suite/tests/serde.rs b/test-suite/tests/serde.rs index 446cd7a..b97937f 100644 --- a/test-suite/tests/serde.rs +++ b/test-suite/tests/serde.rs @@ -1,4 +1,5 @@ extern crate serde; +extern crate serde_json; extern crate toml; #[macro_use] extern crate serde_derive; @@ -589,3 +590,15 @@ fn fixed_size_array() { assert_eq!(value, expected); assert_eq!(toml::to_string(&value).unwrap(), text); } + +#[test] +fn json_interoperability() { + #[derive(Serialize, Deserialize)] + struct Foo { + any: toml::Value + } + + let _foo: Foo = serde_json::from_str(r#" + {"any":1} + "#).unwrap(); +}