阵列多态对象的JSON序列化 [英] JSON serialization of array with polymorphic objects

查看:216
本文介绍了阵列多态对象的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序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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