Java Gson:@Expose 与 @SerializedName
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34752200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gson: @Expose vs @SerializedName
提问by Ahmed
With respect to Gson what is the difference between @Expose
and @SerializedName("stringValue")
?
关于 Gson@Expose
和之间有什么区别 @SerializedName("stringValue")
?
采纳答案by Bek
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
.
即使晚了我也想回答这个问题。为了解释它,我们必须知道什么是serialization
和deserialization
。
serialization
正在转换object
为json string
并且deserialization
正在转换json string
为object
.
Let's say we've User
class with no annotations.
假设我们User
有没有注释的类。
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
而我们serialize
这个object
如下
User user = new User("Ahmed", 30);
Gson gson = new Gson();
String jsonString = gson.toJson(user);
Json string will be like this
Json 字符串将是这样的
{
"userName":"Ahmed",
"userAge":30
}
If we add annotation @SerializedName
如果我们添加注释 @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
Json 字符串将是这样的
{
"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
@Expose
用于允许或禁止serialization
和deserialization
。
@Expose
是可选的,它有两个配置参数:serialize
和deserialize
。默认情况下,它们设置为 true。为了serialize
和deserialize
同@Expose
我们建立GSON对象这样
Gson gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Below userName
won't be deserialized. userName's value will be null
.
下面userName
不会被反序列化。用户名的值为null
.
@SerializedName("name")
@Expose(deserialize = false)
private String userName;
Below userName
won't be serialized.
下面userName
不会连载。
@SerializedName("name")
@Expose(serialize = false)
private String userName;
Json string will be like this. Only userAge
will be deserialized.
Json 字符串将是这样的。只会userAge
被反序列化。
{
"age":30
}
回答by Gavriel
回答by Shubham
Moreover, @Expose
comes with two boolean flags: deserialize
and serialize
, to allow skipping the field for one phase.
此外,@Expose
带有两个布尔标志:deserialize
和serialize
,允许跳过一个阶段的字段。
回答by Trinadh Koya
class Person{
String name;
String password;
}
suppose if i put i annotation Exposeon top of a variable name or password without SerializedName, it will be serialized AS variable name
假设如果我在没有SerializedName 的情况下将我的注释Expose放在变量名或密码之上,它将被序列化为变量名
But if we put SerializedNamelike ("username") or ("password"),they will be serialized with that key
但是如果我们把SerializedName像 (" username") 或 (" password") 一样,它们将用那个键序列化
if Serialized
如果序列化
{"username":"trinadh","password":"hello"}
if not
如果不
{"name":"trinadh","password":"hello"}