Last active
March 20, 2017 18:23
-
-
Save compressed/7538ee517f4f71b7a6746ca4966104a1 to your computer and use it in GitHub Desktop.
serde deserialization error for skipped fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "serde_test" | |
version = "0.1.0" | |
[dependencies] | |
serde = "0.9" | |
serde_json = "0.9" | |
serde_derive = "0.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde_json; | |
extern crate serde; | |
use std::fs::File; | |
use std::io::prelude::*; | |
#[derive(Debug, Deserialize)] | |
struct User { | |
name: String, | |
// don't care about the "fields" field at all | |
// fields: serde_json::Value, | |
} | |
fn main() { | |
let json = File::open("./users.json").unwrap(); | |
let obj = serde_json::from_reader::<File, Vec<User>>(json); | |
println!("{:?}", obj); | |
// fails with Err(Syntax(Message("invalid type: map, expected a sequence"), 1, 2)) | |
// because "fields" goes from a Map to a Vec | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"users":[{"name": "bob", "fields": {"a": "b"}}, {"name": "chris", "fields": []}]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment