|
| 1 | +package com.fasterxml.jackson.module.kotlin.test.github |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonGenerator |
| 4 | +import com.fasterxml.jackson.databind.SerializerProvider |
| 5 | +import com.fasterxml.jackson.databind.module.SimpleModule |
| 6 | +import com.fasterxml.jackson.databind.ser.std.StdSerializer |
| 7 | +import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder |
| 8 | +import org.junit.Test |
| 9 | +import kotlin.test.assertEquals |
| 10 | + |
| 11 | +// Most of the current behavior has been tested on GitHub464, so only serializer-related behavior is tested here. |
| 12 | +class GitHub524 { |
| 13 | + @JvmInline |
| 14 | + value class HasSerializer(val value: Int?) |
| 15 | + object Serializer : StdSerializer<HasSerializer>(HasSerializer::class.java) { |
| 16 | + override fun serialize(value: HasSerializer, gen: JsonGenerator, provider: SerializerProvider) { |
| 17 | + gen.writeString(value.toString()) |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + @JvmInline |
| 22 | + value class NoSerializer(val value: Int?) |
| 23 | + |
| 24 | + data class Poko( |
| 25 | + // ULong has a custom serializer defined in Serializers. |
| 26 | + val foo: ULong = ULong.MAX_VALUE, |
| 27 | + // If a custom serializer is set, the ValueClassUnboxSerializer will be overridden. |
| 28 | + val bar: HasSerializer = HasSerializer(1), |
| 29 | + val baz: HasSerializer = HasSerializer(null), |
| 30 | + val qux: HasSerializer? = null, |
| 31 | + // If there is no serializer, it will be unboxed as the existing. |
| 32 | + val quux: NoSerializer = NoSerializer(2) |
| 33 | + ) |
| 34 | + |
| 35 | + @Test |
| 36 | + fun test() { |
| 37 | + val sm = SimpleModule() |
| 38 | + .addSerializer(Serializer) |
| 39 | + val writer = jacksonMapperBuilder().addModule(sm).build().writerWithDefaultPrettyPrinter() |
| 40 | + |
| 41 | + // 18446744073709551615 is ULong.MAX_VALUE. |
| 42 | + assertEquals( |
| 43 | + """ |
| 44 | + { |
| 45 | + "foo" : 18446744073709551615, |
| 46 | + "bar" : "HasSerializer(value=1)", |
| 47 | + "baz" : "HasSerializer(value=null)", |
| 48 | + "qux" : null, |
| 49 | + "quux" : 2 |
| 50 | + } |
| 51 | + """.trimIndent(), |
| 52 | + writer.writeValueAsString(Poko()) |
| 53 | + ) |
| 54 | + } |
| 55 | +} |
0 commit comments