I'm trying to deserialize a collection, and runnin...
# kotson
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
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
for this?
Have you tried
Copy code
val gson = GsonBuilder()
Yeah, I did. I get the same behavior on deserialization. This is existing data that was serialized before the
field was added…
If I don’t specify the
, just a
, 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