4
votes

I have the following app that shows that the key part of a Dictionary is not sent to JsonConverter, but it is called ToString() on. This is an issue for me as I can't deserialize my Json string .

Any ideas?

class Program
{
    static void Main(string[] args)
    {
        var coll = new Dictionary<Tuple<string,string>, string>();
        coll.Add(Tuple.Create("key1", "KEY1"), "Value1");
        coll.Add(Tuple.Create("key2", "KEY2"), "Value2");
        string json = JsonConvert.SerializeObject(coll);
        Dictionary<Tuple<string, string>, string> coll2;
        Console.WriteLine(json);
        //coll2 = JsonConvert.DeserializeObject<Dictionary<Tuple<string, string>, string>>(json);
        // It throws an exception here 
        //foreach (var k in coll2)
        //{
        //    Console.WriteLine("<{0}|{1}>",k.Key, k.Value);
        //} 

        var t = Tuple.Create("key1", "key2");
        Console.WriteLine(t.ToString());
        string json2 = JsonConvert.SerializeObject(t);
        Console.WriteLine(json2);
    }
}

Output :

{"(key1, KEY1)":"Value1","(key2, KEY2)":"Value2"} (key1, key2)
{"Item1":"key1","Item2":"key2"}
Press any key to continue . . .

2
What does "properly serialize" mean in this context? Can you show us what output you'd like to get? - siride
@siride: {"Item1":"key1","Item2":"key2"} would be the output if JsonConverter had been called. - Dave
Why you use Dictionary instead of Tuple<string, string, string>? - cuongle
A json is in the form {"AStringAsKey":AnObject} and a dictionary is serialized as {"key1":value1,"key2":value2} So your tuple's serialized version can not be used as a key. Therefore Json Serializer calls ToString to convert the dictionary's key(your tuple) to string - L.B
This is just an example showing the issue with the serialization, I will have many more key/value pairs in my dict. - Dave

2 Answers

5
votes

I also had the same problem with Deserializing a Dictionary with Tuple as key. JSON converts the tuple into a mere string. But in my case, i cannot avoid using Tuple as key in the dictionary. So i made a custom JSON convertor to Deserialize the Dictionary with Tuple as key and it worked well.

I have modified the same as per your code. Hope it will work fine and can give you an idea about JSON CustomConverter. Also explained better with comments.

public class TupleKeyConverter : JsonConverter
{
    /// <summary>
    /// Override ReadJson to read the dictionary key and value
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="objectType"></param>
    /// <param name="existingValue"></param>
    /// <param name="serializer"></param>
    /// <returns></returns>
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        Tuple<string, string> _tuple = null;
        string _value = null;
        var _dict = new Dictionary<Tuple<string, string>, string>();

        //loop through the JSON string reader
        while (reader.Read())
        {
            // check whether it is a property
            if (reader.TokenType == JsonToken.PropertyName)
            {
                string readerValue = reader.Value.ToString();
                if (reader.Read())
                {
                    // check if the property is tuple (Dictionary key)
                    if (readerValue.Contains('(') && readerValue.Contains(')'))
                    {
                        string[] result = ConvertTuple(readerValue);

                        if (result == null)
                            continue;

                        // Custom Deserialize the Dictionary key (Tuple)
                        _tuple = Tuple.Create<string, string>(result[0].Trim(), result[1].Trim());

                        // Custom Deserialize the Dictionary value
                        _value = (string)serializer.Deserialize(reader, _value.GetType());

                        _dict.Add(_tuple, _value);
                    }
                    else
                    {
                        // Deserialize the remaining data from the reader
                        serializer.Deserialize(reader);
                        break;
                    }
                }
            }
        }
        return _dict;
    }

    /// <summary>
    /// To convert Tuple
    /// </summary>
    /// <param name="_string"></param>
    /// <returns></returns>
    public string[] ConvertTuple(string _string)
    {
        string tempStr = null;

        // remove the first character which is a brace '('
        if (_string.Contains('('))
            tempStr = _string.Remove(0, 1);

        // remove the last character which is a brace ')'
        if (_string.Contains(')'))
            tempStr = tempStr.Remove(tempStr.Length - 1, 1);

        // seperate the Item1 and Item2
        if (_string.Contains(','))
            return tempStr.Split(',');

        return null;
    }

    /// <summary>
    /// WriteJson needs to be implemented since it is an abstract function.
    /// </summary>
    /// <param name="writer"></param>
    /// <param name="value"></param>
    /// <param name="serializer"></param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }

    /// <summary>
    /// Check whether to convert or not
    /// </summary>
    /// <param name="objectType"></param>
    /// <returns></returns>
    public override bool CanConvert(Type objectType)
    {
        return true;
    }
}

Now declare a property as follows. JsonConvertor Property is important.

[JsonConverter(typeof(TupleKeyConverter))]
public Dictionary<Tuple<int,string>,string> MyDict {get; set;}

Or you could try this to replace this in your code. though i never tested.

coll2 = JsonConvert.DeserializeObject<Dictionary<Tuple<string, string>, string>>("", new TupleKeyConverter());
0
votes

Based on the information you have provided, I would suggest that instead of using a Tuple as your key, use a custom struct or object and override the ToString method. Then you can serialize/deserialize as you wish.