Serde: How to parse JSON partially?


#1

I have JSON which is a mapping of string to arbitrary data structures. Something like this:

{"One": 4,
 "Two": "Hello",
 "Three": [3,4,"super"],
 ...
}

I need to add an additional key to the mapping. It maps to an integer. After the addition, I need to serialize the data into JSON again, without losing anything of the original JSON.

How can I achieve this using Sarde? I’ve managed to deserialize JSON data whose structure is totally known to me. But in this case, I can only make the assumption that the JSON is a mapping at top-level.


#2

You can parse your arbitrary json string to a serde_json::Value::Object type. That’s just a hashmap of string keys and serde_json::Value values so you can then walk / fiddle with this structure to your hearts content and then serialise it back into json.

Right now, parsing to structs only works for exact structure matches.


#3

Thank you! I still had some way to go, but at least, the compiler accepts it now.