Fixed an issue where NullValueSerializer was not being applied.
MODIFY
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
src/main/kotlin/com/fasterxml/jackson/module/kotlin/KotlinSerializers.kt
@@ -45,7 +45,7 @@ object ValueClassUnboxSerializer : StdSerializer<Any>(Any::class.java) {
val unboxed = value::class.java.getMethod("unbox-impl").invoke(value)
if (unboxed == null) {
- gen.writeNull()
+ provider.findNullValueSerializer(null).serialize(unboxed, gen, provider)
return
}

MODIFY
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt
src/test/kotlin/com/fasterxml/jackson/module/kotlin/test/github/Github464.kt
@@ -23,11 +23,6 @@ class Github464 {
}
}
- @Suppress("UNCHECKED_CAST")
- private val writer: ObjectWriter = jacksonObjectMapper()
- .apply { serializerProvider.setNullKeySerializer(NullValueClassKeySerializer as JsonSerializer<Any?>) }
- .writerWithDefaultPrettyPrinter()
-
@JvmInline
value class ValueClass(val value: Int?)
data class WrapperClass(val inlineField: ValueClass)
@@ -44,13 +39,11 @@ class Github464 {
val garply: Map<ValueClass, ValueClass?>
)
- @Test
- fun test() {
- val zeroValue = ValueClass(0)
- val oneValue = ValueClass(1)
- val nullValue = ValueClass(null)
+ private val zeroValue = ValueClass(0)
+ private val oneValue = ValueClass(1)
+ private val nullValue = ValueClass(null)
- val target = Poko(
+ private val target = Poko(
foo = zeroValue,
bar = null,
baz = zeroValue,
@@ -61,6 +54,13 @@ class Github464 {
garply = mapOf(zeroValue to zeroValue, oneValue to null, nullValue to nullValue)
)
+ @Test
+ fun test() {
+ @Suppress("UNCHECKED_CAST")
+ val writer: ObjectWriter = jacksonObjectMapper()
+ .apply { serializerProvider.setNullKeySerializer(NullValueClassKeySerializer as JsonSerializer<Any?>) }
+ .writerWithDefaultPrettyPrinter()
+
assertEquals(
"""
{
@@ -83,6 +83,44 @@ class Github464 {
writer.writeValueAsString(target)
)
}
+
+ object NullValueSerializer : StdSerializer<Any>(Any::class.java) {
+ override fun serialize(value: Any?, gen: JsonGenerator, provider: SerializerProvider) {
+ gen.writeString("null-value")
+ }
+ }
+
+ @Test
+ fun nullValueSerializerTest() {
+ @Suppress("UNCHECKED_CAST")
+ val writer = jacksonObjectMapper()
+ .apply {
+ serializerProvider.setNullKeySerializer(NullValueClassKeySerializer as JsonSerializer<Any?>)
+ serializerProvider.setNullValueSerializer(NullValueSerializer)
+ }.writerWithDefaultPrettyPrinter()
+
+ assertEquals(
+ """
+ {
+ "foo" : 0,
+ "bar" : "null-value",
+ "baz" : 0,
+ "qux" : [ 0, "null-value" ],
+ "quux" : [ 0, "null-value" ],
+ "corge" : {
+ "inlineField" : 0
+ },
+ "grault" : "null-value",
+ "garply" : {
+ "0" : 0,
+ "1" : "null-value",
+ "null-key" : "null-value"
+ }
+ }
+ """.trimIndent(),
+ writer.writeValueAsString(target)
+ )
+ }
}
class SerializerPriorityTest {

Fixed an issue where NullValueSerializer was not being applied.