-
Notifications
You must be signed in to change notification settings - Fork 610
Closed
Description
use serde_json::value::{Value,RawValue};
use serde::Deserialize;
use std::collections::HashMap;
#[derive(Deserialize, Debug)]
struct TestA<'a> {
a: i32,
#[serde(borrow)]
#[serde(flatten)]
rest: HashMap::<&'a str, &'a RawValue>,
}
#[derive(Deserialize, Debug)]
struct TestB<'a> {
#[serde(borrow)]
#[serde(flatten)]
rest: HashMap::<&'a str, &'a RawValue>,
}
#[derive(Deserialize, Debug)]
struct TestC<'a> {
#[serde(borrow)]
#[serde(flatten)]
rest: HashMap::<&'a str, Value>,
}
fn main() {
let s = r#"{
"a": 42,
"b": "foo"
}"#;
let r = serde_json::from_str::<TestA>(s);
println!("{:?}", r);
let r = serde_json::from_str::<TestB>(s);
println!("{:?}", r);
let r = serde_json::from_str::<TestC>(s);
println!("{:?}", r);
}
Output
Err(Error("invalid type: newtype struct, expected any valid JSON value", line: 4, column: 5))
Err(Error("invalid type: newtype struct, expected any valid JSON value", line: 4, column: 5))
Ok(TestC { rest: {"a": Number(42), "b": String("foo")} })
Expected behaviour
s is deserialized to all three structs successfully
Relevant Rust Playground
Versions in use:
- latest stable rustc 1.40.0
- serde_json: 1.0.44
- serde: 1.0.104
AtsukiTak, arnauorriols, saskenuba, kevinpark1217, benluelo and 6 more
Metadata
Metadata
Assignees
Labels
No labels