I have a JSON request of the form:
{
"type": "Car"
"data": {
"object": {
"id": "1"
"color": "Red"
"plate": "J124D"
},
"owner": {
"name": "John"
}
}
}
Now, the type field dictates the content of the object field. In the example above, the type = Car means the object container will have id, color and plate fields. But, if the type = Plane, the Object field will have id, wingspan, manufacturer etc. Basically, the type field will dictate dynamically what Java object corresponds to "object". I am fairly new to Jackson, so I was looking for some pointers on how to achieve this type of dynamic mapping in code. So far I have this,
public class Request {
@JsonProperty
String type;
@JsonProperty
Data data; // Probably need a custom deserializer here?
}
The other difficulty is the usage of Request. Data would probably have to be cast into something like CarData or PlaneData so that callers could easily do request.getType() and then extract data from the request casted to the appropriate type of data (Car or Plane).