8

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.

Sunstrike
  • 456
  • 1
  • 6
  • 22

1 Answers1

2

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"}
Ji Fang
  • 3,288
  • 1
  • 21
  • 18