google / google/gson

Feature Request: Add deserializeNulls

Open
#1,444 6 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

There's already a serializeNulls option, and with the introduction of Kotlin I think it would be good to have a deserializeNulls, right now serializeNulls is only a method that sets a boolean to true, but probably that can be a method that receives a boolean, this way you can reuse the same builder to construct different gsons, the reasoning behind adding deserializeNulls is that since you can mark some fields in Kotlin as non null you expect to be able to use the field with no problem, but if the field in the json string comes as null then you will get an exception at runtime when using that property since gson set that field to null, this is the same as using NonNull annotations for Java where you can "safely use the field", there are some cases where someone can think of something being null as a default case for example if a class has a list of items it's technically the same thing having a null or an empty list in both cases you have no items.

Right now if you define a class with a default constructor and default values for it's fields, gson will make the field null if the json string has it as null, but since you already defined a default value this will "break" the null safety you had, no matter if it's Java or Kotlin. We can always define type adapters for this, but why would all the consumers of the library have to do that if gson could only provide a deserializeNulls option?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.