-
-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
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
117 changes: 117 additions & 0 deletions
117
...le/kotlin/kogeraIntegration/deser/valueClass/mapKey/WithoutCustomDeserializeMethodTest.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,117 @@ | ||
package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey | ||
|
||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonMappingException | ||
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException | ||
import com.fasterxml.jackson.databind.module.SimpleModule | ||
import com.fasterxml.jackson.module.kotlin.defaultMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import java.lang.reflect.InvocationTargetException | ||
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer | ||
|
||
class WithoutCustomDeserializeMethodTest { | ||
companion object { | ||
val throwable = IllegalArgumentException("test") | ||
} | ||
|
||
@Nested | ||
inner class DirectDeserialize { | ||
@Test | ||
fun primitive() { | ||
val result = defaultMapper.readValue<Map<Primitive, String?>>("""{"1":null}""") | ||
assertEquals(mapOf(Primitive(1) to null), result) | ||
} | ||
|
||
@Test | ||
fun nonNullObject() { | ||
val result = defaultMapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""") | ||
assertEquals(mapOf(NonNullObject("foo") to null), result) | ||
} | ||
|
||
@Test | ||
fun nullableObject() { | ||
val result = defaultMapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""") | ||
assertEquals(mapOf(NullableObject("bar") to null), result) | ||
} | ||
} | ||
|
||
data class Dst( | ||
val p: Map<Primitive, String?>, | ||
val nn: Map<NonNullObject, String?>, | ||
val n: Map<NullableObject, String?> | ||
) | ||
|
||
@Test | ||
fun wrapped() { | ||
val src = """ | ||
{ | ||
"p":{"1":null}, | ||
"nn":{"foo":null}, | ||
"n":{"bar":null} | ||
} | ||
""".trimIndent() | ||
val result = defaultMapper.readValue<Dst>(src) | ||
val expected = Dst( | ||
mapOf(Primitive(1) to null), | ||
mapOf(NonNullObject("foo") to null), | ||
mapOf(NullableObject("bar") to null) | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
|
||
@JvmInline | ||
value class HasCheckConstructor(val value: Int) { | ||
init { | ||
if (value < 0) throw throwable | ||
} | ||
} | ||
|
||
@Test | ||
fun callConstructorCheckTest() { | ||
val e = assertThrows<InvocationTargetException> { | ||
defaultMapper.readValue<Map<HasCheckConstructor, String?>>("""{"-1":null}""") | ||
} | ||
assertTrue(e.cause === throwable) | ||
} | ||
|
||
data class Wrapped(val first: String, val second: String) { | ||
class KeyDeserializer : JacksonKeyDeserializer() { | ||
override fun deserializeKey(key: String, ctxt: DeserializationContext) = | ||
key.split("-").let { Wrapped(it[0], it[1]) } | ||
} | ||
} | ||
|
||
@JvmInline | ||
value class Wrapper(val w: Wrapped) | ||
|
||
@Test | ||
fun wrappedCustomObject() { | ||
// If a type that cannot be deserialized is specified, the default is an error. | ||
val thrown = assertThrows<JsonMappingException> { | ||
defaultMapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""") | ||
} | ||
assertTrue(thrown.cause is InvalidDefinitionException) | ||
|
||
val mapper = jacksonObjectMapper() | ||
.registerModule( | ||
object : SimpleModule() { | ||
init { addKeyDeserializer(Wrapped::class.java, Wrapped.KeyDeserializer()) } | ||
} | ||
) | ||
|
||
val result = mapper.readValue<Map<Wrapper, String?>>("""{"foo-bar":null}""") | ||
val expected = mapOf(Wrapper(Wrapped("foo", "bar")) to null) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...kogeraIntegration/deser/valueClass/mapKey/keyDeserializer/SpecifiedForObjectMapperTest.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,70 @@ | ||
package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer | ||
|
||
import com.fasterxml.jackson.databind.module.SimpleModule | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NonNullObject | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.NullableObject | ||
import com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.Primitive | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
class SpecifiedForObjectMapperTest { | ||
companion object { | ||
val mapper = jacksonObjectMapper().apply { | ||
val module = SimpleModule().apply { | ||
this.addKeyDeserializer(Primitive::class.java, Primitive.KeyDeserializer()) | ||
this.addKeyDeserializer(NonNullObject::class.java, NonNullObject.KeyDeserializer()) | ||
this.addKeyDeserializer(NullableObject::class.java, NullableObject.KeyDeserializer()) | ||
} | ||
this.registerModule(module) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class DirectDeserialize { | ||
@Test | ||
fun primitive() { | ||
val result = mapper.readValue<Map<Primitive, String?>>("""{"1":null}""") | ||
assertEquals(mapOf(Primitive(101) to null), result) | ||
} | ||
|
||
@Test | ||
fun nonNullObject() { | ||
val result = mapper.readValue<Map<NonNullObject, String?>>("""{"foo":null}""") | ||
assertEquals(mapOf(NonNullObject("foo-deser") to null), result) | ||
} | ||
|
||
@Test | ||
fun nullableObject() { | ||
val result = mapper.readValue<Map<NullableObject, String?>>("""{"bar":null}""") | ||
assertEquals(mapOf(NullableObject("bar-deser") to null), result) | ||
} | ||
} | ||
|
||
data class Dst( | ||
val p: Map<Primitive, String?>, | ||
val nn: Map<NonNullObject, String?>, | ||
val n: Map<NullableObject, String?> | ||
) | ||
|
||
@Test | ||
fun wrapped() { | ||
val src = """ | ||
{ | ||
"p":{"1":null}, | ||
"nn":{"foo":null}, | ||
"n":{"bar":null} | ||
} | ||
""".trimIndent() | ||
val result = mapper.readValue<Dst>(src) | ||
val expected = Dst( | ||
mapOf(Primitive(101) to null), | ||
mapOf(NonNullObject("foo-deser") to null), | ||
mapOf(NullableObject("bar-deser") to null) | ||
) | ||
|
||
assertEquals(expected, result) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...Integration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForClassTest.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,37 @@ | ||
package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation | ||
|
||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | ||
import com.fasterxml.jackson.module.kotlin.defaultMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer | ||
|
||
class SpecifiedForClassTest { | ||
@JsonDeserialize(keyUsing = Value.KeyDeserializer::class) | ||
@JvmInline | ||
value class Value(val v: Int) { | ||
class KeyDeserializer : JacksonKeyDeserializer() { | ||
override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100) | ||
} | ||
} | ||
|
||
@Test | ||
fun directDeserTest() { | ||
val result = defaultMapper.readValue<Map<Value, String?>>("""{"1":null}""") | ||
|
||
assertEquals(mapOf(Value(101) to null), result) | ||
} | ||
|
||
data class Wrapper(val v: Map<Value, String?>) | ||
|
||
@Test | ||
fun paramDeserTest() { | ||
val mapper = jacksonObjectMapper() | ||
val result = mapper.readValue<Wrapper>("""{"v":{"1":null}}""") | ||
|
||
assertEquals(Wrapper(mapOf(Value(101) to null)), result) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...egration/deser/valueClass/mapKey/keyDeserializer/byAnnotation/SpecifiedForPropertyTest.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,28 @@ | ||
package com.fasterxml.jackson.module.kotlin.kogeraIntegration.deser.valueClass.mapKey.keyDeserializer.byAnnotation | ||
|
||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import com.fasterxml.jackson.databind.KeyDeserializer as JacksonKeyDeserializer | ||
|
||
class SpecifiedForPropertyTest { | ||
@JvmInline | ||
value class Value(val v: Int) { | ||
class KeyDeserializer : JacksonKeyDeserializer() { | ||
override fun deserializeKey(key: String, ctxt: DeserializationContext) = Value(key.toInt() + 100) | ||
} | ||
} | ||
|
||
data class Wrapper(@JsonDeserialize(keyUsing = Value.KeyDeserializer::class) val v: Map<Value, String?>) | ||
|
||
@Test | ||
fun paramDeserTest() { | ||
val mapper = jacksonObjectMapper() | ||
val result = mapper.readValue<Wrapper>("""{"v":{"1":null}}""") | ||
|
||
assertEquals(Wrapper(mapOf(Value(101) to null)), result) | ||
} | ||
} |