user3056462
user3056462

Reputation: 75

How to ignore @JsonProperty when Serialization in Java

json string when deserialization

{"rCode":"1234"} deserialize to Account

public class Account {
    @JsonProperty("rCode")
    private String reasonCode;
}

but, when serialize Account to others like below :

{"reasonCode":"1234"}

How to ignore @JsonProperty("rCode") when Serialization? or How to change property name of json when serialization?

Upvotes: 0

Views: 1572

Answers (3)

Yu Fang Cheng
Yu Fang Cheng

Reputation: 31

The BEST solution is using @JsonAlias, it only works for deserialization.

Upvotes: 0

E.Big
E.Big

Reputation: 771

Simple way:

enum class MyEnum {
    @JsonProperty("1")
    ON, 

    @JsonProperty("0")
    OFF;

    @JsonValue
    fun serialize(): String {
        return this.name
    }
}

So this enum will correctly read input values 0, 1 into OFF, ON but after serialization, output values of the object will be OFF, ON still

Upvotes: 0

Eugene Evdokimov
Eugene Evdokimov

Reputation: 2545

Use simple access methods annotated with @JsonGetter or @JsonSetter respectively, each configured with required name of json property. In your case the code could be something like this:

public class Account {

    private String reasonCode;

    @JsonGetter("reasonCode")
    public String getReasonCode() {
        return reasonCode;
    }

    @JsonSetter("rCode")
    public void setReasonCode(String reasonCode) {
        this.reasonCode = reasonCode;
    }
}

Upvotes: 0

Related Questions