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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:56:43  来源:igfitidea点击:

Gson: @Expose vs @SerializedName

javaandroidjsongson

提问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.

即使晚了我也想回答这个问题。为了解释它,我们必须知道什么是serializationdeserializationserialization正在转换objectjson string并且deserialization正在转换json stringobject.

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用于允许或禁止serializationdeserialization@Expose是可选的,它有两个配置参数:serializedeserialize。默认情况下,它们设置为 true。为了serializedeserialize@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

@SerializeNameis used to set the key that json object will include ,however @Exposeis used to decide whether the variable will be exposed for Serialisation and Deserialisation ,or not. Here'sthe documentation of @Expose.

@SerializeName用于设置 json 对象将包含的键,但@Expose用于决定是否将变量公开用于序列化和反序列化。@Expose.

回答by Shubham

Moreover, @Exposecomes with two boolean flags: deserializeand serialize, to allow skipping the field for one phase.

此外,@Expose带有两个布尔标志:deserializeserialize,允许跳过一个阶段的字段。

回答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"}