DAW JSON Link
|
Dictionaries and Maps are not directly represented in JSON. There are several ways that they are commonly encoded.
Keys are stored as member names and Values as their value. This is very common, but limits the Key type to strings in JSON. However, numbers and other literals are able to be encoded as strings and will be automatically when used as the key type.
Too see a working example using this code, refer to cookbook_kv1_test.cpp Below the JSON object is mapped to a std::unordered_map<std::string, int>
Key/Values are stored as JSON objects in an array. Generally the key member's name is "key"
and the value members name is "value"
The above JSON document an array member named "kv"
whose element is an object with int keys, named "key"
and string values named "value"
.
Below it is mapped to a std::unorderd_map<intmax_t, std::string>
Too see a working example using this code, refer to cookbook_kv2_test.cpp
It is possible to parse/serialize Multimap likes types. A JSON document, such as
When mapped to a C++ class, it will parse the first "firstName"
it finds, skipping the rest. To parse this as a json_key_value
, the following code will work if it is a submember of the root, or subsequent submember's.
Too see a working example using this code, refer to cookbook_kv3_test.cpp
Alternatively, a type like std::vector<std::pair<Key, Value>>
would work in the above example too.
If the root object has duplicate keys, one needs to use the json_value
interface and then use from_json
to parse it.
Too see a working example using this code, refer to cookbook_kv4_test.cpp