Show / Hide Table of Contents

    Json Dictionary Utility

    Use Case

    Unity's built-in JsonUtility cannot serialize Dictionary<TKey,TValue> objects. Therefore, JsonDictionaryUtility can be used to serialize and deserialize dictionary data.

    Usage

    The usage of JsonDictionaryUtility is identical to JsonUtility:

    Serialize to JSON

    To serialize an array to JSON, call ToJson. It returns a JSON object with two arrays "keys" and "values". These arrays are the contents of the array entries.

    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 FromJson to deserialize the JSON string and convert it to a native Dictionary<TKey,TValue>.

    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. JsonDictionaryUtility reads all keys and serializes them into an JSON array. The values are also serialized into their own array. The key-value relationship is preserved by the same indices. This means that the first enty in the keys array is the key for the first value in the values array and so on.

    JsonDictionaryUtility 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.

    • Improve this Doc
    Back to top i5 Toolkit Documentation