JsonAnyGetter / JsonAnySetter the resulting JSON has doubled values

Viewed 1610

I need to write a JSON string that follows this basic format:

"gesamtAngebot":{
    "angebotList":[{
        "instanzId":"string",
        "buchungsKontextList":[{
            "quellSystem":"SOMETHING",
            "payload":{}
        }],
        "payload":{"test1":"test1"}
    }]
}

I'm using the following class to present the data, and an instance of this class is serialized with the Jackson ObjectMapper.

@Data
public class Angebot {

    private String instanzId;

    private List<BuchungsKontext> buchungsKontextList;

    private Map<String, Object> payload = new HashMap<String, Object>();

    @JsonAnyGetter
    public Map<String, Object> any() {
        return payload;
    }

    @JsonAnySetter
    public void set(String name, Object value) {
        payload.put(name, value);
    }
}

If I serialize an instance of this class as-is, the resulting JSON will be something like this:

"gesamtAngebot":{
    "angebotList":[{
        "instanzId":"string",
        "buchungsKontextList":[{
            "quellSystem":"SOMETHING",
            "payload":{}
        }],
        "payload":{"test1":"test1"},
        "test1":"test1"
    }]
}

As you can see the data of "payload" is doubled as it's own element and I don't have any idea why.

Thanks in advance for your attention and advice.

2 Answers
Related