Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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_;} } 
like image 981
Blessed Geek Avatar asked Mar 16 '12 16:03

Blessed Geek


People also ask

How do I map one JSON to another JSON?

You can use the Jackson API to add field or transform a JSON without creating POJO's. It provides a object form called JsonNode , JsonObject and JsonArray types which can be transformed like i did in the below code. I hope this helps you.

Can we have map in JSON?

There are a number of ways to convert a Java Map into JSON. It is quite common to convert Java Arrays and Maps into JSON and vice versa. In this post, we look at 3 different examples to convert Java Map to JSON. We will be using Jackson, Gson and org.

What is the difference between JsonProperty and JsonAlias?

@JsonProperty can change the visibility of logical property using its access element during serialization and deserialization of JSON. @JsonAlias defines one or more alternative names for a property to be accepted during deserialization.


2 Answers

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

like image 57
Enrichman Avatar answered Oct 05 '22 13:10

Enrichman


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;     }  } 
like image 42
Vijai Avatar answered Oct 05 '22 14:10

Vijai