I'm trying to deserialize a collection, and runnin...
# kotson
m
I'm trying to deserialize a collection, and running into an issue with null. I have the following data class:
Copy code
data class DocumentMetadata(val documentId: String, val documentType: DocumentType = DocumentType.SKETCH, val owners: MutableSet<String> = mutableSetOf())
and the following string representation:
Copy code
{
  "documentId": "__healthCheck",
  "documentType": "SKETCH"
}
I'm trying to just do
val meta:DocumentMetadata = Gson().fromJson(metaString)
, but owners comes up as
null
in the deserialized type, instead of the expected empty set of strings. Is there some setting to get the default data class values for any elements not in the input string, or will I have to do a
TypeAdapter
for this?
f
Have you tried
serializeNulls()
?
Copy code
val gson = GsonBuilder()
    .serializeNulls()
    .create()
m
Yeah, I did. I get the same behavior on deserialization. This is existing data that was serialized before the
owners
field was added…
If I don’t specify the
documentType
, just a
documentId
, I also get that as null, so I guess this is not a problem that is unique to collections. But the bottom line remains the same — if fields are not specified in the string, I would like them to have the data class default values. Can I do that w/o a
TypeAdapter
?