Skip to content

Properties marked with @EncodeDefault in data objects are ignored #3214

Description

@RaySmith-ttc

To Reproduce

@Serializable
data object Foo {

    @EncodeDefault
    val bar: String = "value"
}
@Test
fun testFoo() {
    val json = Json.encodeToString(Foo)
    assertEquals("""{"bar":"value"}""", json) // failed, result is {}
}

Expected behavior
All properties annotated with @EncodeDefault must be present in the json, just as they are in the data classes:

@Serializable
data class FooDataClass(val bar1: String) {

    @EncodeDefault
    val bar2: String = "value2"
}
@Test
fun testFooDataClass() {
    val json = Json.encodeToString(FooDataClass("value1"))
    assertEquals("""{"bar1":"value1","bar2":"value2"}""", json) // passed
}

Environment

  • Kotlin version: 2.3.10
  • Library version: 1.11.0
  • Kotlin platforms: JVM
  • Gradle version: 9.6.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions