Jackson Polymorphic Deserialization via field

a 夏天 提交于 2021-02-19 06:18:05

问题


let's say, i have a class

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
}

type of class B is changing, according to A's typeId , so if typeId of A is "XXX", type of data field is BChildOne, and if typeId of A is "YYY", type of data field is BChildTwo.

how can i achive that?

so for i tried that;

@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{

but i got following error;

Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'typeId' that is to contain type id (for class B)

which is obvious, because typeId field is in class A not B.


回答1:


Assuming that your JSON documents are like:

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

You can use:

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
}

In this approach, @JsonTypeInfo is set to use type as an external property to determine the right class to map the data property. The JSON document can be deserialized as following:

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



回答2:


I had the same serialization issue once and I made an implementation like this. see the code below.

    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;
    }
}

To achieve generic structure an execute method is created in some service class like below.

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


来源:https://stackoverflow.com/questions/51018823/jackson-polymorphic-deserialization-via-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!