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.