Skip to content

Commit cf7a44d

Browse files
committed
Fixed handling of null and JsonValue as well as FasterXML#904
1 parent e30c612 commit cf7a44d

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
5858
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)
5959

6060
if (unboxed == null) {
61-
provider.findNullValueSerializer(null).serialize(null, gen, provider)
61+
provider.defaultSerializeNull(gen)
6262
return
6363
}
6464

@@ -77,8 +77,8 @@ internal sealed class ValueClassSerializer<T : Any>(t: Class<T>) : StdSerializer
7777
// As shown in the processing of the factory function, jsonValueGetter is always a static method.
7878
val jsonValue: Any? = staticJsonValueGetter.invoke(null, unboxed)
7979
jsonValue
80-
?.let { provider.findValueSerializer(it::class.java).serialize(it, gen, provider) }
81-
?: provider.findNullValueSerializer(null).serialize(null, gen, provider)
80+
?.let { provider.defaultSerializeValue(it, gen) }
81+
?: provider.defaultSerializeNull(gen)
8282
}
8383
}
8484

src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/GitHub873.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package com.fasterxml.jackson.module.kotlin.test.github
22

3+
import com.fasterxml.jackson.annotation.JsonValue
34
import com.fasterxml.jackson.module.kotlin.defaultMapper
45
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
56
import com.fasterxml.jackson.module.kotlin.readValue
67
import kotlin.test.Test
78

89
class GitHub873 {
10+
@JvmInline
11+
value class Person(
12+
val properties: Map<String, Any>,
13+
)
14+
15+
data class TimestampedPerson(
16+
val timestamp: Long,
17+
val person: Person,
18+
)
19+
920
@Test
1021
fun `should serialize value class`() {
1122

@@ -35,12 +46,18 @@ class GitHub873 {
3546
}
3647

3748
@JvmInline
38-
value class Person(
39-
val properties: Map<String, Any>,
40-
)
49+
value class MapAsJsonValue(val value: String) {
50+
@get:JsonValue
51+
val jsonValue get() = mapOf("key" to value)
52+
}
4153

42-
data class TimestampedPerson(
43-
val timestamp: Long,
44-
val person: Person,
45-
)
54+
data class JsonValueWrapper(val value: MapAsJsonValue)
55+
56+
@Test
57+
fun `JsonValue is serialized in the same way`() {
58+
val data = JsonValueWrapper(MapAsJsonValue("value"))
59+
val json = defaultMapper.writeValueAsString(data)
60+
61+
assert("""{"value":{"key":"value"}}""" == json)
62+
}
4663
}

0 commit comments

Comments
 (0)