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 @Exposeand @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 serializationand deserialization.
serializationis converting objectinto json stringand deserializationis converting json stringinto object.
即使晚了我也想回答这个问题。为了解释它,我们必须知道什么是serialization和deserialization。
serialization正在转换object为json string并且deserialization正在转换json string为object.
Let's say we've Userclass 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 serializethis objectas 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
}
@Exposeis used to allow or disallow serializationand deserialization.
@Exposeis optional and it has two configuration parameters: serializeand deserialize. By default they're set to true.
To serializeand deserializewith @Exposewe create gson object like this
@Expose用于允许或禁止serialization和deserialization。
@Expose是可选的,它有两个配置参数:serialize和deserialize。默认情况下,它们设置为 true。为了serialize和deserialize同@Expose我们建立GSON对象这样
Gson gsonBuilder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
Below userNamewon't be deserialized. userName's value will be null.
下面userName不会被反序列化。用户名的值为null.
@SerializedName("name")
@Expose(deserialize = false)
private String userName;
Below userNamewon't be serialized.
下面userName不会连载。
@SerializedName("name")
@Expose(serialize = false)
private String userName;
Json string will be like this. Only userAgewill be deserialized.
Json 字符串将是这样的。只会userAge被反序列化。
{
"age":30
}
回答by Gavriel
回答by Shubham
Moreover, @Exposecomes with two boolean flags: deserializeand 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"}

