How to map JSON field names to different object field names?

JavaXmlJsonAnnotationsJackson

Java Problem Overview


What is the equiv way in Jackson json annotation for the following jax-b annotations?

I need to produce json rather than xml and need to know the conventional jackson annotations that is equivalently denoted in jax-b.

  1. rename a field.
  2. use getters instead of fields.

These features are especially crucial if the json/xml element name is a java reserved word like "new", "public", "static", etc.

So that we have to name the POJO fields as "_new_", "_public_", "_static_", etc, respectively,

but use jax-b annotation to rename them back to "new", "public", "static", etc in the generated XML (and json) elements.

Renaming a field

@XmlAccessorType(XmlAccessType.FIELD)
public class Person{
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String address;
    @XmlElement(name = "contractor")
    protected boolean _restricted_ ;
    @XmlElement(name = "new")
    protected boolean _new_ ;
}

Redirect to using property getter (I think this is how it is done in jax-b)

@XmlAccessorType(XmlAccessType.PROPERTY)
public class Person{
    protected String name;
    protected String address;
    protected boolean _restricted_ ;
    protected boolean _new_ ;
    
    @XmlElement(required = true)
    protected String getName() {return name;}
    @XmlElement(required = true)
    protected String getAddress() {return address;}
    @XmlElement(name = "contractor")
    protected boolean getRestricted() {return _restricted_;}
    @XmlElement(name = "new")
    protected boolean getNew(){return _new_;}
}

Java Solutions


Solution 1 - Java

Probably it's a bit late but anyway..

you can rename a property just adding

@JsonProperty("contractor")

And by default Jackson use the getter and setter to serialize and deserialize.

For more detailed information: http://wiki.fasterxml.com/JacksonFAQ

Solution 2 - Java

With some example, You can also use it in getter and setter to rename it to different field

public class Sample {

    private String fruit;

    @JsonProperty("get_apple")
    public void setFruit(String fruit) {
        this.fruit = fruit;
    }

    @JsonProperty("send_apple")
    public String getFruit() {
        return fruit;
    }

}

Solution 3 - Java

Please note that the proper JavaEE API for this is to use the javax.json.bind.annotation.JsonbProperty annotation. Of course Jackson and others are just some implementations of the JSON Binding API, they will likely comply with this.

Solution 4 - Java

If you are not using Jackson still want to rename a property you can use @SerializedName("your_original_key_name")

My JSON Data:

{
  "default": "0"
}

As you know we never use predefined keywords as a variable name so solution is:

@SerializedName("default")
private String default_value;

public String getDefault_value() {
        return default_value;
    }
public void setDefault_value(String default_value) {
        this.default_value = default_value;
    }

That's all you have to do now value comes from the key "default" and you can use it with getter and setter using "default_value"

In this case (Predifind Keywords as Json Key Name) or in any other case where you want to change your variable name to get data from the original key name this is the easiest approach.

Solution 5 - Java

I'm just reiterating @Himanshu's Answer,

Java Bean Property Field

@SerializedName("TCS Rate")
private String TCSRate;

Here we need this import

import com.google.gson.annotations.SerializedName;

Maven Dependency

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
	<version>2.9.0</version>
</dependency>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlessed GeekView Question on Stackoverflow
Solution 1 - JavaEnrichmanView Answer on Stackoverflow
Solution 2 - JavaVijaiView Answer on Stackoverflow
Solution 3 - JavaFlorin AsăvoaieView Answer on Stackoverflow
Solution 4 - JavaHimanshuView Answer on Stackoverflow
Solution 5 - JavaPratik GauravView Answer on Stackoverflow