简体   繁体   English

反序列化列表 <AbstractClass> 与newtonsoft.json

[英]Deserialize a List<AbstractClass> with newtonsoft.json

i'm trying to serialize and deserialize a list of abstract classes ( mustinherit for vb), obviusly inside it there are only instances of derived classes. 我正在尝试序列化和反序列化一个abstract类列表( mustinherit ),其中只有派生类的实例。

I've decorated the list parameter with the JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto) obtaining an output that look like this: 我用JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)修饰了list参数,获得了如下所示的输出:

But when i deserialize it keep saying that he cannot deserialize an abstract class. 但是当我反序列化时,它一直说他不能反序列化抽象类。

http://james.newtonking.com/json/help/index.html?topic=html/SerializeTypeNameHandling.htm http://james.newtonking.com/json/help/index.html?topic=html/SerializeTypeNameHandling.htm

public class ConcreteClass
{
    private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
    //<Newtonsoft.Json.JsonProperty(itemtypenamehandling:=Newtonsoft.Json.TypeNameHandling.Auto)>
    public ObservableCollection<AbstractClass> Nodes {
        get { return this._Nodes; }
    }
    public string Name { get; set; }
    public int Id { get; set; }
}

public abstract class AbstractClass
{
    private ObservableCollection<AbstractClass> _Nodes = new ObservableCollection<AbstractClass>();
    [Newtonsoft.Json.JsonProperty(itemtypenamehandling = Newtonsoft.Json.TypeNameHandling.Auto)]
    public ObservableCollection<AbstractClass> Nodes {
        get { return this._Nodes; }
    }
}

removing the commented line it works! 删除它起作用的注释行!

Make sure you specify TypeNameHandling when deserializing, as per the docs: 根据文档,确保在反序列化时指定TypeNameHandling:

// for security TypeNameHandling is required when deserializing
Stockholder newStockholder = JsonConvert.DeserializeObject<Stockholder>(jsonTypeNameAuto, new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.Auto
});

It is worth noting that the documentation is deserializing a Concrete class that contains a collection of Abstract classes. 值得注意的是,文档正在反序列化包含 Abstract类集合的Concrete类。

As an experiment try creating a throw-away class (concrete) that has a single property with your list of abstract objects and see if you can serialize and deserialize that. 作为一个实验,尝试创建一个抛弃类(具体),它具有一个带有抽象对象列表的单个属性,并查看是否可以对其进行序列化和反序列化。

UPDATE: 更新:

I just tested the following code in LINQPad: 我刚刚在LINQPad中测试了以下代码:

void Main()
{
    var test = new List<Business>();
    test.Add(new Hotel { Name = "Hilton", Stars = 5 });
    test.Add(new Pool { Name = "Big Splash", Capacity = 500 });

    test.Dump();

    string json = JsonConvert.SerializeObject(test, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });

    json.Dump();

    var businesses = JsonConvert.DeserializeObject<List<Business>>(json, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });

    businesses.Dump();
}

// Define other methods and classes here
public abstract class Business
{
    public string Name { get;set; }
}
public class Hotel : Business
{
    public int Stars { get;set; }
}
public class Pool : Business
{
    public int Capacity { get;set;}
}

It worked perfectly. 它工作得很好。 Abstract collection serialized to: 抽象集合序列化为:

{
  "$type": "System.Collections.Generic.List`1[[UserQuery+Business, query_jvrdcu]], mscorlib",
  "$values": [
    {
      "$type": "UserQuery+Hotel, query_jvrdcu",
      "Stars": 5,
      "Name": "Hilton"
    },
    {
      "$type": "UserQuery+Pool, query_jvrdcu",
      "Capacity": 500,
      "Name": "Big Splash"
    }
  ]
}

The original and the deserialized collections matched. 原始和反序列化的集合匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM