Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: Serialize/deserialize with different property name [duplicate]

I have this POJO:

public class SetPoint {

    private String tagName;
    //more fields

   //getters and setters

}

I'm getting SetPoints from a REST API, do something with them and then send them again. Problem is that I want deserialize a SetPoint from a JSON like:

{
    "tagnameOpc" : "6GH783",
    //more fields
}

But when I send them, I want serialize a SetPoint as:

{
    "tagName" : "6GH783"
    //more fields
}

I mean, I want the property tagName to be named different in each case.

Is this possible?

like image 997
Héctor Avatar asked Dec 18 '25 20:12

Héctor


1 Answers

Try using a different JsonProperty annotation for the getter and the setter. E.g.

@JsonProperty("tagnameOpc")
void setTagName(String name)

@JsonProperty("tagName")
String getTagName()

If that doesn't work try with an extra setter

@JsonIgnore
void setTagName(String name)

@JsonProperty("tagnameOpc")
void setTagNameOpc(String name) {
    setTagName(name);
}

@JsonProperty("tagName")
String getTagName()
like image 166
Manos Nikolaidis Avatar answered Dec 20 '25 08:12

Manos Nikolaidis



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!