简体   繁体   中英

Jackson polymorphic deserialization of property using an existing sibling property value

I have an existing Request / Response protocol using JSON that I have no control over.

Example 1: response JSON not requiring any polymorphic deserialisation

{
  "name" : "simple_response"
  "params" : {
    "success" : true
  }
}

Example 2: response JSON requiring polymorphic deserialisation of params property

{
  "name" : "settings_response",
  "params" : {
    "success" : true,
    "settings" : "Some settings info"
  }
}

My class structure looks like this:

class Response { // Not abstract. Used if no specialized response properties needed
  @JsonProperty("params")
    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
            include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
            property = "name")
    @JsonSubTypes({
            @JsonSubTypes.Type(value=GetSettingsResponseParams.class, name="settings_response")
    })
  Params params;
  String name; // Need to use its value to determine type of params
}

class Params {
  boolean success;
}

class GetSettingsResponseParams extends Params {
  String settings;
}

When I try to deserialise the JSON in "Example 2" I get:

Unexpected token (END_OBJECT), expected VALUE_STRING: need JSON String that contains type id (for subtype of com.foo.Params)

What am I doing wrong and how can I fix it?

Response model should look like:

class Response {

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "name", visible = true)
    @JsonSubTypes({
            @JsonSubTypes.Type(value = GetSettingsResponseParams.class, name = "settings_response"),
            @JsonSubTypes.Type(value = Params.class, name = "simple_response")
    })
    private Params params;
    private String name;

    // getters, settets, toString, etc.
}

Above model works properly for two presented JSON payloads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM