Serde deserializing a JSON top-level dictionary


#1

I have JSON data like this

{
    "add0" : {
        "callcreates" : []
       
    },
    "add1" : {
        "callcreates" : []
    }
    //.... 254 other opcodes
}

And I would capture this in a Rust structure. At the top level I have what seems to be a hashmap in Rust. But note the JSON Is not something like

{"opcodes":  {"add1": ... "add2: ... }}

where the there is a placeholder for the top-level hashmap.

For the value of the hashmap, I could define a struct.

I have been able to use serde_json::Value to read in the entire structure, but then I don’t know how to iterate over the top-level JSON keys.

Suggestions?

P.S. http://valve.github.io/blog/2014/08/26/json-serialization-in-rust-part-2/ seems to do what I want except it no longer works playpen


#2

Have you tried just deserializing into a normal std::collections::HashMap? If that needs to be within a struct then you can either wrap it later, or use the #[serde(flatten)] annotation.

(playground)


#3

This is fantastic and does exactly what is needed. Many thanks.