反序列化为正确的子对象 [英] Deserialize into correct child objects

查看:100
本文介绍了反序列化为正确的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将父级和不同子级的混合反序列化为List<parent>.不幸的是,我孩子的所有多余字段都被丢弃,并被序列化为父级.

I am trying to deserialize a mix of a parent and different childs into a List<parent>. Unfortunately all the extra fields of my childs get discarded and are being serialized as a parent.

我一直在尝试找出如何整体命名对象,以将它们链接到正确的子类.我没有任何结果.

I have been trying to figure out how to name the objects as a whole to link them to the correct child class. I did not have any results with this.

这些是一个简单的父类和子类,我尝试从中序列化对象:

These are a simple parent and child class i try to serialize objects from:

[JsonObject(MemberSerialization.OptIn)]
    class Product
    {
        [JsonProperty]
        public string Name { get; set; }
        [JsonProperty]
        public int Amount { get; set; }
        [JsonProperty]
        public int Value { get; set; }
    }

    [JsonObject(MemberSerialization.OptIn)]
    class ProductType : Product
    {
        [JsonProperty(Order = 4)]
        public int Volume { get; set; }
    }

序列化不是问题,体积被添加为特殊产品"对象的最终字段.目标是有多个孩子.在序列化列表中2个创建的对象的代码下面.反序列化失败,因为它将两个对象都放回列表中,成为普通产品:

Serializing is not a problem, volume gets added as final field for the "special product" object. The goal is to have multiple childs. Below the code that serializes 2 created objects in a list. It fails deserializing as it puts both objects back as a normal product in the list:

Product test1 = new Product { Name = "Simple Product", Amount = 110, Value = 10 };
ProductType test2 = new ProductType { Name= "Special Type", Amount = 230, Value = 22, Volume = 6 };

List<Product> ProductList = new List<Product>() { test1, test2 };             

string output = JsonConvert.SerializeObject(ProductList, Formatting.Indented); //Works correctly.

List<Product> DeSerialized = JsonConvert.DeserializeObject<List<Product>>(output); //Fails creating child object.

推荐答案

我必须启用类型名处理并将其作为设置参数传递给序列化器和反序列化器.我之所以陷入困境,是因为我想在课堂上做这样的事情.

I had to enable typename handling and pass that to the serializer and deserializer as a settings parameter. I was stuck on this because i was trying to do something like this on my classes.

JsonSerializerSettings settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

string output = JsonConvert.SerializeObject(ProductList, Formatting.Indented, settings);

List<product> deserializedList = JsonConvert.DeserializeObject<List<product>>(output, settings);

仍然应该有一个选项可以只序列化您想要的类,如上面的示例所示,我不需要序列化和存储基类/父类,因为我在(反)序列化时已经给出了该类型.我也不需要序列化List对象,这尤其会带来不必要的混乱.因此,尽管这是解决我的问题的一种方法,但我正在寻找一种更好的方法来解决此问题,但以下内容并没有给出错误,但似乎也没有执行任何操作:

Still there should be a option to serialize just the classes you want as for above example i do not need to serialize and store the base/parent class as i already give that type when (de)serializing. I also do not need to serialize the List object, this especially gives unnecessary clutter. So although this is a solution to my problem i am looking for a better way to do this, the following is does not give errors but does not seem to do anything either:

[JsonObject(ItemTypeNameHandling = TypeNameHandling.Objects/all/auto/...)]
class ...

这篇关于反序列化为正确的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