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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With