将 JSON 反序列化为具有通用类型参数的接口列表

Deserialization of JSON to List of Interface with generic type parameter

如标题所述,我正在尝试反序列化 JSON 但遇到了一些麻烦。我认为下面包含了必要的信息。

public class Variable<T> : IVariable where T : IConvertible
{
    //...
}

public class ArrayVariable<T> : IVariable where T : IConvertible
{
    //...
}

所以我有一个 IVariable 列表,然后我成功序列化了它(所有信息都在 json 中):

JsonConvert.SerializeObject(myIVariableList)

现在我正在尝试反序列化它,但我无法确定正确的方法来进行反序列化,因为它涉及查找通用类型 T 除了类型 VariableArrayVariable。我已经试过了

JsonConvert.DeserializeObject<List<IVariable>>(result.newValues)

但显然,您可以创建接口的实例。任何帮助将不胜感激。

您可以使用 TypeNameHandling.All 将类型信息添加到您的序列化 json 中,然后在解析过程中使用它:

var variables = new List<IVariable>()
{
    new Variable<int>()
};
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
var serializeObject = JsonConvert.SerializeObject(variables, settings);
var list = JsonConvert.DeserializeObject<List<IVariable>>(serializeObject, settings);

您可以使用 TypeNameHandling.All,但我强烈建议您避免使用它,因为它是

另一个更安全的选择是使用自定义转换器。这是一个非常简单(且脆弱)的示例,应该可以帮助您入门:

首先让我们做一些共享接口的基本 类:

public interface IVariable { }

public class Foo : IVariable
{
    public int A { get; set; }
}

public class Bar : IVariable
{
    public int B { get; set; }
}

现在我们可以制作转换器了:

public class IVariableConverter : JsonConverter<IVariable>
{
    public override IVariable ReadJson(JsonReader reader, Type objectType, 
        IVariable existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // First load the JSON into a JObject
        var variable = JObject.Load(reader);

        // If the JSON had a property called A, it must be a Foo:
        if (variable.ContainsKey("A"))
        {
            return variable.ToObject<Foo>();
        }

        // If the JSON had a property called B, it must be a Bar:
        if (variable.ContainsKey("B"))
        {
            return variable.ToObject<Bar>();
        }

        // And who knows what was passed in if it was missing both of those properties?!
        throw new Exception("Er, no idea what that JSON was supposed to be!");


    }

    public override void WriteJson(JsonWriter writer, IVariable value, 
        JsonSerializer serializer)
    {
        // Feel free to write your own code here if you need it
        throw new NotImplementedException();
    }
}

现在我们可以做一些实际的反序列化了:

// A basic JSON example:
var json = "[{\"A\":1},{\"B\":2}]";

// The settings to tell the serialiser how to process an IVariable object
var settings = new JsonSerializerSettings
{
    Converters = new List<JsonConverter> { new IVariableConverter() }
};

// And deserialise with the defined settings
var result = JsonConvert.DeserializeObject<List<IVariable>>(json, settings);

您需要在如何识别每种类型方面更有创意,但这是实现您需要的安全方法。