|
| 1 | +package com.fasterxml.jackson.module.kotlin |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonToken |
| 4 | +import com.fasterxml.jackson.core.exc.InputCoercionException |
| 5 | +import com.fasterxml.jackson.databind.* |
| 6 | +import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializer |
| 7 | +import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializers |
| 8 | + |
| 9 | +// The reason why key is treated as nullable is to match the tentative behavior of StdKeyDeserializer. |
| 10 | +// If StdKeyDeserializer is modified, need to modify this too. |
| 11 | + |
| 12 | +internal object UByteKeyDeserializer : StdKeyDeserializer(TYPE_SHORT, UByte::class.java) { |
| 13 | + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UByte? = super.deserializeKey(key, ctxt) |
| 14 | + ?.let { |
| 15 | + (it as Short).asUByte() ?: throw InputCoercionException( |
| 16 | + null, |
| 17 | + "Numeric value (${key}) out of range of UByte (0 - ${UByte.MAX_VALUE}).", |
| 18 | + JsonToken.VALUE_NUMBER_INT, |
| 19 | + UByte::class.java |
| 20 | + ) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +internal object UShortKeyDeserializer : StdKeyDeserializer(TYPE_INT, UShort::class.java) { |
| 25 | + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UShort? = super.deserializeKey(key, ctxt) |
| 26 | + ?.let { |
| 27 | + (it as Int).asUShort() ?: throw InputCoercionException( |
| 28 | + null, |
| 29 | + "Numeric value (${key}) out of range of UShort (0 - ${UShort.MAX_VALUE}).", |
| 30 | + JsonToken.VALUE_NUMBER_INT, |
| 31 | + UShort::class.java |
| 32 | + ) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +internal object UIntKeyDeserializer : StdKeyDeserializer(TYPE_LONG, UInt::class.java) { |
| 37 | + override fun deserializeKey(key: String?, ctxt: DeserializationContext): UInt? = super.deserializeKey(key, ctxt) |
| 38 | + ?.let { |
| 39 | + (it as Long).asUInt() ?: throw InputCoercionException( |
| 40 | + null, |
| 41 | + "Numeric value (${key}) out of range of UInt (0 - ${UInt.MAX_VALUE}).", |
| 42 | + JsonToken.VALUE_NUMBER_INT, |
| 43 | + UInt::class.java |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// kind parameter is dummy. |
| 49 | +internal object ULongKeyDeserializer : StdKeyDeserializer(TYPE_LONG, ULong::class.java) { |
| 50 | + override fun deserializeKey(key: String?, ctxt: DeserializationContext): ULong? = key?.let { |
| 51 | + it.toBigInteger().asULong() ?: throw InputCoercionException( |
| 52 | + null, |
| 53 | + "Numeric value (${key}) out of range of ULong (0 - ${ULong.MAX_VALUE}).", |
| 54 | + JsonToken.VALUE_NUMBER_INT, |
| 55 | + ULong::class.java |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +internal object KotlinKeyDeserializers : StdKeyDeserializers() { |
| 61 | + override fun findKeyDeserializer( |
| 62 | + type: JavaType, |
| 63 | + config: DeserializationConfig?, |
| 64 | + beanDesc: BeanDescription? |
| 65 | + ): KeyDeserializer? = when (type.rawClass) { |
| 66 | + UByte::class.java -> UByteKeyDeserializer |
| 67 | + UShort::class.java -> UShortKeyDeserializer |
| 68 | + UInt::class.java -> UIntKeyDeserializer |
| 69 | + ULong::class.java -> ULongKeyDeserializer |
| 70 | + else -> null |
| 71 | + } |
| 72 | +} |
0 commit comments