You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.2 KiB

use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use serde_with::serde_as;
#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize)]
struct Path(pub Vec<u32>);
#[derive(Serialize, Deserialize, Debug)]
struct Rel {
path: Path,
count: usize,
}
#[serde_as]
#[derive(Serialize, Deserialize)]
struct Cl1 {
#[serde_as(as = "Vec<(_, _)>")]
cache: HashMap<Vec<u32>, String>,
}
type RelCache = HashMap<Path, Rel>;
#[serde_as]
#[derive(Serialize, Deserialize, Debug)]
struct Cl2 {
#[serde_as(as = "Vec<(_, _)>")]
cache: RelCache,
}
fn main() {
let mut cache = HashMap::new();
cache.insert(vec![1, 2, 3], "Erster Eintrag".to_owned());
cache.insert(vec![4, 5, 6], "Zweiter Eintrag".to_owned());
let cl = Cl1 {
cache
};
let json = serde_json::to_string(&cl);
dbg!(json);
let path1 = Path(vec![1, 2, 3]);
let path2 = Path(vec![4, 5, 6]);
let rel1 = Rel {
path: path1.clone(),
count: 37,
};
let rel2 = Rel {
path: path2.clone(),
count: 87,
};
let mut cache = RelCache::new();
cache.insert(path1, rel1);
cache.insert(path2, rel2);
let cl = Cl2 {
cache
};
let json = serde_json::to_string(&cl).unwrap();
dbg!(&json);
let obj: Cl2 = serde_json::from_str(&json).unwrap();
dbg!(obj);
}
/// Output:
///
/// [src/main.rs:58] &json = "{\"cache\":[[[4,5,6],{\"path\":[4,5,6],\"count\":87}],[[1,2,3],{\"path\":[1,2,3],\"count\":37}]]}"
/// [src/main.rs:60] obj = Cl2 {
/// cache: {
/// Path(
/// [
/// 1,
/// 2,
/// 3,
/// ],
/// ): Rel {
/// path: Path(
/// [
/// 1,
/// 2,
/// 3,
/// ],
/// ),
/// count: 37,
/// },
/// Path(
/// [
/// 4,
/// 5,
/// 6,
/// ],
/// ): Rel {
/// path: Path(
/// [
/// 4,
/// 5,
/// 6,
/// ],
/// ),
/// count: 87,
/// },
/// },
/// }