Search code examples
androidgsonjson-deserialization

Android Gson - deserializing null vs absent values


I'd like to understand the difference with a server returning value verses that value being completely absent in the response.

lets take at what i have so far:

data class MyApiResponse(@SerializedName("name") val name: String,
                                  @SerializedName("address") val address: String,
                                  @SerializedName("max_time") val maxTime: Double? = null //this field  might  BE  COMPLETELY absent in response, what will  happen  here ? 
)

regarding the maxTime value, if the value is COMPLETELY absent from server response, will the app crash or will the value be null ?

the issue is im trying to distinguish between server sending

max_time: null vs it being completely absent ,how does gson handle this ?


Solution

  • In both cases it will be null. Might look here for more info: Gson optional and required fields