Skip to content

Commit

Permalink
Fixed differences with 2.x branch
Browse files Browse the repository at this point in the history
Differences caused by merging errors and other problems were resolved.
  • Loading branch information
k163377 committed Nov 19, 2023
1 parent bd3b2c9 commit 7dc693a
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tools.jackson.module.kotlin
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.isAccessible

internal class ConstructorValueCreator<T>(override val callable: KFunction<T>) : tools.jackson.module.kotlin.ValueCreator<T>() {
internal class ConstructorValueCreator<T>(override val callable: KFunction<T>) : ValueCreator<T>() {
override val accessible: Boolean = callable.isAccessible

init {
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/tools/jackson/module/kotlin/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ fun jsonMapper(initializer: JsonMapper.Builder.() -> Unit = {}): JsonMapper {
}

//TODO: causing java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
fun jacksonObjectMapper(): ObjectMapper =
jsonMapper { addModule(kotlinModule()) }

fun jacksonObjectMapper(): ObjectMapper = jsonMapper { addModule(kotlinModule()) }
fun jacksonMapperBuilder(): JsonMapper.Builder = JsonMapper.builder().addModule(kotlinModule())

// 22-Jul-2019, tatu: Can not be implemented same way as in 2.x, addition via mapper.builder():
//fun ObjectMapper.registerKotlinModule(): ObjectMapper = this.registerModule(KotlinModule())

inline fun <reified T> jacksonTypeRef(): TypeReference<T> = object : TypeReference<T>() {}

inline fun <reified T> ObjectMapper.readValue(p: JsonParser): T = readValue(p, jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.readValues(p: JsonParser): MappingIterator<T> = readValues(p, jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.readValues(jp: JsonParser): MappingIterator<T> = readValues(jp, jacksonTypeRef<T>())

inline fun <reified T> ObjectMapper.readValue(src: File): T = readValue(src, jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.readValue(src: URL): T = readValue(src, jacksonTypeRef<T>())
Expand All @@ -59,9 +57,9 @@ inline fun <reified T> ObjectMapper.readValue(src: ByteArray): T = readValue(src
inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T = readValue(this.treeAsTokens(n), jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.convertValue(from: Any): T = convertValue(from, jacksonTypeRef<T>())

inline fun <reified T> ObjectReader.readValueTyped(p: JsonParser): T = forType(jacksonTypeRef<T>()).readValue(p)
inline fun <reified T> ObjectReader.readValuesTyped(p: JsonParser): Iterator<T> = readValues(p, jacksonTypeRef<T>())
inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T? = forType(jacksonTypeRef<T>()).readValue(this.treeAsTokens(n))
inline fun <reified T> ObjectReader.readValueTyped(jp: JsonParser): T = forType(jacksonTypeRef<T>()).readValue(jp)
inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iterator<T> = readValues(jp, jacksonTypeRef<T>())
inline fun <reified T> ObjectReader.treeToValue(jp: TreeNode): T? = forType(jacksonTypeRef<T>()).readValue(this.treeAsTokens(jp))

inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.Builder = this.addMixIn(T::class.java, U::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,14 @@ internal class KotlinAnnotationIntrospector(
* Subclasses can be detected automatically for sealed classes, since all possible subclasses are known
* at compile-time to Kotlin. This makes [com.fasterxml.jackson.annotation.JsonSubTypes] redundant.
*/
override fun findSubtypes(cfg : MapperConfig<*>, a: Annotated): MutableList<NamedType>? {
return a.rawType
.takeIf { it.isKotlinClass() }
?.let { rawType ->
rawType.kotlin.sealedSubclasses
.map { NamedType(it.java) }
.toMutableList()
.ifEmpty { null }
}
}
override fun findSubtypes(cfg : MapperConfig<*>, a: Annotated): MutableList<NamedType>? = a.rawType
.takeIf { it.isKotlinClass() }
?.let { rawType ->
rawType.kotlin.sealedSubclasses
.map { NamedType(it.java) }
.toMutableList()
.ifEmpty { null }
}

private fun AnnotatedField.hasRequiredMarker(): Boolean? {
val byAnnotation = (member as Field).isRequiredByAnnotation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ object RegexDeserializer : StdDeserializer<Regex>(Regex::class.java) {
}
}

/*
internal class KotlinDeserializers: Deserializers.Base() {
override fun findBeanDeserializer(type: JavaType, config: DeserializationConfig?, beanDesc: BeanDescription?): ValueDeserializer<*>? {
return if (type.isInterface && type.rawClass == Sequence::class.java) {
SequenceDeserializer
} else if (type.rawClass == Regex::class.java) {
RegexDeserializer
} else {
null
*/
object UByteDeserializer : StdDeserializer<UByte>(UByte::class.java) {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext) =
p.shortValue.asUByte() ?: throw InputCoercionException(
Expand Down Expand Up @@ -98,7 +88,7 @@ internal class KotlinDeserializers(
override fun findBeanDeserializer(
type: JavaType,
config: DeserializationConfig?,
beanDesc: BeanDescription?
beanDesc: BeanDescription?,
): ValueDeserializer<*>? {
return when {
type.isInterface && type.rawClass == Sequence::class.java -> SequenceDeserializer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ internal object KotlinKeyDeserializers : JDKKeyDeserializers() {
override fun findKeyDeserializer(
type: JavaType,
config: DeserializationConfig?,
beanDesc: BeanDescription?
beanDesc: BeanDescription?,
): KeyDeserializer? = when (type.rawClass) {
UByte::class.java -> UByteKeyDeserializer
UShort::class.java -> UShortKeyDeserializer
Expand Down
17 changes: 0 additions & 17 deletions src/main/kotlin/tools/jackson/module/kotlin/KotlinSerializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,3 @@ internal class KotlinSerializers : Serializers.Base() {
}
}
}

// This serializer is used to properly serialize the value class.
// The getter generated for the value class is special,
// so this class will not work properly when added to the Serializers
// (it is configured from KotlinAnnotationIntrospector.findSerializer).
internal class ValueClassBoxSerializer<T : Any>(
private val outerClazz: Class<out Any>, innerClazz: Class<T>
) : StdSerializer<T>(innerClazz) {
private val boxMethod = outerClazz.getMethod("box-impl", innerClazz)

override fun serialize(value: T?, gen: JsonGenerator, provider: SerializerProvider) {
// Values retrieved from getter are considered validated and constructor-impl is not executed.
val boxed = boxMethod.invoke(null, value)

provider.findValueSerializer(outerClazz).serialize(boxed, gen, provider)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ internal class KotlinValueInstantiator(
numCallableParameters++
}

return if (numCallableParameters == jsonParamValueList.size && valueCreator is tools.jackson.module.kotlin.ConstructorValueCreator) {
return if (numCallableParameters == jsonParamValueList.size && valueCreator is ConstructorValueCreator) {
// we didn't do anything special with default parameters, do a normal call
super.createFromObjectWith(ctxt, jsonParamValueList)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class ValueCreator<T> {
// @see #584
val valueParameters: List<KParameter> get() = callable.valueParameters

/**
/**
* Checking process to see if access from context is possible.
* @throws IllegalAccessException
*/
Expand Down

0 comments on commit 7dc693a

Please sign in to comment.