如何使用Newtonsoft Json.Net反序列化接口

时间:2018-05-30 21:24:52

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

我有这个类层次结构:

public class ProxyBotsSnapshotLogEntryDetails : IBotsSnapshotLogEntryDetails
{
    public ICollection<IBotSnapshot> Snapshots { get; set; }
}
public class ProxyBotSnapshot : IBotSnapshot
{
    public string Name { get; set; }
    public ICollection<IBotSnapshotItem> States { get; set; }
}

public class ProxyBotSnapshotItem : IBotSnapshotItem
{
    public int Count { get; set; }
    public IrcBotChannelStateEnum State { get; set; }
}

及其相应的接口

public interface IBotsSnapshotLogEntryDetails
{
    ICollection<IBotSnapshot> Snapshots { get; set; }
}

public interface IBotSnapshot
{
    string Name { get; set; }
    ICollection<IBotSnapshotItem> States { get; set; }
}

public interface IBotSnapshotItem
{
    int Count { get; set; }
    IrcBotChannelStateEnum State { get; set; }
}

我想从JSON反序列化:

var test = JsonConvert.DeserializeObject<ProxyBotsSnapshotLogEntryDetails>(entry.DetailsSerialized);

但我收到一条错误消息,说Newtonsoft无法转换接口。

我找到了这篇有前途的文章:

  

https://www.c-sharpcorner.com/UploadFile/20c06b/deserializing-interface-properties-with-json-net/

但我不确定如何使用该属性,因为在我的情况下,该属性是一个接口列表。

1 个答案:

答案 0 :(得分:1)

知道了!

文章中提供的转换器非常好用,我只是缺少在集合属性上使用它的语法。以下是包含转换器和工作属性的代码:

// From the article
public class ConcreteConverter<T> : JsonConverter
{
    public override bool CanConvert(Type objectType) => true;

    public override object ReadJson(JsonReader reader,
     Type objectType, object existingValue, JsonSerializer serializer)
    {
        return serializer.Deserialize<T>(reader);
    }

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

public class ProxyBotsSnapshotLogEntryDetails : IBotsSnapshotLogEntryDetails
{
    [JsonProperty(ItemConverterType = typeof(ConcreteConverter<ProxyBotSnapshot>))]
    public ICollection<IBotSnapshot> Snapshots { get; set; }
}
public class ProxyBotSnapshot : IBotSnapshot
{
    public string Name { get; set; }

    [JsonProperty(ItemConverterType = typeof(ConcreteConverter<ProxyBotSnapshotItem>))]
    public ICollection<IBotSnapshotItem> States { get; set; }
}

public class ProxyBotSnapshotItem : IBotSnapshotItem
{
    public int Count { get; set; }
    public IrcBotChannelStateEnum State { get; set; }
}