austin
austin

Reputation: 1171

Repeat json field with different name

i have a POJO mapped which i serialize using Jackson

public class Foo{

private String bar;

// public setter and getter for bar
}

it serializes to

{bar:"value"}

is there a jackson annotation to get another field in the JSON with the same value but with a different alias name, something like

{bar:"value", another_bar:"value"}

Upvotes: 0

Views: 101

Answers (1)

Andreas
Andreas

Reputation: 159215

This should work for duplicating the value, though why you'd want to waste space like that is puzzling:

public class Foo {

    private String bar;

    @JsonProperty
    public String getBar() {
        return this.bar;
    }
    public void setBar(String bar) {
        this.bar = bar;
    }

    @JsonProperty("another_bar")
    public String getAnotherBar() {
        return this.bar;
    }
}

Upvotes: 1

Related Questions