@@ -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
@@ -242,6 +243,7 @@ internal inline fun <T> convert(crossinline converter: (T) -> Any?): TypeConvert
242
243
243
244
private enum class DummyEnum
244
245
246
+ @Suppress(" UNCHECKED_CAST" )
245
247
internal fun createConverter (from : KType , to : KType , options : ParserOptions ? = null): TypeConverter ? {
246
248
if (from.arguments.isNotEmpty() || to.arguments.isNotEmpty()) return null
247
249
if (from.isMarkedNullable) {
@@ -250,25 +252,24 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
250
252
}
251
253
val fromClass = from.jvmErasure
252
254
val toClass = to.jvmErasure
255
+ return when {
256
+ fromClass == toClass -> TypeConverterIdentity
253
257
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 )
258
+ toClass.isValue -> {
259
+ val constructor =
260
+ toClass.primaryConstructor ? : error( " Value type $toClass doesn't have primary constructor " )
261
+ val underlyingType = constructor .parameters.single().type
262
+ val converter = getConverter(from, underlyingType )
263
+ ? : throw TypeConverterNotFoundException (from, underlyingType, null )
264
+ return convert< Any > {
265
+ val converted = converter(it )
266
+ if (converted == null && ! underlyingType.isMarkedNullable) {
267
+ throw TypeConversionException (it, from, underlyingType, null )
268
+ }
269
+ constructor .call(converted )
266
270
}
267
- constructor .call(converted)
268
271
}
269
- }
270
272
271
- return when {
272
273
fromClass == String ::class -> {
273
274
val parser = Parsers [to.withNullability(false )]
274
275
when {
0 commit comments