@@ -229,6 +229,7 @@ internal fun getConverter(from: KType, to: KType, options: ParserOptions? = null
229
229
convertersCache.getOrPut(Triple (from, to, options)) { createConverter(from, to, options) }
230
230
231
231
internal typealias TypeConverter = (Any ) -> Any?
232
+ private val TypeConverterIdentity : TypeConverter = { it }
232
233
233
234
internal fun Any.convertTo (type : KType ): Any? {
234
235
val clazz = javaClass.kotlin
@@ -250,25 +251,24 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
250
251
}
251
252
val fromClass = from.jvmErasure
252
253
val toClass = to.jvmErasure
254
+ return when {
255
+ fromClass == toClass -> TypeConverterIdentity
253
256
254
- if (fromClass == toClass) return { it }
255
-
256
- if ( toClass.isValue) {
257
- val constructor =
258
- toClass.primaryConstructor ? : error( " Value type $toClass doesn't have primary constructor " )
259
- val underlyingType = constructor .parameters.single().type
260
- val converter = getConverter(from, underlyingType)
261
- ? : throw TypeConverterNotFoundException (from, underlyingType, null )
262
- return convert< Any > {
263
- val converted = converter (it)
264
- if (converted == null && ! underlyingType.isMarkedNullable) {
265
- throw TypeConversionException (it, from, underlyingType, null )
257
+ toClass.isValue -> {
258
+ val constructor =
259
+ toClass.primaryConstructor ? : error( " Value type $toClass doesn't have primary constructor " )
260
+ val underlyingType = constructor .parameters.single().type
261
+ val converter = getConverter(from, underlyingType )
262
+ ? : throw TypeConverterNotFoundException (from, underlyingType, null )
263
+ return convert< Any > {
264
+ val converted = converter(it )
265
+ if (converted == null && ! underlyingType.isMarkedNullable) {
266
+ throw TypeConversionException (it, from, underlyingType, null )
267
+ }
268
+ constructor .call(converted )
266
269
}
267
- constructor .call(converted)
268
270
}
269
- }
270
271
271
- return when {
272
272
fromClass == String ::class -> {
273
273
val parser = Parsers [to.withNullability(false )]
274
274
when {
0 commit comments