为什么 Jackson 多态序列化在列表中不起作用? [英] Why does Jackson polymorphic serialization not work in lists?

查看:22
本文介绍了为什么 Jackson 多态序列化在列表中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

杰克逊正在做一些真正奇怪的事情,我找不到任何解释.我正在做多态序列化,当一个对象独立时它可以完美地工作.但是,如果您将同一个对象放入一个列表并序列化该列表,则会删除类型信息.

Jackson is doing something truly bizarre and I cannot find any explanation for it. I'm doing polymorphic serialization and it works perfectly when an object is on its own. But if you put the same object into a list and serialize the list instead, it erases the type information.

丢失类型信息的事实会导致人们怀疑类型擦除.但这发生在列表的内容的序列化过程中;Jackson 所要做的就是检查它正在序列化的当前对象以确定其类型.

The fact that it's losing type info would lead one to suspect type erasure. But this is happening during serialization of the contents of the list; all Jackson has to do is inspect the current object it's serializing to determine its type.

我使用 Jackson 2.5.1 创建了一个示例:

I've created an example using Jackson 2.5.1:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class Test {

  @JsonIgnoreProperties(ignoreUnknown = true)
  @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
  @JsonSubTypes({
    @Type(value = Dog.class, name = "dog"),
    @Type(value = Cat.class, name = "cat")})
  public interface Animal {}

  @JsonTypeName("dog")
  public static class Dog implements Animal {
    private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }

  @JsonTypeName("cat")
  public static class Cat implements Animal {
    private String name;

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }

  public static void main(String[] args) throws JsonProcessingException {
    List<Cat> list = new ArrayList<>();
    list.add(new Cat());
    System.out.println(new ObjectMapper().writeValueAsString(list));
    System.out.println(new ObjectMapper().writeValueAsString(list.get(0)));
  }
}

输出如下:

[{"name":null}]
{"@type":"cat","name":null}

如您所见,当对象在列表中时,Jackson 没有添加类型信息.有谁知道为什么会这样?

As you can see, Jackson is not adding the type information when the object is in a list. Does anyone know why this is happening?

推荐答案

讨论了造成这种情况的各种原因 这里这里.我不一定同意其中的原因,但是 Jackson 由于类型擦除而立即不知道 List(或 CollectionMap) 包含.它选择使用不解释您的注释的简单序列化程序.

The various reasons for why this happens are discussed here and here. I don't necessarily agree with the reasons, but Jackson, because of type erasure, doesn't off the bat know the type of elements the List (or Collection or Map) contains. It chooses to use a simple serializer that doesn't interpret your annotations.

您在这些链接中建议了两个选项:

You have two options suggested in those links:

首先,您可以创建一个实现 List 的类,适当地实例化它并序列化实例.

First, you can create a class that implements List<Cat>, instantiate it appropriately and serialize the instance.

class CatList implements List<Cat> {...}

泛型类型参数 Cat 不会丢失.Jackson 可以访问并使用它.

The generic type argument Cat is not lost. Jackson has access to it and uses it.

其次,您可以为 List 类型实例化和使用 ObjectWriter.例如

Second, you can instantiate and use an ObjectWriter for the type List<Cat>. For example

System.out.println(new ObjectMapper().writerFor(new TypeReference<List<Cat>>() {}).writeValueAsString(list));

将打印

[{"@type":"cat","name":"heyo"}]

这篇关于为什么 Jackson 多态序列化在列表中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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