summaryrefslogtreecommitdiff
path: root/runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt')
-rw-r--r--runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt52
1 files changed, 52 insertions, 0 deletions
diff --git a/runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt b/runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt
new file mode 100644
index 00000000..f3a39015
--- /dev/null
+++ b/runtime/commonMain/src/kotlinx/serialization/PlatformUtils.kt
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2018 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package kotlinx.serialization
+
+import kotlinx.serialization.internal.defaultSerializer
+import kotlin.reflect.KClass
+
+@UseExperimental(ExperimentalMultiplatform::class)
+@OptionalExpectation
+expect annotation class SharedImmutable()
+
+@ImplicitReflectionSerializer
+fun <T : Any> KClass<T>.serializer(): KSerializer<T> = compiledSerializer() ?: defaultSerializer()
+ ?: throw SerializationException("Can't locate argument-less serializer for $this. For generic classes, such as lists, please provide serializer explicitly.")
+
+@ImplicitReflectionSerializer
+expect fun <T : Any> KClass<T>.compiledSerializer(): KSerializer<T>?
+
+expect fun String.toUtf8Bytes(): ByteArray
+expect fun stringFromUtf8Bytes(bytes: ByteArray): String
+
+expect fun <E: Enum<E>> enumFromName(enumClass: KClass<E>, value: String): E
+expect fun <E: Enum<E>> enumFromOrdinal(enumClass: KClass<E>, ordinal: Int): E
+
+expect fun <E: Enum<E>> KClass<E>.enumClassName(): String
+expect fun <E: Enum<E>> KClass<E>.enumMembers(): Array<E>
+
+expect fun <T: Any, E: T?> ArrayList<E>.toNativeArray(eClass: KClass<T>): Array<E>
+
+/**
+ * Checks whether the receiver is an instance of a given kclass.
+ *
+ * This check is a replacement for [KClass.isInstance] because on JVM it requires kotlin-reflect.jar in classpath (see KT-14720).
+ *
+ * On JS and Native, this function delegates to aforementioned [KClass.isInstance] since it is supported there out-of-the-box;
+ * on JVM, it falls back to java.lang.Class.isInstance which causes difference when applied to function types with big arity.
+ */
+internal expect fun Any.isInstanceOf(kclass: KClass<*>): Boolean