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

2

6

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?

Turgor answered 6/8, 2016 at 23:13 Comment(0)
U
2

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.

Underestimate answered 11/8, 2016 at 6:38 Comment(0)
W
-1

Seem the problem is related to Parcel. You might want take a look at this parceler

@AutoValue
@Parcel
public abstract class Media {
    @ParcelProperty("title") public abstract String title();

}
Winwaloe answered 11/8, 2016 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.