Merge pull request #255 from kornelski/master

Support deserializing from other integer types
This commit is contained in:
Alex Crichton 2018-08-06 10:17:30 -05:00 committed by GitHub
commit 48daf82e1a
2 changed files with 29 additions and 0 deletions

View file

@ -423,6 +423,22 @@ impl<'de> de::Deserialize<'de> for Value {
Ok(Value::Integer(value))
}
fn visit_u64<E: de::Error>(self, value: u64) -> Result<Value, E> {
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<E>(self, value: u32) -> Result<Value, E> {
Ok(Value::Integer(value.into()))
}
fn visit_i32<E>(self, value: i32) -> Result<Value, E> {
Ok(Value::Integer(value.into()))
}
fn visit_f64<E>(self, value: f64) -> Result<Value, E> {
Ok(Value::Float(value))
}

View file

@ -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();
}