Json.NET反序列化元组< ...>在另一种类型内不起作用?

时间:2015-01-12 09:32:46

标签: c# serialization json.net deserialization json-deserialization

使用Json.net,反序列化包含Tuple<...>的类型不起作用(序列化有效,但反序列化不起作用):

[TestMethod]
public void Test()
{
    var orig = new TupleHolder("what????", true);
    var json = JsonConvert.SerializeObject(orig);
    Assert.AreEqual("{\"Tup\":{\"Item1\":\"what????\",\"Item2\":true}}", json);
    // great! serialization works like a charm! now let's test deserialization:
    var dupl = JsonConvert.DeserializeObject<TupleHolder>(json);

    Assert.AreEqual("ZZZ", dupl.Tup.Item1); // pass! but it should be "what????"... what????
    Assert.AreEqual(false, dupl.Tup.Item2); // pass! but it should be "true", right???

    Assert.AreEqual(orig.Tup.Item1, dupl.Tup.Item1); // fail!
    Assert.AreEqual(orig.Tup.Item2, dupl.Tup.Item2); // fail!
}

public class TupleHolder
{
    public Tuple<string, bool> Tup { get; set; }
    public TupleHolder() { Tup = new Tuple<string, bool>("ZZZ", false); }
    public TupleHolder(string s, bool b) { Tup = new Tuple<string, bool>(s, b); }
}

有趣的是Tuple<...>的直接反序列化确实有效:

[TestMethod]
public void Test2()
{
    var orig = new Tuple<string, bool>("ABC", true);
    var json = JsonConvert.SerializeObject(orig);
    var dupl = JsonConvert.DeserializeObject<Tuple<string, bool>>(json);
    Assert.AreEqual(orig, dupl); // direct deserialization of Tuple<...> works.
}

这是一个Json.NET错误还是我错过了什么?

2 个答案:

答案 0 :(得分:2)

Remi提供的答案帮助了我。我拿了他的TupleConverter并将其设为2元组。任何N元组的概念都是一样的。

我把它留在这里,以防它帮助某人。

public class TupleConverter<U, V> : Newtonsoft.Json.JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(Tuple<U, V>) == objectType;
    }

    public override object ReadJson(
        Newtonsoft.Json.JsonReader reader,
        Type objectType,
        object existingValue,
        Newtonsoft.Json.JsonSerializer serializer)
    {
        if (reader.TokenType == Newtonsoft.Json.JsonToken.Null)
            return null;

        var jObject = Newtonsoft.Json.Linq.JObject.Load(reader);

        var target = new Tuple<U, V>(
            jObject["m_Item1"].ToObject<U>(), jObject["m_Item2"].ToObject<V>());

        return target;
    }

    public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}
  

注意:我的元组是使用m_Item1m_Item2进行JSON序列化的,因此我必须将jObject["ItemX"]更改为jObject["m_ItemX"]

List<Tuple<int, User>>的使用示例:

string result = "String to deserialize";
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new TupleConverter<int, User>());
List<Tuple<int, User>> users = JsonConvert.DeserializeObject<List<Tuple<int, User>>>(result, settings);

答案 1 :(得分:0)

我最终得到了一些更通用的东西,希望对你有帮助

public class TupleConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        var match = Regex.Match(objectType.Name, "Tuple`([0-9])", RegexOptions.IgnoreCase);
        return match.Success;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        try
        {
            var tupleTypes = objectType.GetProperties().ToList().Select(p => p.PropertyType).ToArray();

            var jObject = Newtonsoft.Json.Linq.JObject.Load(reader);

            var valueItems = new List<object>();

            for (var i = 1; i <= tupleTypes.Length; i++)
                valueItems.Add(jObject[$"m_Item{i}"].ToObject(tupleTypes[i - 1]));

            var convertedObject = objectType.GetConstructor(tupleTypes)?.Invoke(valueItems.ToArray());

            return convertedObject;
        }
        catch (Exception ex)
        {
            throw new Exception("Something went wrong in this implementation", ex);
        }
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}