With respect to Gson what is the difference between @Expose
and @SerializedName("stringValue")
?

- 143,130
- 81
- 406
- 459

- 4,397
- 12
- 45
- 74
-
@MarkKeen, hey mate the link is broken. – iCantC Feb 26 '20 at 10:52
-
updated link (another google search) http://websystique.com/java/json/gson-json-annotations-example/ – Mark Feb 26 '20 at 12:59
-
The default value will be true If we don't specify any value to the Expose annotation. – Chetan Gaikwad Jan 31 '22 at 16:51
4 Answers
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
}

- 7,790
- 4
- 18
- 31
-
1
-
Thank you. When we set serialization to false, after serialization we'll not see out username in json string. But one moment. Let's assume we set username deserialization to false. So when we will deserialize our json, we'll get username null. Am I right? – Hayk Mkrtchyan Oct 01 '19 at 12:34
-
-
The default value will be true If we don't specify any value to the Expose annotation. – Chetan Gaikwad Jan 31 '22 at 16:50
-
@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. Here's the documentation of @Expose
.

- 6,644
- 14
- 65
- 103

- 18,880
- 12
- 68
- 105
-
46
-
2
-
2@HamzehSoboh then it will depend upon the values passed in `@Expose`. (its complete syntax is `@Expose(serialize =
,deserialize = – ansh sachdeva Jul 26 '20 at 07:55)` , where true is default for both.
class Person{
String name;
String password;
}
suppose if i put i annotation Expose on top of a variable name or password without SerializedName, it will be serialized AS variable name
But if we put SerializedName like ("username") or ("password"),they will be serialized with that key
if Serialized
{"username":"trinadh","password":"hello"}
if not
{"name":"trinadh","password":"hello"}

- 1,089
- 15
- 19
Moreover, @Expose
comes with two boolean flags: deserialize
and serialize
, to allow skipping the field for one phase.