How to change @SerializedName, from GSON, attribute in extended class

I have a class like this:

class A { @Expose @SerializedName("a_id") private String id; } 

Now I want to create a class B that extends A, but in B I want to change the SerializedName id to "b_id". Is there any way to do this?

+4
source share
3 answers

It is impossible out of the box. You will need to write a custom adapter type. See Custom Serialization and Deserialization in the Gson User Guide.

+2
source

apply @SerializedName ("b_id") to the installer in B

class B {private String id;

 @SerializedName("b_id") private void setId(String id); 

}

0
source

From Gson 2.4 you can use alternate names. In your case, you can do it like this:

 class A { @Expose @SerializedName(value = "a_id", alternate="b_id") private String id; } 

The format is as follows:

 @SerializedName(value="name1", alternate={"name2", "name3"}) 

More details here .

0
source

Source: https://habr.com/ru/post/1415663/


All Articles