1 /* 2 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization.internal 6 7 import kotlinx.serialization.* 8 import kotlin.reflect.* 9 10 @Suppress("NOTHING_TO_INLINE") getCheckednull11internal actual inline fun <T> Array<T>.getChecked(index: Int): T { 12 return get(index) 13 } 14 15 @Suppress("NOTHING_TO_INLINE") getCheckednull16internal actual inline fun BooleanArray.getChecked(index: Int): Boolean { 17 return get(index) 18 } 19 platformSpecificSerializerNotRegisterednull20internal actual fun KClass<*>.platformSpecificSerializerNotRegistered(): Nothing { 21 throw SerializationException( 22 "${notRegisteredMessage()}\n" + 23 "On Kotlin/Wasm explicitly declared serializer should be used for interfaces and enums without @Serializable annotation" 24 ) 25 } 26 27 @Suppress( 28 "UNCHECKED_CAST", 29 "DEPRECATION_ERROR" 30 ) 31 @OptIn(ExperimentalAssociatedObjects::class) constructSerializerForGivenTypeArgsnull32internal actual fun <T : Any> KClass<T>.constructSerializerForGivenTypeArgs(vararg args: KSerializer<Any?>): KSerializer<T>? = 33 when (val assocObject = findAssociatedObject<SerializableWith>()) { 34 is KSerializer<*> -> assocObject as KSerializer<T> 35 is SerializerFactory -> assocObject.serializer(*args) as KSerializer<T> 36 else -> null 37 } 38 39 @Suppress("DEPRECATION_ERROR") compiledSerializerImplnull40internal actual fun <T : Any> KClass<T>.compiledSerializerImpl(): KSerializer<T>? = 41 this.constructSerializerForGivenTypeArgs() 42 43 44 internal actual fun <T> createCache(factory: (KClass<*>) -> KSerializer<T>?): SerializerCache<T> { 45 return object: SerializerCache<T> { 46 override fun get(key: KClass<Any>): KSerializer<T>? { 47 return factory(key) 48 } 49 } 50 } 51 createParametrizedCachenull52internal actual fun <T> createParametrizedCache(factory: (KClass<Any>, List<KType>) -> KSerializer<T>?): ParametrizedSerializerCache<T> { 53 return object: ParametrizedSerializerCache<T> { 54 override fun get(key: KClass<Any>, types: List<KType>): Result<KSerializer<T>?> { 55 return kotlin.runCatching { factory(key, types) } 56 } 57 } 58 } 59 toNativeArrayImplnull60internal actual fun <T : Any, E : T?> ArrayList<E>.toNativeArrayImpl(eClass: KClass<T>): Array<E> = toTypedArray() 61 62 internal actual fun isReferenceArray(rootClass: KClass<Any>): Boolean = rootClass == Array::class