【问题标题】:Kotlin-moshi codegen inheritance with default values带有默认值的 Kotlin-moshi 代码生成继承
【发布时间】:2019-09-01 21:15:12
【问题描述】:

如果我有一个开放类并从中继承数据类,Kotlin-moshi codegen 会跳过默认值。这是预期的行为吗?如何让 moshi-kotlin 解析所有值,包括超类中的默认值?

@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) 打印 {"bar":"bar"} 没有通道字段。

【问题讨论】:

    标签: android kotlin moshi


    【解决方案1】:

    让你的超类属性可变可以解决你的问题。

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

    输出

    {"bar":"bar","foo":"foo"}
    

    【讨论】:

      猜你喜欢
      • 2020-02-18
      • 2023-03-30
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多