Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 297
755 Avatar asked May 20 '26 08:05

755


1 Answers

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.

like image 135
tuckermi Avatar answered May 22 '26 23:05

tuckermi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!