Gson: Kotlin null safety

Created on 6 Sep 2017  路  5Comments  路  Source: google/gson

Hi,
When there is a field with a null value, it is written to the notnull field in the Kotlin object. How to make sure that the default value is not overwritten by null

Most helpful comment

Hi, just curious what's the reason behind not supporting/planning to support it in Gson? does it require a huge refactoring or something?

All 5 comments

You would need to write a custom TypeAdapter which used Kotlin's reflection library to call the primary constructor and set values via properties. Moshi has an adapter you can port to Gson if you want: https://github.com/square/moshi/blob/master/kotlin/src/main/java/com/squareup/moshi/KotlinJsonAdapter.kt, but we won't be adding one to Gson.

Thank you

Hi, does anybody know about solution for Gson? I found https://github.com/sargunv/gson-kotlin but it seems to have some flaws with primitive types (see issue 3), also, reusing full reflection seems quite heavy. Or is it better to switch to other library?

Hi, just curious what's the reason behind not supporting/planning to support it in Gson? does it require a huge refactoring or something?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  路  15Comments

RobMans426 picture RobMans426  路  20Comments

GoogleCodeExporter picture GoogleCodeExporter  路  15Comments

GoogleCodeExporter picture GoogleCodeExporter  路  31Comments

danieleguiducci picture danieleguiducci  路  34Comments