How to retrieve Serialized Name annotation value from java class | Core Java Training
20% Super Sale Discount Avail Now

Core Java Training Forum

How to retrieve Serialized Name annotation value from java class

M
Manju Bansal Post on 07/11/2018

I have created one java pojo class and it has many @SerilalizedName annotation. Can you guide me how can I retrieve all the string value of @SeriliazedName in other java class
===========================================================

public class AppleTest {

 

    @SerializedName("ae")

    @Expose

    private Ae ae;

    @SerializedName("at")

    @Expose

    private At at;

    @SerializedName("au")

    @Expose

    private Au au;

    @SerializedName("be-fr")

    @Expose

    private BeFr beFr;

    @SerializedName("be-nl")

    @Expose

    private BeNl beNl;

    @SerializedName("br")

    

    @Expose

    private Br br;

    @SerializedName("ca")

    @Expose

    private Ca ca;

    @SerializedName("ch-de")

    @Expose

    private ChDe chDe;

    @SerializedName("ch-fr")

    @Expose

    private ChFr chFr;

    @SerializedName("cn")

    @Expose

    private Cn cn;

    @SerializedName("cz")

    @Expose

    private Cz cz;

    @SerializedName("de")

    @Expose

    private De de;

    @SerializedName("dk")

    @Expose

    private Dk dk;

    @SerializedName("es")

    @Expose

    private Es es;

    @SerializedName("fi")

    @Expose

    private Fi fi;

    @SerializedName("fr")

    @Expose

    private Fr fr;

    @SerializedName("hk")

    @Expose

    private Hk hk;

    @SerializedName("hk-zh")

    @Expose

    private HkZh hkZh;

    @SerializedName("hu")

    @Expose

    private Hu hu;

    @SerializedName("id")

    @Expose

    private Id id;

    @SerializedName("ie")

    @Expose

    private Ie ie;

    @SerializedName("it")

    @Expose

    private It it;

    @SerializedName("jp")

    @Expose

    private Jp jp;

    @SerializedName("kr")

    @Expose

    private Kr kr;

    @SerializedName("lu")

    @Expose

    private Lu lu;

    @SerializedName("mx")

    @Expose

    private Mx mx;

    @SerializedName("my")

    @Expose

    private My my;

    @SerializedName("nl")

    @Expose

    private Nl nl;

    @SerializedName("no")

    @Expose

    private No no;

    @SerializedName("nz")

    @Expose

    private Nz nz;

    @SerializedName("ph")

    @Expose

    private Ph ph;

    @SerializedName("pl")

    @Expose

    private Pl pl;

    @SerializedName("pt")

    @Expose

    private Pt pt;

    @SerializedName("ru")

    @Expose

    private Ru ru;

    @SerializedName("se")

    @Expose

    private Se se;

    @SerializedName("sg")

    @Expose

    private Sg sg;

    @SerializedName("th")

    @Expose

    private Th th;

    @SerializedName("th-en")

    @Expose

    private ThEn thEn;

    @SerializedName("tr")

    @Expose

    private Tr tr;

    @SerializedName("tw")

    @Expose

    private Tw tw;

    @SerializedName("uk")

    @Expose

    private Uk uk;

    @SerializedName("us")

    @Expose

    private Us us;

    @SerializedName("vn")

    @Expose

    private Vn vn;

    @SerializedName("xf")

    @Expose

    private Xf xf;

 

    public Ae getAe() {

        return ae;

    }

 

    public void setAe(Ae ae) {

        this.ae = ae;

    }

 

    public At getAt() {

        return at;

    }

 

    public void setAt(At at) {

        this.at = at;

    }

 

    public Au getAu() {

        return au;

    }

 

    public void setAu(Au au) {

        this.au = au;

    }

 

    public BeFr getBeFr() {

        return beFr;

    }

 

    public void setBeFr(BeFr beFr) {

        this.beFr = beFr;

    }

 

    public BeNl getBeNl() {

        return beNl;

    }

 

    public void setBeNl(BeNl beNl) {

        this.beNl = beNl;

    }

 

    public Br getBr() {

        return br;

    }

 

    public void setBr(Br br) {

        this.br = br;

    }

 

    public Ca getCa() {

        return ca;

    }

 

    public void setCa(Ca ca) {

        this.ca = ca;

    }

 

    public ChDe getChDe() {

        return chDe;

    }

 

    public void setChDe(ChDe chDe) {

        this.chDe = chDe;

    }

 

    public ChFr getChFr() {

        return chFr;

    }

 

    public void setChFr(ChFr chFr) {

        this.chFr = chFr;

    }

 

    public Cn getCn() {

        return cn;

    }

 

    public void setCn(Cn cn) {

        this.cn = cn;

    }

 

    public Cz getCz() {

        return cz;

    }

 

    public void setCz(Cz cz) {

        this.cz = cz;

    }

 

    public De getDe() {

        return de;

    }

 

    public void setDe(De de) {

        this.de = de;

    }

 

    public Dk getDk() {

        return dk;

    }

 

    public void setDk(Dk dk) {

        this.dk = dk;

    }

 

    public Es getEs() {

        return es;

    }

 

    public void setEs(Es es) {

        this.es = es;

    }

 

    public Fi getFi() {

        return fi;

    }

 

    public void setFi(Fi fi) {

        this.fi = fi;

    }

 

    public Fr getFr() {

        return fr;

    }

 

    public void setFr(Fr fr) {

        this.fr = fr;

    }

 

    public Hk getHk() {

        return hk;

    }

 

    public void setHk(Hk hk) {

        this.hk = hk;

    }

 

    public HkZh getHkZh() {

        return hkZh;

    }

 

