Skip to content

Commit 2c41387

Browse files
committed
Added a test case that uses a custom KeyDeserializer
1 parent 333c334 commit 2c41387

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.mapKey.keyDeserializer
2+
3+
import com.fasterxml.jackson.databind.module.SimpleModule
4+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
5+
import io.github.projectmapk.jackson.module.kogera.readValue
6+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.NonNullObject
7+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.NullableObject
8+
import io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.Primitive
9+
import org.junit.jupiter.api.Assertions.assertEquals
10+
import org.junit.jupiter.api.Nested
11+
import org.junit.jupiter.api.Test
12+
13+
class SpecifiedForObjectMapperTest {
14+
companion object {
15+
val mapper = jacksonObjectMapper().apply {
16+
val module = SimpleModule().apply {
17+
this.addKeyDeserializer(Primitive::class.java, Primitive.KeyDeserializer())
18+
this.addKeyDeserializer(NonNullObject::class.java, NonNullObject.KeyDeserializer())
19+
this.addKeyDeserializer(NullableObject::class.java, NullableObject.KeyDeserializer())
20+
}
21+
this.registerModule(module)
22+
}
23+
}
24+
25+
@Nested
26+
inner class DirectDeserialize {
27+
@Test
28+
fun primitive() {
29+
val result = mapper.readValue<Map<Primitive, String?>>("""{"1":null}""")
30+
assertEquals(mapOf(Primitive(101) to null), result)
31+
}
32+
33+
@Test
34+
fun nonNullObject() {
35+
val result = mapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""")
36+
assertEquals(mapOf(NonNullObject("foo-deser") to null), result)
37+
}
38+
39+
@Test
40+
fun nullableObject() {
41+
val result = mapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""")
42+
assertEquals(mapOf(NullableObject("bar-deser") to null), result)
43+
}
44+
}
45+
46+
data class Dst(
47+
val p: Map<Primitive, String?>,
48+
val nn: Map<NonNullObject, String?>,
49+
val n: Map<NullableObject, String?>
50+
)
51+
52+
@Test
53+
fun wrapped() {
54+
val src = """
55+
{
56+
"p":{"1":null},
57+
"nn":{"foo":null},
58+
"n":{"bar":null}
59+
}
60+
""".trimIndent()
61+
val result = mapper.readValue<Dst>(src)
62+
val expected = Dst(
63+
mapOf(Primitive(101) to null),
64+
mapOf(NonNullObject("foo-deser") to null),
65+
mapOf(NullableObject("bar-deser") to null)
66+
)
67+
68+
assertEquals(expected, result)
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation
2+
3+
import com.fasterxml.jackson.databind.DeserializationContext
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
5+
import io.github.projectmapk.jackson.module.kogera.defaultMapper
6+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
7+
import io.github.projectmapk.jackson.module.kogera.readValue
8+
import org.junit.jupiter.api.Assertions.assertEquals
9+
import org.junit.jupiter.api.Test
10+
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer
11+
12+
class SpecifiedForClassTest {
13+
@JsonDeserialize(keyUsing = Value.KeyDeserializer::class)
14+
@JvmInline
15+
value class Value(val v: Int) {
16+
class KeyDeserializer : JacksonKeyDeserializer() {
17+
override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100)
18+
}
19+
}
20+
21+
@Test
22+
fun directDeserTest() {
23+
val result = defaultMapper.readValue<Map<Value, String?>>("""{"1":null}""")
24+
25+
assertEquals(mapOf(Value(101) to null), result)
26+
}
27+
28+
data class Wrapper(val v: Map<Value, String?>)
29+
30+
@Test
31+
fun paramDeserTest() {
32+
val mapper = jacksonObjectMapper()
33+
val result = mapper.readValue<Wrapper>("""{"v":{"1":null}}""")
34+
35+
assertEquals(Wrapper(mapOf(Value(101) to null)), result)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.projectmapk.jackson.module.kogera.zIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation
2+
3+
import com.fasterxml.jackson.databind.DeserializationContext
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
5+
import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper
6+
import io.github.projectmapk.jackson.module.kogera.readValue
7+
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Test
9+
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer
10+
11+
class SpecifiedForPropertyTest {
12+
@JvmInline
13+
value class Value(val v: Int) {
14+
class KeyDeserializer : JacksonKeyDeserializer() {
15+
override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100)
16+
}
17+
}
18+
19+
data class Wrapper(@JsonDeserialize(keyUsing = Value.KeyDeserializer::class) val v: Map<Value, String?>)
20+
21+
@Test
22+
fun paramDeserTest() {
23+
val mapper = jacksonObjectMapper()
24+
val result = mapper.readValue<Wrapper>("""{"v":{"1":null}}""")
25+
26+
assertEquals(Wrapper(mapOf(Value(101) to null)), result)
27+
}
28+
}

0 commit comments

Comments
 (0)