Skip to content

JsonValue set in value class does not work #530

@k163377

Description

@k163377

Describe the bug
SSIA

This issue is related to #199 .

To Reproduce

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import org.junit.Test
import kotlin.test.assertEquals

class GitHubXXX {
    // At the moment, the output is the same with or without `JsonValue`,
    // but this pattern is included in the test case in case the option to default to a serialization method that
    // does not `unbox` is introduced in the future.
    @JvmInline value class Foo(@get:JsonValue val value: Int)

    @JvmInline value class Bar(val value: Int) {
        @get:JsonValue val jsonValue: String get() = this.toString()
    }

    interface JsonValueGetter { @get:JsonValue val jsonValue: String get() = this.toString() }
    @JvmInline value class Baz(val value: Int): JsonValueGetter

    @JvmInline value class Qux(val value: Int): JsonValueGetter

    @JvmInline value class Corge(val value: Int) {
        @get:JsonValue val jsonValue: String get() = this.toString()
    }

    data class Data<T : Any>(
        val foo1: Foo,
        val foo2: Foo?,
        val bar1: Bar,
        val bar2: Bar?,
        val baz1: Baz,
        val baz2: Baz?,
        val qux1: JsonValueGetter,
        val qux2: JsonValueGetter?,
        val corge1: T,
        val corge2: T?
    )

    @Test
    fun test() {
        val writer = jacksonMapperBuilder().build().writerWithDefaultPrettyPrinter()

        assertEquals(
            """
                {
                  "foo1" : 1,
                  "foo2" : 2,
                  "bar1" : "Bar(value=3)",
                  "bar2" : "Bar(value=4)",
                  "baz1" : "Baz(value=5)",
                  "baz2" : "Baz(value=6)",
                  "qux1" : "Qux(value=7)",
                  "qux2" : "Qux(value=8)",
                  "corge1" : "Corge(value=9)",
                  "corge2" : "Corge(value=10)"
                }
            """.trimIndent(),
            writer.writeValueAsString(
                Data(
                    Foo(1), Foo(2),
                    Bar(3), Bar(4),
                    Baz(5), Baz(6),
                    Qux(7), Qux(8),
                    Corge(9), Corge(10)
                )
            )
        )
    }
}

At the moment, actual as follows.

{
  "foo1" : 1,
  "foo2" : 2,
  "bar1" : 3,
  "bar2" : 4,
  "baz1" : 5,
  "baz2" : 6,
  "qux1" : 7,
  "qux2" : 8,
  "corge1" : 9,
  "corge2" : 10
}

Versions
This commit: 9cd05ee

Additional context
I am going to fix this problem myself.
I would appreciate it if you could point out any omissions in the test cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions