通过现场进行杰克逊多态反序列化

时间:2018-06-25 08:08:45

标签: java jackson deserialization fasterxml

让我说一堂课

public class A{
  private UUID typeId;
  private B data;
}

public abstract class B{
  private String a;
}

public class BChildOne extends B{
  ... some variables
}

public class BChildTwo  extends B{
  ... some variables
}

类B的类型正在更改,根据A的typeId,因此,如果A的typeId为“ XXX”,则数据字段的类型为BChildOne,如果A的typeId为“ YYY”,则数据字段的类型为BChildTwo。 / p>

我该如何实现?

所以我尝试过;

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility =
JsonAutoDetect.Visibility.NONE, setterVisibility = 
JsonAutoDetect.Visibility.NONE)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = 
JsonTypeInfo.As.EXTERNAL_PROPERTY , property = "typeId")
@JsonSubTypes({
 @JsonSubTypes.Type(value = BChildOne.class, name = "40ad2fe6-e672-4f0e- 
986e- 
 619c7a1a3223") }
 )
 public abstract class B{

但我收到以下错误;

  

意外的令牌(END_OBJECT),预期的FIELD_NAME:缺少包含类型ID(用于B类)的属性“ typeId”

这很明显,因为typeId字段在类A中而不在类B中。

2 个答案:

答案 0 :(得分:2)

假设您的JSON文档如下:

{
  "type": "foo",
  "data": {
    "someCommonProperty": "common property",
    "fooProperty": "foo specific property"
  }
}
{
  "type": "bar",
  "data": {
    "someCommonProperty": "common property",
    "barProperty": "bar specific property"
  }
}

您可以使用:

public class Wrapper {

    private String type;

    @JsonTypeInfo(use = Id.NAME, property = "type", include = As.EXTERNAL_PROPERTY)
    @JsonSubTypes(value = { 
        @JsonSubTypes.Type(value = Foo.class, name = "foo"),
        @JsonSubTypes.Type(value = Bar.class, name = "bar") 
    })
    private AbstractData data;

    // Getters and setters
}
public abstract class AbstractData {

    private String someCommonProperty;

    // Getters and setters
}
public class Foo extends AbstractData {

    private String fooProperty;

    // Getters and setters
}
public class Bar extends AbstractData {

    private String barProperty;

    // Getters and setters
}

在这种方法中,将@JsonTypeInfo设置为使用type作为external property来确定映射data属性的正确类。 JSON文档可以反序列化如下:

ObjectMapper mapper = new ObjectMapper();
Wrapper wrapper = mapper.readValue(json, Wrapper.class);  

答案 1 :(得分:0)

我曾经有一个相同的序列化问题,并且我做了一个这样的实现。参见下面的代码。

    protected Entity serialize(Object entity) throws Exception {
    try {
        if ( entity instanceof AbstractProcessRequest ) {
            AbstractProcessRequest request = (AbstractProcessRequest) entity;
            String value = mapper.writeValueAsString(request.prepareJSONRequest());
            logger.info("Telesales String json request " + value);
            return new Entity(value, UTF_8);
        } else {
            String value = mapper.writeValueAsString(entity);
            return new StringEntity(value, UTF_8);
        }
    } catch (Exception e) {
        logger.error("Telesales --> Error occured serializing entity", e);
        throw e;
    }
}

为实现通用结构,在如下所示的某些服务类中创建了execute方法。

private <T> T execute(Object entity, Class<T> clazz, HttpRequestBase request, String contentType) throws Exception {