liveBook · Manning

KParameter.type

As explained in Kotlin in Action, 2E

KParameter.type is a Kotlin reflection property that returns the reflected Kotlin type of a callable parameter.

KParameter.type returns the reflected KType of a callable parameter. During JSON deserialization, inspect it before passing values to callBy: mismatched types cause IllegalArgumentException, especially for numeric parameters such as Int, Long, and Double. section 12.2.5

Selecting a serializer from the parameter type

fun serializerForType(type: KType): ValueSerializer<out Any?>? = when (type) { typeOf<Byte>() -> ByteSerializer typeOf<Int>() -> IntSerializer typeOf<Boolean>() -> BooleanSerializer else -> null } A deserializer can call serializerForType(param.type), convert JSON values to the constructor parameter's Kotlin type, and then supply the converted value in the callBy argument map. section 12.2.5

Listing 12.10 Getting a serializer based on the type of the value
fun serializerForType(type: KType): ValueSerializer<out Any?>? =
       when (type) {
           typeOf<Byte>() -> ByteSerializer
           typeOf<Int>() -> IntSerializer
           typeOf<Boolean>() -> BooleanSerializer
           // ...
           else -> null
       }

Browse the compact type-to-serializer mapping used to connect reflected KType values with built-in deserializers. section 12.2.5

How Kotlin in Action, 2E covers it

How deep it goes
Explains runtime type matching, numeric conversion, serializer lookup, and validation, but does not detail KType internals.
Examples
Uses Byte, Int, and Boolean serializer mappings plus Boolean runtime validation during JSON deserialization.
What it assumes
Assumes familiarity with callable parameters, KType, callBy, typeOf, and serializer-based JSON deserialization.
What you can do after
Apply it via Kotlin reflection to select conversions and prevent incorrectly typed values from reaching callBy.
How it connects
Connects Kotlin reflection with KType, callBy, typeOf, constructor parameters, and ValueSerializer.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.