https://kotlinlang.org logo
#jackson-kotlin
Title
# jackson-kotlin
r

reik.schatz

03/28/2019, 11:41 AM
Copy code
import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

data class Container(@JsonUnwrapped val row: Map<String, Any?>)

class JsonUnwrappedTest {

    @Test
    fun `should unwrap`() {
        val row = Container(row = mapOf("foo" to "bar"))

        val objectMapper = ObjectMapper()
        objectMapper.registerModule(KotlinModule())

        val json = objectMapper.writeValueAsString(row)
        assertThat(json).isEqualTo("""{"foo": "bar"}""") // fails
    }
}
d

diesieben07

03/28/2019, 11:48 AM
This has nothing to do with Kotlin.
JsonUnwrapped
only works for POJOs, not Maps. You can use a combination of
JsonAnyGetter
and
JsonAnySetter
to achieve something similar, seen here: https://stackoverflow.com/a/18043785
r

reik.schatz

03/28/2019, 12:27 PM
@diesieben07 oh, thanks a lot. I wasn’t aware of this
83 Views