    public void setHkZh(HkZh hkZh) {

        this.hkZh = hkZh;

    }

 

    public Hu getHu() {

        return hu;

    }

 

    public void setHu(Hu hu) {

        this.hu = hu;

    }

 

    public Id getId() {

        return id;

    }

 

    public void setId(Id id) {

        this.id = id;

    }

 

    public Ie getIe() {

        return ie;

    }

 

    public void setIe(Ie ie) {

        this.ie = ie;

    }

 

    public It getIt() {

        return it;

    }

 

    public void setIt(It it) {

        this.it = it;

    }

 

    public Jp getJp() {

        return jp;

    }

 

    public void setJp(Jp jp) {

        this.jp = jp;

    }

 

    public Kr getKr() {

        return kr;

    }

 

    public void setKr(Kr kr) {

        this.kr = kr;

    }

 

    public Lu getLu() {

        return lu;

    }

 

    public void setLu(Lu lu) {

        this.lu = lu;

    }

 

    public Mx getMx() {

        return mx;

    }

 

    public void setMx(Mx mx) {

        this.mx = mx;

    }

 

    public My getMy() {

        return my;

    }

 

    public void setMy(My my) {

        this.my = my;

    }

 

    public Nl getNl() {

        return nl;

    }

 

    public void setNl(Nl nl) {

        this.nl = nl;

    }

 

    public No getNo() {

        return no;

    }

 

    public void setNo(No no) {

        this.no = no;

    }

 

    public Nz getNz() {

        return nz;

    }

 

    public void setNz(Nz nz) {

        this.nz = nz;

    }

 

    public Ph getPh() {

        return ph;

    }

 

    public void setPh(Ph ph) {

        this.ph = ph;

    }

 

    public Pl getPl() {

        return pl;

    }

 

    public void setPl(Pl pl) {

        this.pl = pl;

    }

 

    public Pt getPt() {

        return pt;

    }

 

    public void setPt(Pt pt) {

        this.pt = pt;

    }

 

    public Ru getRu() {

        return ru;

    }

 

    public void setRu(Ru ru) {

        this.ru = ru;

    }

 

    public Se getSe() {

        return se;

    }

 

    public void setSe(Se se) {

        this.se = se;

    }

 

    public Sg getSg() {

        return sg;

    }

 

    public void setSg(Sg sg) {

        this.sg = sg;

    }

 

    public Th getTh() {

        return th;

    }

 

    public void setTh(Th th) {

        this.th = th;

    }

 

    public ThEn getThEn() {

        return thEn;

    }

 

    public void setThEn(ThEn thEn) {

        this.thEn = thEn;

    }

 

    public Tr getTr() {

        return tr;

    }

 

    public void setTr(Tr tr) {

        this.tr = tr;

    }

 

    public Tw getTw() {

        return tw;

    }

 

    public void setTw(Tw tw) {

        this.tw = tw;

    }

 

    public Uk getUk() {

        return uk;

    }

 

    public void setUk(Uk uk) {

        this.uk = uk;

    }

 

    public Us getUs() {

        return us;

    }

 

    public void setUs(Us us) {

        this.us = us;

    }

 

    public Vn getVn() {

        return vn;

    }

 

    public void setVn(Vn vn) {

        this.vn = vn;

    }

 

    public Xf getXf() {

        return xf;

    }

 

    public void setXf(Xf xf) {

        this.xf = xf;

    }

 

}

Y

Instructor

Yogesh Chawla Reply on 20/11/2018


To explain this, we must know what is serialization and deserialization.
Serialization is converting object into json string and deserialization is converting json string back into object.

Let's say we've User class with no annotations.

public class User{
private String userName;
private Integer userAge;

public User(String name, Integer age){
userName = name;
userAge = age;
}
}

And we serialize this object as below

User user = new User("ABC", 30);
Gson gson = new Gson();
String jsonString = gson.toJson(user);

Json string will be like this

{
"userName":"ABC",
"userAge":30
}

where Gson provides a set of annotations to simplify the serialisation and deserialisation processes.
This @SerializedName and @Expose annotations are part of gson API only.

package is com.google.gson.annotations.*;


jar is

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>


Now if we add annotation @SerializedName

public class User{

@SerializedName("name")
private String userName;
@SerializedName("age")
private Integer userAge;

public User(String name, Integer age){
userName = name;
userAge = age;
}
}

Json string will be like this

{
"name":"ABC",
"age":30
}

see the difference in name

whereas

@Expose is used to allow or disallow serialization and deserialization.
@Expose is optional and it has two configuration parameters: serialize and deserialize.

By default they're set to true. To serialize and deserialize with @Expose we create gson object like this

Gson gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

Below userName won't be deserialized. userName's value will be null.

@SerializedName("name")
@Expose(deserialize = false)
private String userName;

Below userName won't be serialized.

@SerializedName("name")
@Expose(serialize = false)
private String userName;

Json string will be like this. Only userAge will be deserialized.

{
"age":30
}

This is what @SerializedName and @Expose annotation do.

Now if you want to call these fields in another class, do this

Let's say this your class:

public class Box {

@SerializedName("w")
private int width;

@SerializedName("h")
private int height;

@SerializedName("d")
private int depth;

}

to get this, you need to do this

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
public static void main(final String[] args) {
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();

final Box box = new Box();
box.setWidth(10);
box.setHeight(20);
box.setDepth(30);

final String json = gson.toJson(box);
System.out.printf("Serialised: %s%n", json);

final Box otherBox = gson.fromJson(json, Box.class);
System.out.printf("Same box: %s%n", box.equals(otherBox));
}
}

The above code will print the following to the command prompt.

Serialised: {"w":10,"h":20,"d":30}
Same box: true