I am trying to serialise a nested json to a BTree<string,string>. I will be using specific elements of this collection to bind to different structs as required.
JSON
{ "data": "some_data", "key": "some_key", "nestedValue": { "timestamp": "0", "d1": "d1", "d2": "d2", "time": 0, "secindaryNestedValue": [{ "d3": "test1", "d4": "test2" }, { "d3": "test3", "d4": "test4" } ] }, "timestamp": 0 } I am attempting to serialise this as follows:
let input: BTreeMap<String, String> = serde_json::from_str(INPUT).unwrap(); println!("input -> {:?}",input); I want to get an output as following:
BTree items
Key Value data some_data key some_key nested_value "{\"d1\":\"d1\",\"d2\":\"d2\",\"time\":0,\"secindaryNestedValue\":[{\"d3\":\"test1\",\"d4\":\"test2\"},{\"d3\":\"test3\",\"d4\":\"test4\"}]}" timestamp 0 I am doing this so that my nested jsons can be as generic as possible.
In subsequent operations I will be binding the nested json to a struct using serde as follows using the struct :
use serde_derive::Deserialize; use serde_derive::Serialize; #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Root { pub data: String, pub key: String, pub nested_value: NestedValue, pub timestamp: i64, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct NestedValue { pub timestamp: String, pub d1: String, pub d2: String, pub time: i64, pub secindary_nested_value: Vec<SecindaryNestedValue>, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct SecindaryNestedValue { pub d3: String, pub d4: String, } I would want to use the nested value later, Convert the json string to json and bind it to a similar struct.
Open to suggestions on not using a BTree and something better, but my usecase requires me to have the inner nested jsons as a string which I can bind later.
serde_json::value::RawValue?