Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to deserialize alternate name with GSON, AutoValue, and Retrofit 2

I am using retrofit version 2.1.0 to deserialize JSON into pojos. A field in the pojo can be received under different names in the json. To deserialize the field correctly, I used the @serializedName annotation in the following way:

@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 the resulting JSON has the field under the key "title", Gson reads it correctly, but when the field is associated with the "name" key, it does not get read.

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

like image 482
Javier Ventajas Hernández Avatar asked Aug 06 '16 23:08

Javier Ventajas Hernández


1 Answers

I'm assuming you're using the com.ryanharter.auto.value:auto-value-gson plugin. 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 and you should then be able to deserialize alternate names.

like image 57
heenenee Avatar answered Oct 21 '22 21:10

heenenee