From 551a66f4c01e0772e281ef151eb944902ae1e738 Mon Sep 17 00:00:00 2001 From: Rafal Bednarczuk Date: Fri, 25 Sep 2020 08:14:12 +0200 Subject: [PATCH] fix #373 --- .../jackson/module/kotlin/Extensions.kt | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt index 5fd35756e..ddc2c3f80 100644 --- a/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt +++ b/src/main/kotlin/com/fasterxml/jackson/module/kotlin/Extensions.kt @@ -20,22 +20,22 @@ fun jacksonMapperBuilder(): JsonMapper.Builder = JsonMapper.builder().addModule( inline fun jacksonTypeRef(): TypeReference = object : TypeReference() {} -inline fun ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef()) -inline fun ObjectMapper.readValues(jp: JsonParser): MappingIterator = readValues(jp, jacksonTypeRef()) +inline fun ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValues(jp: JsonParser): MappingIterator = readValues(jp, jacksonTypeRef()).throwIfNullableTypeIsNull() -inline fun ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef()) -inline fun ObjectMapper.readValue(content: String): T = readValue(content, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: Reader): T = readValue(src, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef()) -inline fun ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef()) +inline fun ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValue(content: String): T = readValue(content, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValue(src: Reader): T = readValue(src, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef()).throwIfNullableTypeIsNull() -inline fun ObjectMapper.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java) -inline fun ObjectMapper.convertValue(from: Any): T = convertValue(from, jacksonTypeRef()) +inline fun ObjectMapper.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java).throwIfNullableTypeIsNull() +inline fun ObjectMapper.convertValue(from: Any): T = convertValue(from, jacksonTypeRef()).throwIfNullableTypeIsNull() -inline fun ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef()) -inline fun ObjectReader.readValuesTyped(jp: JsonParser): Iterator = readValues(jp, jacksonTypeRef()) -inline fun ObjectReader.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java) +inline fun ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectReader.readValuesTyped(jp: JsonParser): Iterator = readValues(jp, jacksonTypeRef()).throwIfNullableTypeIsNull() +inline fun ObjectReader.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java).throwIfNullableTypeIsNull() internal fun JsonMappingException.wrapWithPath(refFrom: Any?, refFieldName: String) = JsonMappingException.wrapWithPath(this, refFrom, refFieldName) internal fun JsonMappingException.wrapWithPath(refFrom: Any?, index: Int) = JsonMappingException.wrapWithPath(this, refFrom, index) @@ -48,4 +48,11 @@ inline fun SimpleModule.addSerializer(kClass: KClass, seria inline fun SimpleModule.addDeserializer(kClass: KClass, deserializer: JsonDeserializer) = this.apply { addDeserializer(kClass.java, deserializer) addDeserializer(kClass.javaObjectType, deserializer) +} + +inline fun T.throwIfNullableTypeIsNull(): T { + if (null !is T && this == null) { + throw Exception() + } + return this } \ No newline at end of file