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