|
| 1 | +package com.fasterxml.jackson.module.kotlin.test.github |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo |
| 4 | +import com.fasterxml.jackson.annotation.JsonTypeInfo |
| 5 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators |
| 6 | +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper |
| 7 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 8 | +import com.fasterxml.jackson.module.kotlin.testPrettyWriter |
| 9 | +import kotlin.test.Test |
| 10 | +import kotlin.test.assertEquals |
| 11 | +import kotlin.test.assertSame |
| 12 | + |
| 13 | +class GitHub281 { |
| 14 | + @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type") |
| 15 | + @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator::class, property = "@id") |
| 16 | + interface Entity |
| 17 | + |
| 18 | + object NumberEntity : Entity |
| 19 | + |
| 20 | + data class NumberValue(val value: Int) { |
| 21 | + val entity = NumberEntity |
| 22 | + } |
| 23 | + |
| 24 | + private val json = """ |
| 25 | + [ { |
| 26 | + "value" : 10, |
| 27 | + "entity" : { |
| 28 | + "@type" : ".GitHub281${'$'}NumberEntity", |
| 29 | + "@id" : 1 |
| 30 | + } |
| 31 | + }, { |
| 32 | + "value" : 11, |
| 33 | + "entity" : 1 |
| 34 | + } ] |
| 35 | + """.trimIndent() |
| 36 | + |
| 37 | + @Test |
| 38 | + fun `test writing involving type, id and object`() { |
| 39 | + val input = listOf(NumberValue(10), NumberValue(11)) |
| 40 | + |
| 41 | + val output = jacksonObjectMapper() |
| 42 | + .testPrettyWriter() |
| 43 | + .writeValueAsString(input) |
| 44 | + |
| 45 | + assertEquals(json, output) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `test reading involving type, id and object`() { |
| 50 | + val output = jacksonObjectMapper().readValue<List<NumberValue>>(json) |
| 51 | + |
| 52 | + assertEquals(2, output.size) |
| 53 | + val (a, b) = output |
| 54 | + assertSame(NumberEntity::class.java, a.entity.javaClass) |
| 55 | + assertSame(NumberEntity::class.java, b.entity.javaClass) |
| 56 | + assertEquals(10, a.value) |
| 57 | + assertEquals(11, b.value) |
| 58 | + } |
| 59 | +} |
0 commit comments