Json Dictionary Utility
Use Case
Unity's built-in Json
Usage
The usage of Json
Serialize to JSON
To serialize an array to JSON, call To
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary.Add("firstKey", 42);
dictionary.Add("secondKey", 1);
string json = JsonDictionaryUtility.ToJson(dictionary);
// result:
// "{\"keys\":[\"firstKey\",\"secondKey\"],\"values\":[42,1]}"
Deserialize from JSON
If the JSON string uses the same format of a "keys"-array and a "values"-array, you can use From
string json = "{\"keys\":[\"firstKey\",\"secondKey\"],\"values\":[42,1]}";
Dictionary<string, int> dictionary = JsonDictionaryUtility.FromJson<string, int>(json);
// resulting dictionary has two entries with:
// dictionary["firstKey"] == 42
// dictionary["secondKey"] == 1
Functionality
A dictionary consists of a set of key value pairs with unique keys.
Json
Json
Dictionary cannot serialize or deserialize dictionaries to/from key-value pairs in the JSON string. It relies on the unfolding of the entries into the key and value arrays. Therefore, it is not directly compatible with other JSON libraries that convert dictionary keys to JSON keys and the dictionary values to the value of a key entry.Utility