Skip to content

Commit

Permalink
Merge pull request #763 from k163377/refactors
Browse files Browse the repository at this point in the history
Minor refactoring to support `value class` in deserialization
  • Loading branch information
k163377 committed Jan 21, 2024
2 parents ea31521 + 31a9267 commit a0ce010
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
1 change: 1 addition & 0 deletions release-notes/CREDITS-2.x
Expand Up @@ -18,6 +18,7 @@ Contributors:
# 2.17.0 (not yet released)

WrongWrong (@k163377)
* #763: Minor refactoring to support value class in deserialization.
* #760: Improved processing related to parameter parsing on Kotlin.
* #759: Organize internal commons.
* #758: Deprecated SingletonSupport.
Expand Down
Expand Up @@ -102,14 +102,16 @@ internal class KotlinDeserializers(
config: DeserializationConfig?,
beanDesc: BeanDescription?,
): JsonDeserializer<*>? {
val rawClass = type.rawClass

return when {
type.isInterface && type.rawClass == Sequence::class.java -> SequenceDeserializer
type.rawClass == Regex::class.java -> RegexDeserializer
type.rawClass == UByte::class.java -> UByteDeserializer
type.rawClass == UShort::class.java -> UShortDeserializer
type.rawClass == UInt::class.java -> UIntDeserializer
type.rawClass == ULong::class.java -> ULongDeserializer
type.rawClass == KotlinDuration::class.java ->
type.isInterface && rawClass == Sequence::class.java -> SequenceDeserializer
rawClass == Regex::class.java -> RegexDeserializer
rawClass == UByte::class.java -> UByteDeserializer
rawClass == UShort::class.java -> UShortDeserializer
rawClass == UInt::class.java -> UIntDeserializer
rawClass == ULong::class.java -> ULongDeserializer
rawClass == KotlinDuration::class.java ->
JavaToKotlinDurationConverter.takeIf { useJavaDurationConversion }?.delegatingDeserializer
else -> null
}
Expand Down
Expand Up @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.BeanDescription
import com.fasterxml.jackson.databind.DeserializationConfig
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.deser.SettableBeanProperty
import com.fasterxml.jackson.databind.deser.ValueInstantiator
import com.fasterxml.jackson.databind.deser.ValueInstantiators
Expand Down Expand Up @@ -47,14 +48,15 @@ internal class KotlinValueInstantiator(
valueCreator.valueParameters.forEachIndexed { idx, paramDef ->
val jsonProp = props[idx]
val isMissing = !buffer.hasParameter(jsonProp)
val valueDeserializer: JsonDeserializer<*>? by lazy { jsonProp.valueDeserializer }

val paramType = paramDef.type
var paramVal = if (!isMissing || jsonProp.hasInjectableValueId()) {
val tempParamVal = buffer.getParameter(jsonProp)
if (tempParamVal == null && jsonProp.skipNulls() && paramDef.isOptional) {
return@forEachIndexed
}
tempParamVal
buffer.getParameter(jsonProp) ?: run {
if (jsonProp.skipNulls() && paramDef.isOptional) return@forEachIndexed

null
}
} else {
when {
paramDef.isOptional || paramDef.isVararg -> return@forEachIndexed
Expand All @@ -63,15 +65,15 @@ internal class KotlinValueInstantiator(
// Primitive types always try to get from a buffer, considering several settings
jsonProp.type.isPrimitive -> buffer.getParameter(jsonProp)
// to get suitable "missing" value provided by deserializer
else -> jsonProp.valueDeserializer?.getAbsentValue(ctxt)
else -> valueDeserializer?.getAbsentValue(ctxt)
}
}

val propType = jsonProp.type

if (paramVal == null) {
if (propType.requireEmptyValue()) {
paramVal = jsonProp.valueDeserializer!!.getEmptyValue(ctxt)
paramVal = valueDeserializer!!.getEmptyValue(ctxt)
} else {
val isMissingAndRequired = isMissing && jsonProp.isRequired

Expand Down

0 comments on commit a0ce010

Please sign in to comment.