Cannot Deserialize Alternate Name Using GSON, AutoValue, and Retrofit 2

I am using modified version 2.1.0 to deserialize JSON in pojos. A field in pojo can be obtained under different names in json. To deserialize the field correctly, I used the @serializedName annotation as follows:

@AutoValue public abstract class Media implements Parcelable { @SerializedName(value = "title", alternate = {"name"}) public abstract String title(); // More fields and code 

However, for some reason, when there is a field under the key "header" as a result of JSON, Gson reads it correctly, but when the field is associated with a "name", it is not read.

How can I get GSON to recognize an alternate name during deserialization?

+6
source share
2 answers

I assume that you are using the com.ryanharter.auto.value:auto-value-gson plugin com.ryanharter.auto.value:auto-value-gson . Support for alternate serialized names was not added until version 0.4.0. Update to com.ryanharter.auto.value:auto-value-gson:0.4.2 , after which you can deserialize the alternative names.

+1
source

The problem seems to be related to the parcel. You can take a look at this parceler

 @AutoValue @Parcel public abstract class Media { @ParcelProperty("title") public abstract String title(); } 
-one
source

All Articles