阵列多态对象的JSON序列化

编程入门 行业动态 更新时间:2024-10-24 16:24:52
本文介绍了阵列多态对象的JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有可能与.NET标准JavascriptSerializer / JsonDataContractSerializer或外部解析器,使用的包装方式,包括对象类型序列化对象数组?

Is it possible with .NET standard JavascriptSerializer/JsonDataContractSerializer or external parsers, to serialize objects array using a wrapper approach including the object type?

例如,从列表生成此JSON:

For example, to generate this JSON from a List:

[{ 'dog': { ...dog properties... } }, { 'cat': { ...cat properties... } }]

而不是典型的

instead of typical:

[{ ...dog properties... }, { ...cat properties... }]

这是可行的在Java中使用杰克逊JsonTypeInfo.As.WRAPPER_OBJECT属性。

This is doable in Java with Jackson using JsonTypeInfo.As.WRAPPER_OBJECT attribute.

推荐答案

Json.NET 有整洁的解决方案这一点。还有就是增加了智能型信息设置 - 声明它是这样的:

Json.NET has a neat solution for this. There is a setting that intelligently adds type information - declare it like this:

new JsonSerializer { TypeNameHandling = TypeNameHandling.Auto };

这将决定类型的嵌入是否需要,并添加必要。可以说,我有以下类:

This will determine whether type embedding is required and add it where necessary. Lets say I had the following classes:

public class Message { public object Body { get; set; } } public class Person { public string Name { get; set; } } public class Manager : Person { } public class Department { private List<Person> _employees = new List<Person>(); public List<Person> Employees { get { return _employees; } } }

请注意,邮件正文类型的对象,而经理人的子类。如果我序列化邮件与体部有一个经理,我得到这样的:

Notice the Message Body is of type object, and that Manager subclasses Person. If I serialize a Message with a Department Body that has a single Manager I get this:

{ "Body": { "$type":"Department, MyAssembly", "Employees":[ { "$type":"Manager, MyAssembly", "Name":"Tim" }] } }

注意它是如何增加的$ type属性来描述部经理类型。如果我现在一个人添加到员工列表,更改消息正文是这样的类型部:

Notice how it's added the $type property to describe the Department and Manager types. If I now add a Person to the Employees list and change the Message Body to be of type Department like this:

public class Message { public Department Body { get; set; } }

然后身体类型注释不再需要和新的人没有被标注 - 没有注解的假设元素实例声明数组类型。序列化格式变为:

then the Body type annotation is no longer needed and the new Person is not annotated - absence of annotation assumes the element instance is of the declared array type. The serialized format becomes:

{ "Body": { "Employees":[ { "$type":"Manager, MyAssembly", "Name":"Tim" }, { "Name":"James" }] } }

这是一种有效的方法 - 类型注释仅添加需要的地方。虽然这是.NET具体的方法很简单,以处理反序列化器/其他平台上的消息类型应该是相当容易地扩展到处理这个问题。

This is an efficient approach - type annotation is only added where required. While this is .NET specific, the approach is simple enough to handle that deserializers/message types on other platforms should be fairly easily extended to handle this.

我会讳莫如深,但在公共API用这个,因为它是非标准。在这种情况下,你想避免的多态​​性,并进行版本控制,并在邮件中输入信息是非常明确的属性。

I'd be reticent about using this in a public API though, as it is non-standard. In that case you'd want to avoid polymorphism, and make versioning and type information very explicit properties in the message.

更多推荐

阵列多态对象的JSON序列化

本文发布于:2023-08-04 06:01:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   对象   序列化   多态   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!