Jackson排除序列化过程中容器(列表,地图,集合)的类型信息 [英] Jackson excluding type information for containeirs (List, Map, Collection) during serialization

查看:209
本文介绍了Jackson排除序列化过程中容器(列表,地图,集合)的类型信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jackson 中,我想为每个自定义对象添加类型信息.为了做到这一点而没有注释,我正在使用

In Jackson I want to include type-information for every custom objects. To accomplish this without annotation, I am using

OBJECT_MAPPER.enableDefaultTypingAsProperty(DefaultTyping.NON_FINAL, "@Ketan");

它正在工作,但是它还包括ListMapCollection的类型信息,就像容器本身一样.

It is working but it is also including type-information for List, Map, Collection like container itself.

让我给您一个AnimalDogCatZoo层次结构的标准示例.

Let me give you a standard example of Animal, Dog, Cat and Zoo hierarchy.

class Zoo {

    List<Cat> cats;
    Dog dog;

    public Dog getDog() {
       return dog;
    }

    public void setDog(Dog dog) {
       this.dog = dog;
    }

    public List<Cat> getCats() {
       return cats;
    }

    public void setCats(List<Cat> cats) {
       this.cats = cats;
    }

}

在这里,我有两个自定义对象,CatDog.我只想只包含那些类型信息,但是它也包含了容器信息–在我的情况下也是List.

Here, I have two custom objects, Cat and Dog. I just want to include type information for only those, but it is including for container – List in my case – as well.

请在下面查看我通过序列化获得的JSON字符串.

Please see below the JSON string I got by serialization.

{
    "@Ketan": "com.csam.wsc.enabling.core.codec.json.test.Zoo1",
    "cats": [
        // This line contains the issue //
        "java.util.ArrayList",
        [
            {
                "@Ketan": "com.csam.wsc.enabling.core.codec.json.test.Cat",
                "name": "animalName",
                "likesCream": true,
                "lives": 10
            },
            {
                "@Ketan": "com.csam.wsc.enabling.core.codec.json.test.Cat",
                "name": "animalName",
                "likesCream": true,
                "lives": 10
            }
        ]
    ],
    "dog": {
        "@Ketan": "com.csam.wsc.enabling.core.codec.json.test.Dog",
        "name": "animalName",
        "barkVolume": 0.0
    }
}

对我来说,一切都很好,但突出显示的内容– JSON字符串中的java.util.ArrayList.我不想要这样的容器类型信息.

Everything is fine to me except what I have highlighted – java.util.ArrayList in the JSON string. I do not want such a container type information.

是否在API级别以上本身提供了简单的支持来实现此目标,而又不重写TypeResolverBuilder或进行任何自定义?

Is there any easy support at above API level itself to achieve this without overriding TypeResolverBuilder or any customization?

推荐答案

否,除非您声明Collections为最终类型.

No, unless you declare your Collections to be final types.

这篇关于Jackson排除序列化过程中容器(列表,地图,集合)的类型信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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