1

I want to serialize nulls for a specific field in GSON. I have looked at the solution provided here as well as several others which fail when setting:

 minifyEnabled true
 shrinkResources true

I have been trying various (failed attempts) at modifying the answers given in the link, but I have not had any luck.

Given my object looks as follows:

data class EmojiType(
    @SerializeNull
    var value: String?
) 

When I create a post request and pass null, I expect my object to look as follows:

{
   "value":null
}

However, as mentioned, when minifyEnabled and shrinkResources are set to true, my object ends up looking like this:

{
   "a":null
}

How can I avoid this and retain the field name of value?

Display name
  • 730
  • 2
  • 8
  • 23
  • Where is `"a"` coming from? I doubt that it's coming from GSON. – CryptoFool Mar 07 '21 at 01:01
  • @CryptoFool I believe this is occurring due to the obfuscation process. When setting `minifyEnabled` and `shrinkResources` to false, my "value" field stays intact, it's only when setting these values to true, that I get the "a". This is based on implementing the highest voted answer on the link I provided in my question. – Display name Mar 07 '21 at 01:08
  • Ah. Ok. I missed the obfuscation part of your question until now. – CryptoFool Mar 07 '21 at 03:12

1 Answers1

2

I faced a similar issue with Gson, where specifying the SerializedName got it to work:

data class EmojiType(
    @SerializedName("value")
    var value: String?
) 
Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33