Skip to content

Commit

Permalink
Added test of interaction between Kotlin object singleton deserializa…
Browse files Browse the repository at this point in the history
…tion and Jackson @JsonTypeInfo and @JsonIdentityInfo.
  • Loading branch information
george-hawkins committed Dec 19, 2019
1 parent 643929e commit 12ca397
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.fasterxml.jackson.module.kotlin.test

import com.fasterxml.jackson.annotation.JsonIdentityInfo
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.ObjectIdGenerators
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type")
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator::class, property = "@id")
interface Entity

object NumberEntity : Entity

data class NumberValue(val value: Int) {
val entity = NumberEntity
}

class TestGithubX {
private val json = """
[ {
"value" : 10,
"entity" : {
"@type" : ".NumberEntity",
"@id" : 1
}
}, {
"value" : 11,
"entity" : 1
} ]
""".trimIndent()

@Test
fun `test writing involving type, id and object`() {
val input = listOf(NumberValue(10), NumberValue(11))

val output = jacksonObjectMapper()
.writerFor(jacksonTypeRef<List<NumberValue>>())
.withDefaultPrettyPrinter()
.writeValueAsString(input)

assertEquals(json, output)
}

@Test
fun `test reading involving type, id and object`() {
val output = jacksonObjectMapper().readValue<List<NumberValue>>(json)

assertEquals(2, output.size)
val (a, b) = output
assertSame(NumberEntity::class.java, a.entity.javaClass)
assertSame(NumberEntity::class.java, b.entity.javaClass)
assertEquals(10, a.value)
assertEquals(11, b.value)
}
}

0 comments on commit 12ca397

Please sign in to comment.