Kotlinx.serialization: Crash in serialization code on iOS

Created on 28 Sep 2018  Â·  6Comments  Â·  Source: Kotlin/kotlinx.serialization

My serializable classes don't use unsupported native features - no enums, arrays, etc. They only use Lists, Strings and Ints. Android project has no problem serializing/deserializaing the classes. Native project for iOS compiles without errors. But as soon as I try to deserialize a string on iOS, I get the error below. I copied the string that crashes deserialization on iOS and tested it using JUnit in my Android library and it deserializes without problems.

kfun:kotlin.Error.<init>(kotlin.String?)kotlin.Error + 70
kfun:kotlin.NotImplementedError.<init>(kotlin.String)kotlin.NotImplementedError + 70
kfun:[email protected]<#GENERIC_kotlin.Any>.()Generic + 211
kfun:[email protected]<#GENERIC_kotlin.Any>.()Generic + 84
kfun:kotlinx.serialization.context.getOrDefault@kotlinx.serialization.context.SerialContext?.(kotlin.reflect.KClass<#GENERIC_kotlin.Any>)Generic + 179
kfun:Foo.Companion.parse(kotlin.String)Foo + 224

Foo.parse() is the method that I call from iOS and Android:

@Serializable
data class Foo(@SerialName("Bar") val Bars: List<Bar>? = null) {

    @Serializable
    data class Bar(...)

    companion object {
        @JvmStatic
        fun parse(jsonText: String): Foo = JSON.nonstrict.parse(jsonText)
    }
}

Am I doing anything wrong here?

Thank you in advance!

bug native

All 6 comments

More info: if I pass serializer to JSON.parse() method as below

@Serializable
data class Foo(@SerialName("Bar") val Bars: List<Bar>? = null) {

    @Serializable
    data class Bar(...)

    companion object {
        @JvmStatic
        fun parse(jsonText: String): Foo = JSON.nonstrict.parse(serializer(), jsonText)
    }
}

I get the following error

kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 70
kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 70
kfun:kotlin.native.IncorrectDereferenceException.<init>(kotlin.String)kotlin.native.IncorrectDereferenceException + 70
ThrowIncorrectDereferenceException + 66
CheckIsMainThread + 26
kfun:kotlinx.serialization.json.<get-C2TC>#internal + 15
kfun:kotlinx.serialization.json.charToTokenClass$kotlinx-serialization-runtime-native(kotlin.Char)ValueType + 67
kfun:kotlinx.serialization.json.Parser.nextToken() + 159
kfun:kotlinx.serialization.json.Parser.<init>(kotlin.String)kotlinx.serialization.json.Parser + 267
kfun:kotlinx.serialization.json.JSON.parse(kotlinx.serialization.DeserializationStrategy<#GENERIC>;kotlin.String)Generic + 162
kfun:Foo.Companion.parse(kotlin.String)Foo + 174

Is the error related to Main Thread? (CheckIsMainThread)

Hi, for the first exception I can say that it is not working by design – since there are no reflection on K/N, KClass.serializer() method throws NotImplementedError and therefore, you should always use two-arguments JSON.stringify and JSON.parse, passing a serializer alongside the object.

Second issue is likely indeed connected with threading. From which thread you're trying to use a parser? K/N has limitations on access to global constants from different threads.

It is indeed connected to recent changes in K/N mutability policy. Now global vals are only accessible from the main thread (see https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md) for details. I'll make a workaround.

@sandwwraith Thank you for the information! I'm indeed deserializing on a worker thread. Will be looking forward to the workaround to resume my project. Please update the thread when it is available. Thank you again.

@sandwwraith Thank you for the quick fix! I can confirm that the problem is resolved in eap13 branch.

Fixed in 0.8.2-rc13

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kdabir picture kdabir  Â·  3Comments

raderio picture raderio  Â·  3Comments

daweedm picture daweedm  Â·  4Comments

kastork picture kastork  Â·  3Comments

just-kip picture just-kip  Â·  3Comments