All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nativeMain.kotlinx.serialization.internal.Platform.kt Maven / Gradle / Ivy

There is a newer version: 0.12.0-356
Show newest version
/*
 * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.internal

import kotlinx.serialization.*
import kotlin.reflect.*

@Suppress("NOTHING_TO_INLINE")
internal actual inline fun  Array.getChecked(index: Int): T {
    return get(index)
}

@Suppress("NOTHING_TO_INLINE")
internal actual inline fun BooleanArray.getChecked(index: Int): Boolean {
    return get(index)
}

internal actual fun KClass<*>.platformSpecificSerializerNotRegistered(): Nothing {
    throw SerializationException(
        notRegisteredMessage() +
            "To get enum serializer on Kotlin/Native, it should be annotated with @Serializable annotation.\n" +
            "To get interface serializer on Kotlin/Native, use PolymorphicSerializer() constructor function.\n"
    )
}

@Suppress(
    "UNCHECKED_CAST",
    "DEPRECATION_ERROR"
)
@OptIn(ExperimentalAssociatedObjects::class)
internal actual fun  KClass.constructSerializerForGivenTypeArgs(vararg args: KSerializer): KSerializer? =
    when (val assocObject = findAssociatedObject()) {
        is KSerializer<*> -> assocObject as KSerializer
        is kotlinx.serialization.internal.SerializerFactory -> assocObject.serializer(*args) as KSerializer
        else -> null
    }

@Suppress("DEPRECATION_ERROR")
internal actual fun  KClass.compiledSerializerImpl(): KSerializer? =
    this.constructSerializerForGivenTypeArgs()


internal actual fun  createCache(factory: (KClass<*>) -> KSerializer?): SerializerCache {
    return object: SerializerCache {
        override fun get(key: KClass): KSerializer? {
            return factory(key)
        }
    }
}

internal actual fun  createParametrizedCache(factory: (KClass, List) -> KSerializer?): ParametrizedSerializerCache {
    return object: ParametrizedSerializerCache {
        override fun get(key: KClass, types: List): Result?> {
            return kotlin.runCatching { factory(key, types) }
        }
    }
}

internal actual fun  ArrayList.toNativeArrayImpl(eClass: KClass): Array {
    val result = arrayOfAnyNulls(size)
    var index = 0
    for (element in this) result[index++] = element
    @Suppress("USELESS_CAST")
    return result as Array
}

@Suppress("UNCHECKED_CAST")
private fun  arrayOfAnyNulls(size: Int): Array = arrayOfNulls(size) as Array

internal actual fun isReferenceArray(rootClass: KClass): Boolean = rootClass == Array::class




© 2015 - 2025 Weber Informatics LLC | Privacy Policy