Just murmur

Duplicated JSON Properties After Serialize with Jackson

Following problem found in the previous post, certain property got duplicated in the JSON after serialized polymorphic objects with Jackson.

According to this article, Jackson will output action property based on the value of @JsonSubTypes.Type, and then serialize the object. Since action property is used for polymorphic object deserialization and as an actual member of the class, action get duplicated.

The solution is simple and can be found in the same artitle. Just change JsonTypeInfo.As.PROPERTY in @JsonTypeInfo to JsonTypeInfo.As.EXISTING_PROPERTY.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, visible = true, property = "action")
@JsonSubTypes({
    @Type(value = SendMessageAction.class, name = "sendMessage"),
    @Type(value = LoginAction.class, name = "login")
})
abstract class Action {
    protected String action;

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}