I have incoming JSON data in the following format
{
"header": {
"schema_id": {
"namespace": "omh",
"name": "physical-activity",
},
},
"body": {
"activity_name": "walking",
"distance": {
"value": 1.5,
"unit": "mi"
},
}
}
and corresponding Java classes that looks like
public class DataPoint<T extends Measure> {
private DataPointHeader header;
private T body;
and
@JsonNaming(LowerCaseWithUnderscoresStrategy.class)
public class PhysicalActivity extends Measure {
private String activityName;
private LengthUnitValue distance;
I'd like Jackson to resolve body
to the PhysicalActivity
type based on the schema_id
in the JSON document, e.g. in pseudocode
if schema_id.namespace == 'omh' && schema_id.name == 'physical-activity'
then return PhysicalActivity.class
I've tried doing this with @JsonTypeIdResolver
but if I try to navigate to header.schema_id.name
with @JsonTypeInfo
, e.g.
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "header.schema_id.name")
@JsonTypeIdResolver(DataPointTypeIdResolver.class)
public abstract class Measure {
I get a missing property: 'header.schema_id.name'
error. And even if I could, I don't think I can take a decision on both the namespace
and name
properties.
Is there a sane way to do this besides building from scratch with @JsonTypeResolver
?