diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub314.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub314.kt new file mode 100644 index 00000000..0ec29be1 --- /dev/null +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub314.kt @@ -0,0 +1,29 @@ +package io.github.projectmapk.jackson.module.kogera.zPorted.test.github + +import com.fasterxml.jackson.databind.MapperFeature +import io.github.projectmapk.jackson.module.kogera.jsonMapper +import io.github.projectmapk.jackson.module.kogera.kotlinModule +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class GitHub314 { + // Since Nothing? is compiled as a Void, it can be serialized by specifying ALLOW_VOID_VALUED_PROPERTIES + data object NothingData { + val data: Nothing? = null + } + + @Test + fun test() { + val expected = """{"data":null}""" + + val withoutKotlinModule = jsonMapper { enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) } + assertEquals(expected, withoutKotlinModule.writeValueAsString(NothingData)) + + val withKotlinModule = jsonMapper { + enable(MapperFeature.ALLOW_VOID_VALUED_PROPERTIES) + addModule(kotlinModule()) + } + + assertEquals(expected, withKotlinModule.writeValueAsString(NothingData)) + } +} diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub618.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub618.kt new file mode 100644 index 00000000..5aad570a --- /dev/null +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub618.kt @@ -0,0 +1,30 @@ +package io.github.projectmapk.jackson.module.kogera.zPorted.test.github + +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.SerializerProvider +import com.fasterxml.jackson.databind.annotation.JsonSerialize +import com.fasterxml.jackson.databind.ser.std.StdSerializer +import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class GitHub618 { + @JsonSerialize(using = V.Serializer::class) + @JvmInline + value class V(val value: String) { + class Serializer : StdSerializer(V::class.java) { + override fun serialize(p0: V, p1: JsonGenerator, p2: SerializerProvider) { + p1.writeString(p0.toString()) + } + } + } + + data class D(val v: V?) + + @Test + fun test() { + val mapper = jacksonObjectMapper() + // expected: {"v":null}, but NullPointerException thrown + assertEquals("""{"v":null}""", mapper.writeValueAsString(D(null))) + } +} diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub625.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub625.kt new file mode 100644 index 00000000..18438770 --- /dev/null +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/GitHub625.kt @@ -0,0 +1,55 @@ +package io.github.projectmapk.jackson.module.kogera.zPorted.test.github + +import com.fasterxml.jackson.annotation.JsonInclude +import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.junit.jupiter.api.Test + +class GitHub625 { + @JvmInline + value class Primitive(val v: Int) + + @JvmInline + value class NonNullObject(val v: String) + + @JvmInline + value class NullableObject(val v: String?) + + @JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.NON_NULL) + data class Dto( + val primitive: Primitive? = null, + val nonNullObject: NonNullObject? = null, + val nullableObject: NullableObject? = null + ) { + fun getPrimitiveGetter(): Primitive? = null + fun getNonNullObjectGetter(): NonNullObject? = null + fun getNullableObjectGetter(): NullableObject? = null + } + + @Test + fun test() { + val mapper = jacksonObjectMapper() + val dto = Dto() + assertEquals("{}", mapper.writeValueAsString(dto)) + } + + @JsonInclude(value = JsonInclude.Include.NON_EMPTY, content = JsonInclude.Include.NON_NULL) + data class FailingDto( + val nullableObject1: NullableObject = NullableObject(null), + val nullableObject2: NullableObject? = NullableObject(null), + val map: Map = mapOf("nullableObject" to NullableObject(null),) + ) { + fun getNullableObjectGetter1(): NullableObject = NullableObject(null) + fun getNullableObjectGetter2(): NullableObject? = NullableObject(null) + fun getMapGetter(): Map = mapOf("nullableObject" to NullableObject(null)) + } + + @Test + fun failing() { + val writer = jacksonObjectMapper() + val json = writer.writeValueAsString(FailingDto()) + + assertNotEquals("{}", json) + } +} diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github464.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github464.kt index 89fa4474..cb2870dd 100644 --- a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github464.kt +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github464.kt @@ -1,5 +1,6 @@ package io.github.projectmapk.jackson.module.kogera.zPorted.test.github +import com.fasterxml.jackson.annotation.JsonPropertyOrder import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind.JsonSerializer import com.fasterxml.jackson.databind.ObjectMapper @@ -42,6 +43,20 @@ class Github464 { // val xyzzy: T get() = quux } + @JsonPropertyOrder( + "foo", + "bar", + "baz", + "qux", + "quux", + "corge", + "grault", + "garply", + "waldo", + "fred", + "plugh", + // "xyzzy" + ) class Poko( val foo: ValueClass, val bar: ValueClass?, @@ -150,20 +165,22 @@ class Github464 { } } - class SerializerPriorityTest { - @JvmInline - value class ValueBySerializer(val value: Int) + @JvmInline + value class ValueBySerializer(val value: Int) - object Serializer : StdSerializer(ValueBySerializer::class.java) { - override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeString(value.value.toString()) - } + object Serializer : StdSerializer(ValueBySerializer::class.java) { + override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeString(value.value.toString()) } - object KeySerializer : StdSerializer(ValueBySerializer::class.java) { - override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { - gen.writeFieldName(value.value.toString()) - } + } + object KeySerializer : StdSerializer(ValueBySerializer::class.java) { + override fun serialize(value: ValueBySerializer, gen: JsonGenerator, provider: SerializerProvider) { + gen.writeFieldName(value.value.toString()) } + } + + @Nested + inner class SerializerPriorityTest { private val target = mapOf(ValueBySerializer(1) to ValueBySerializer(2)) private val sm = SimpleModule() @@ -172,8 +189,7 @@ class Github464 { @Test fun simpleTest() { - val om: ObjectMapper = jacksonMapperBuilder() - .addModule(sm).build() + val om: ObjectMapper = jacksonMapperBuilder().addModule(sm).build() assertEquals("""{"1":"2"}""", om.writeValueAsString(target)) } diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github536.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github536.kt new file mode 100644 index 00000000..ee6bc6b7 --- /dev/null +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github536.kt @@ -0,0 +1,54 @@ +package io.github.projectmapk.jackson.module.kogera.zPorted.test.github + +import com.fasterxml.jackson.annotation.JsonKey +import io.github.projectmapk.jackson.module.kogera.jacksonMapperBuilder +import io.github.projectmapk.jackson.module.kogera.testPrettyWriter +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class Github536 { + @JvmInline + value class JsonKeyGetter(val value: Int) { + @get:JsonKey + val jsonKey: String + get() = this.toString() + } + + interface IJsonKeyGetter { + @get:JsonKey + val jsonKey: String + get() = this.toString() + } + + @JvmInline + value class JsonKeyGetterImplementation(val value: Int) : IJsonKeyGetter + + @JvmInline + value class JsonKeyGetterImplementationDisabled(val value: Int) : IJsonKeyGetter { + @get:JsonKey(false) + override val jsonKey: String + get() = super.jsonKey + } + + private val writer = jacksonMapperBuilder().build().testPrettyWriter() + + @Test + fun test() { + val src = mapOf( + JsonKeyGetter(0) to 0, + JsonKeyGetterImplementation(1) to 1, + JsonKeyGetterImplementationDisabled(2) to 2 + ) + + assertEquals( + """ + { + "JsonKeyGetter(value=0)" : 0, + "JsonKeyGetterImplementation(value=1)" : 1, + "2" : 2 + } + """.trimIndent(), + writer.writeValueAsString(src) + ) + } +} diff --git a/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github630.kt b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github630.kt new file mode 100644 index 00000000..9ae99b60 --- /dev/null +++ b/src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/test/github/Github630.kt @@ -0,0 +1,37 @@ +package io.github.projectmapk.jackson.module.kogera.zPorted.test.github + +import com.fasterxml.jackson.annotation.JsonProperty +import io.github.projectmapk.jackson.module.kogera.jacksonObjectMapper +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class Github630 { + private val mapper = jacksonObjectMapper() + + data class Dto( + // from #570, #603 + val FOO: Int = 0, + val bAr: Int = 0, + @JsonProperty("b") + val BAZ: Int = 0, + @JsonProperty("q") + val qUx: Int = 0, + // from #71 + internal val quux: Int = 0, + // from #434 + val `corge-corge`: Int = 0, + // additional + @get:JvmName("aaa") + val grault: Int = 0 + ) + + @Test + fun test() { + val dto = Dto() + + assertEquals( + """{"FOO":0,"bAr":0,"b":0,"q":0,"quux":0,"corge-corge":0,"grault":0}""", + mapper.writeValueAsString(dto) + ) + } +}