0

an API returns the following JSON:

"objectA":{
  "type":"typeA",
  "value":{
    "propertyA":{
      "propertyAA":"3000",
      "propertyAB":3
    },
    "propertyB":10
  }
}

While "type" can be a wide range of classes and the "value" field contains the properties of that object. Is there an out of the box / annotation based solution provided by Jackson to solve this?

Before the last update by the API provider, the API was returning:

"objectA":{"type":"typeA","propertyA":{"propertyAA":"3000","propertyAB":3},"propertyB":10}

Which I was able to parse using:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = TypeA.class, name = "typeA"),
[...]

As stated above, I am wondering if there is a Jackson configuration that allows to configure both, the field containing the type and the field which contains the actual object.

Thank you very much and best regards!

Marvin
  • 11
  • 2

1 Answers1

0

I temporary solved this by introducing a wrapper object which handles the 'type' and 'value' fields. For the 'value' field I used the "EXTERNAL_PROPERTY" approach as shown here: Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected

Works, but still does not feel very clean :( So if someone knows a better approach please share it! :)

Marvin
  • 11
  • 2