Last active
October 15, 2019 13:05
-
-
Save bastman/94f6c8afb86edfaf6a302b990dcfe210 to your computer and use it in GitHub Desktop.
jackson-module-kotlin-issue-130
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
package com.example | |
import com.fasterxml.jackson.databind.DeserializationFeature | |
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.SerializationFeature | |
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | |
import com.fasterxml.jackson.module.kotlin.readValue | |
import org.junit.jupiter.api.Test | |
/** | |
* see: https://github.com/FasterXML/jackson-module-kotlin/issues/130 | |
*/ | |
class FooTest { | |
@Test | |
fun testA() { | |
val source = "{}" | |
doIt(source) | |
} | |
@Test | |
fun testB() { | |
val source = """ {"s":null} """ | |
// will fail: JSON.readValue<FooNotNullable>(source) | |
doIt(source) | |
} | |
private fun doIt(source:String) { | |
println("==== source: $source ====") | |
JSON.readValue<FooNullable>(source) | |
.also { | |
println("fooNullable: $it") | |
println("fooNullable.toJson(): ${JSON.writeValueAsString(it)}") | |
} | |
JSON.readValue<FooNotNullable>(source) | |
.also { | |
println("fooNotNullable: $it") | |
println("fooNotNullable.toJson(): ${JSON.writeValueAsString(it)}") | |
} | |
} | |
} | |
private val JSON:ObjectMapper = defaultMapper() | |
private data class FooNullable(val s:String?="default", val i:Int?=100) | |
private data class FooNotNullable(val s:String="default", val i:Int=100) | |
private fun defaultMapper(): ObjectMapper = jacksonObjectMapper() | |
.findAndRegisterModules() | |
// toJson() | |
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | |
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) | |
// fromJson() | |
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | |
.disable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT) | |
.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) | |
.disable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) | |
.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) | |
.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment