Created
January 16, 2020 05:20
-
-
Save hikilaka/aa688026c8790b145d10214855c002c3 to your computer and use it in GitHub Desktop.
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
use serde::{Deserialize, Serialize}; | |
#[derive(Serialize, Deserialize, Debug)] | |
#[serde(untagged)] | |
enum Stuff { | |
Array(Vec<u8>), | |
Number(u8), | |
} | |
#[derive(Serialize, Deserialize, Debug)] | |
struct Object { | |
name: String, | |
stuff: Stuff, | |
} | |
fn main() { | |
let objs: Vec<Object> = serde_json::from_str(&include_str!("../objects.json")).unwrap(); | |
println!("{:?}", objs); | |
// output: | |
//[Object { name: "quack", stuff: Array([1, 2, 3]) }, Object { name: "quack2", stuff: Number(50) }] | |
} |
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
[ | |
{ | |
"name": "quack", | |
"stuff": [ | |
1, | |
2, | |
3 | |
] | |
}, | |
{ | |
"name": "quack2", | |
"stuff": 50 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment