如何正确序列化元组作为关键字典

时间:2012-09-11 17:02:47

标签: c# json.net

我有以下应用程序,它显示字典的关键部分未发送到JsonConverter,但它被称为ToString()。这对我来说是一个问题,因为我无法反序列化我的Json字符串。

有什么想法吗?

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);
    }
}

输出:

  

{“(key1,KEY1)”:“Value1”,“(key2,KEY2)”:“Value2”}(key1,key2)
  { “项目1”: “KEY1”, “项目2”: “KEY2”}
  按任意键继续 。 。

2 个答案:

答案 0 :(得分:3)

我将使用元组作为键反序列化字典也遇到了同样的问题。 JSON将元组转换为纯粹的字符串。但在我的情况下,我无法避免使用元组作为字典中的键。所以我制作了一个自定义的JSON转换器,用Tuple作为键反序列化字典,效果很好。

我根据您的代码进行了相同的修改。希望它能正常工作,并且可以让您了解JSON CustomConverter。还可以通过评论更好地解释。

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;
    }
}

现在声明一个属性如下。 JsonConvertor Property很重要。

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

或者您可以尝试在代码中替换它。虽然我从未测试过。

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

答案 1 :(得分:0)

根据您提供的信息,我建议您不要使用Tuple作为键,而是使用自定义结构或对象并覆盖ToString方法。然后你可以按照自己的意愿序列化/反序列化。