forked from FasterXML/jackson-module-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added test of interaction between Kotlin object singleton deserializa…
…tion and Jackson @JsonTypeInfo and @JsonIdentityInfo.
- Loading branch information
1 parent
643929e
commit 12ca397
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/GithubX.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |