Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin-moshi codegen inheritance with default values

If I have an open class and inherited data class from it, Kotlin-moshi codegen skip default value. Is this intended behaviour? How to make moshi-kotlin parse all values including default from superclass?

@JsonClass(generateAdapter = true)
data class B(val bar: String) : A(foo = "foo")

@JsonClass(generateAdapter = true)
open class A(val foo: String)

val b = B("bar")

adapter.toJson(b) prints {"bar":"bar"} without channel field.

like image 892
Sunstrike Avatar asked Apr 11 '19 09:04

Sunstrike


1 Answers

Make your superclass property mutable can solve your issue.

@JsonClass(generateAdapter = true)
data class B(val bar: String) : A(foo = "foo")

@JsonClass(generateAdapter = true)
open class A(var foo: String)

Output

{"bar":"bar","foo":"foo"}
like image 186
Ji Fang Avatar answered Jan 19 '23 07:01

Ji Fang