Given a yaml with a map of items,
items:
item1:
uid: ab1234
foo: bar
item2:
uid: cd5678
foo: baz
how can I parse it with serde into a Vec<Item>
with a new field, "name", generated from the key of the original map,
struct Item {
name: String, // to be populated by the key (item1, item2)
uid: String,
foo: String,
}
I'm trying to get the following code (rustexplorer) to work, which currently errors out with Err(Error("items: invalid type: map, expected a sequence", line: 3, column: 3))
.
/*
[dependencies]
serde = "1.0.193"
serde_derive = "1.0.193"
serde_yaml = "0.9.27"
*/
use serde_derive::{Deserialize, Serialize};
use std::vec::Vec;
#[derive(Debug, Serialize, Deserialize)]
struct Item {
name: String,
uid: String,
foo: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Schema {
items: Vec<Item>,
}
fn main() {
let input = r###"
items:
item1:
uid: ab1234
foo: bar
item2:
uid: cd5678
foo: baz
"###;
let items = serde_yaml::from_str::<Schema>(input);
println!("{:?}", items);
}
serde_as
but not sure how. – Icebreakerserde_derive
through the mainserde
crate with the featurederive
and replaceserde_derive
with justserde
– Essive