I have a scenario where every json object shares some properties and those properties i want to be in the superclass, and every other property needs to be in the subclass wrapped in it's specific object key. The way i want to do this is like the following:
public class Shared {
@Data
@AllArgsConstructor
@NoArgConstructor
public static class Superclass {
@JsonProperty("shared1")
private String shared1;
@JsonProperty("shared2")
private String shared2;
}
@Data
@ToString(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
@NoArgsConstructor
/** Everything that is in the subclass should be in a wrapper key **/
public static class Subclass extends Superclass {
@JsonProperty("subClassSpecific1")
private String subClassSpecific1;
@JsonProperty("subClassSpecific2")
private String subClassSpecific2;
public Subclass(String shared1, String shared2, String subClassSpecific1, String subClassSpecific2) {
super(shared1, shared2);
this.subClassSpecific1 = subClassSpecific1;
this.subClassSpecific2 = subClassSpecific2;
}
}
}
the Json that i have looks like this:
{
"shared1": "value1",
"shared2": "value2",
"wrapper": {
"subClassSpecific1": "value3",
"subClassSpecific2": "value4"
}
}
now i know this can work if i have the Subclass properties wrapped in a separate object and annotate it with @JsonProperty("wrapper"), but is there a way to skip the wrapping and the properties and tell ObjectMapper to wrap the properties of the subclass to a nested key, instead of flattening it?