How to retrieve Serialized Name annotation value from java class | Core Java 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 classAppleTest {
@SerializedName( "ae" )
@Expose
private Aeae;
@SerializedName( "at" )
@Expose
private Atat;
@SerializedName( "au" )
@Expose
private Auau;
@SerializedName( "be-fr" )
@Expose
privateBeFrbeFr;
@SerializedName( "be-nl" )
@Expose
private BeNlbeNl;
@SerializedName( "br" )
@Expose
privateB rbr;
@SerializedName( "ca" )
@Expose
privateCaca;
@SerializedName( "ch-de" )
@Expose
privateChDechDe;
@SerializedName( "ch-fr" )
@Expose
privateChFrchFr;
@SerializedName( "cn" )
@Expose
private Cncn;
@SerializedName( "cz" )
@Expose
private Czcz;
@SerializedName( "de" )
@Expose
private Dede;
@SerializedName( "dk" )
@Expose
private Dkdk;
@SerializedName( "es" )
@Expose
private Eses;
@SerializedName( "fi" )
@Expose
private Fifi;
@SerializedName( "fr" )
@Expose
private Frfr;
@SerializedName( "hk" )
@Expose
private Hkhk;
@SerializedName( "hk-zh" )
@Expose
private HkZhhkZh;
@SerializedName( "hu" )
@Expose
private Huhu;
@SerializedName( "id" )
@Expose
private Idid;
@SerializedName( "ie" )
@Expose
private Ieie;
@SerializedName( "it" )
@Expose
private Itit;
@SerializedName( "jp" )
@Expose
private Jpjp;
@SerializedName( "kr" )
@Expose
private Krkr;
@SerializedName( "lu" )
@Expose
private Lulu;
@SerializedName( "mx" )
@Expose
private Mxmx;
@SerializedName( "my" )
@Expose
private Mymy;
@SerializedName( "nl" )
@Expose
private Nlnl;
@SerializedName( "no" )
@Expose
private Nono;
@SerializedName( "nz" )
@Expose
private Nznz;
@SerializedName( "ph" )
@Expose
private Phph;
@SerializedName( "pl" )
@Expose
private Plpl;
@SerializedName( "pt" )
@Expose
private Ptpt;
@SerializedName( "ru" )
@Expose
private Ruru;
@SerializedName( "se" )
@Expose
private Sese;
@SerializedName( "sg" )
@Expose
private Sgsg;
@SerializedName( "th" )
@Expose
private Thth;
@SerializedName( "th-en" )
@Expose
private ThEnthEn;
@SerializedName( "tr" )
@Expose
private Trtr;
@SerializedName( "tw" )
@Expose
private Twtw;
@SerializedName( "uk" )
@Expose
private Ukuk;
@SerializedName( "us" )
@Expose
private Usus;
@SerializedName( "vn" )
@Expose
private Vnvn;
@SerializedName( "xf" )
@Expose
private Xfxf;
// Getters Setters of each field
}

Y

Instructor

Yogesh Chawla Reply on 20/11/2018

@SerializeName is used to set the key that json object will include ,however @Expose is used to decide whether the variable will be exposed for
Serialisation and Deserialisation ,or not.

To explain it we must know what is serialization and deserialization.
Serialization is converting object into json string and deserialization is converting json string 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", 25);
Gson gson = new Gson();
String jsonString = gson.toJson(user);

Json string will be like this

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

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":25
}
@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":25
}
Gson provides this 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



com.google.code.gson
gson
2.3.1