Skip to content

Instantly share code, notes, and snippets.

@hikilaka
Created January 16, 2020 05:20
Show Gist options
  • Save hikilaka/aa688026c8790b145d10214855c002c3 to your computer and use it in GitHub Desktop.
Save hikilaka/aa688026c8790b145d10214855c002c3 to your computer and use it in GitHub Desktop.
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) }]
}
[
{
"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