Search code examples
javajsonjacksonfasterxml

Jackson JSON: Serialize Array of Objects as their parent type


I'm serializing an array of objects of different types, but all of which are of supertype BasicClass. When I serialize it as such:

@JsonProperty
public BasicClass[] getReferences() {
    return this.refs;
}

it also serializes all the subtype JSON properties. How can I make it only serialize the supertype properties?


Solution

  • You should use the @JsonSerialize annotation. It will allow you to specify a Typing value as either dynamic or static. In your case you want to use the static type rather than waiting til runtime for it to see the specific subclass.