moshi: default value in kotlin data class not work when server send null for that value
json:
{
"percentChange": null,
"change": "-2500.00",
"value": "130000",
"name": null
}
data class:
@JsonClass(generateAdapter = true)
data class Reference(
val name: String? = "-",
val value: Double,
val change: Double,
val percentChange: Double? = -10.0
)
but data for name and percentChange is null that should “-” for name and “-10.0” for percentChange. if server not send name and percentChange, default value work, but if send that null default value not work! I use converter-moshi:2.4.0 and retrofit:2.4.0
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 15 (1 by maintainers)
how about this?
@NightlyNexus Well, this is kind of good for a small module, but imagine something like 50 field modules with the server can send nulls for most of them (30 for example). Then it can get really ugly really fast with one backing up field (with the default value) for each nullable field, right?
As I was asking @swankjesse, I really think this should be approached from the parser “Moshi” side, not the models itself, the models already defined their defaults as per the language “Kotlin” specs.
Now I understand that it was a design decision and that’s ok, I was wondering if you can point me how to extend the auto-gen adapter so I can have an auto-gen adapter like the one you guys create but with this added functionality. 😃
This doesn’t work for data classes