Gson: @Expose vs @SerializedName
Even if it's late I wanted to answer this question.
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("Ahmed", 30);
Gson gson = new Gson();
String jsonString = gson.toJson(user);
Json string will be like this
{
"userName":"Ahmed",
"userAge":30
}
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":"Ahmed",
"age":30
}
@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
}
Is there a Gson @DeserializedName equivalent to the @SerializedName annotation?
According to the doc
public abstract String[] alternate Returns: the alternative names of
the field when it is deserialized
so you can use
@SerializedName(value="this_field", alternate={"thisField"})
private Integer thisField;
This would lead into this_field
being used during serialization.
During deserialization it would be used this_field
if it is available and if not it will use thisField
if this becomes available.
How to serialize using gson with @SerializedName annotation?
The solution
String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN "
+ "(SELECT uid2 FROM friend WHERE uid1 = me() )";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql", params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());
try {
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
**//here i get Data values**
JSONObject data = response.getGraphObject()
.getInnerJSONObject();
FacebookResponses facebookResponses = gson
.fromJson(data.toString(),
FacebookResponses.class);
Intent i = new Intent(getActivity()
.getApplicationContext(),
FacebookUsersImages.class);
i.putExtra("facebookResponses", facebookResponses);
startActivity(i);
// Log.i(TAG, "Result finale : " +
// facebookResponses.toString());
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Request.executeBatchAsync(request);
FacebookResponses Class
public class FacebookResponses implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("data")
public FacebookRisp[] data;
public FacebookRisp[] getData() {
return data;
}
public void setData(FacebookRisp[] data) {
this.data = data;
}
@Override
public String toString() {
return "FacebookResponses [data=" + Arrays.toString(data) + "]";
}
}
FacebookRisp Class
public class FacebookRisp implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("pic_square")
private String pic_square;
@SerializedName("pic")
private String pic;
@SerializedName("pic_big")
private String pic_big;
@SerializedName("pic_small")
private String pic_small;
@SerializedName("uid")
private String uid;
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getPic_big() {
return pic_big;
}
public void setPic_big(String pic_big) {
this.pic_big = pic_big;
}
public String getPic_small() {
return pic_small;
}
public void setPic_small(String pic_small) {
this.pic_small = pic_small;
}
@SerializedName("name")
private String name;
public String getPic_square() {
return pic_square;
}
public void setPic_square(String pic_square) {
this.pic_square = pic_square;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "FacebookRisp [pic_square=" + pic_square + ", pic=" + pic + ", pic_big=" + pic_big + ", pic_small=" + pic_small + ", uid=" + uid
+ ", name=" + name + "]";
}
}
Why is @SerializedName not serializing fields when using Scala (works fine without annotation)
The answer looked like this...
case class BaseObject(
...
@(SerializedName@scala.annotation.meta.field)("dnsRecord") dnsEntries: Array[DNSEntry]
)
or even better
object GsonScala {
type SerializedName = com.google.gson.annotations.SerializedName @scala.annotation.meta.field
}
case class BaseObject(
...
@GsonScala.SerializedName("dnsRecord") dnsEntries: Array[DNSEntry]
)
Set Dynamic SerializedName annotation for Gson data class
You need to parse data as Map, the code below can help for that.
data class UploadLinksResponse(
@SerializedName("data")
@Expose
val data: Map<String, MediaLink>
)
data class MediaLink(
@SerializedName("id")
val id: String,
@SerializedName("bucket")
val bucket: String,
@SerializedName("location")
val uploadLocation: String
)
so you will able to fetch data as a map then your dynamic data will be key and related data will work as a value for that key, then you need to use the iterator to get data from key and value as per key's value.
Is the SerializedName annotation necessary if the local field name is to be the same as the incoming data's field name?
No, you do not need to provide the annotation if the variable name matches the input name. There are instances where you may want to name a variable something other than the input, and that is where you will need the annotation to help gson with the mapping.
Match dynamic serialized name with gson
You can use HashMap when you don't know the data key values in json
Use like below
@SerializedName("pages")
public HashMap pages
You can also use custom deserializer to make it work
https://futurestud.io/tutorials/gson-advanced-custom-deserialization-basics
Related Topics
How and Where Are Annotations Used in Java
Why Hypot() Function Is So Slow
What Is the Basic Purpose of @Serializedname Annotation in Android Using Gson
Hosting an Executable Within Android Application
Code Will Only Return 0.0, 0.0 Gps Coordinate While Throwing Nullpointerexception
Source Code Does Not Match the Bytecode' When Debugging on a Device
Change Icons of Checked and Unchecked for Checkbox for Android
Android Onconfigurationchanged Not Being Called
Problem Unmarshalling Parcelables
More Efficient Way for Pausing Loop Wanted
Collections Sort(List<T>,Comparator<? Super T>) Method Example
Capture and Log the Response Body
Exposed Beyond App Through Clipdata.Item.Geturi
Sort Data to Recyclerview Based on Latest Date from Firebase
Android Days Between Two Dates