retrofit icon indicating copy to clipboard operation
retrofit copied to clipboard

Deserializing and nulls

Open t0mm13b opened this issue 12 years ago • 1 comments

Just wondering, is this expected behaviour - have a look at this api for the type related_site as shown https://api.stackexchange.com/docs/types/related-site

There's a field indicated 'may be absent'.

Now, I have this class here...

public class RelatedSite{
    @SerializedName("api_site_parameter") public String apiSiteParameter = ""; // may be absent!
    @SerializedName("name") public String name = "";
    @SerializedName("relation") public Relation relation = Relation.Unknown;
    @SerializedName("site_url") public String siteUrl = "";
}

This apiSiteParameter is optional in the JSON record, in the class, it defaults to empty, upon retrieval via retrofit, this gets clobbered and ends up null, this can cause problems later on, any way of getting around that and preventing fields like that getting clobbered by default value replaced with null. Would the keyword transient declared, prevent this from happening? Any suggestions?

t0mm13b avatar Feb 06 '14 14:02 t0mm13b

Apologies for my blatant post as describing the issue!

Only way around this is to write a custom adapter for strings and use gson's registerTypeAdapter.

t0mm13b avatar Feb 06 '14 15:02 t0mm13b