On Kotlin/Native (iOS) I’m getting an unsupported ...
# serialization
k
On Kotlin/Native (iOS) I’m getting an unsupported call of refied inlined function when using
Json.encodeToString(value: T)
. This is the following code snippet.
Copy code
override var market: Market?
        get() = marketJson?.let(json::decodeFromString)
        set(value) { marketJson = value?.let(json::encodeToString) }
Is this a potential bug in K/N or serialization? Using Kotlin 1.6.10, serialization 1.3.1. Full stack trace in thread.
Copy code
Uncaught Kotlin exception: kotlin.IllegalStateException: unsupported call of reified inlined function `kotlinx.serialization.encodeToString`
    at 0   shared                              0x000000010957875c kfun:kotlin.Throwable#<init>(kotlin.String?){} + 96 (/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt:24:37)
    at 1   shared                              0x0000000109571670 kfun:kotlin.Exception#<init>(kotlin.String?){} + 92 (/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)
    at 2   shared                              0x00000001095718a4 kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 92 (/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3   shared                              0x0000000109571f3c kfun:kotlin.IllegalStateException#<init>(kotlin.String?){} + 92 (/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/Exceptions.kt:70:44)
    at 4   shared                              0x00000001095c6a30 ThrowIllegalStateExceptionWithMessage + 168 (/opt/buildAgent/work/6326934d18cfe24e/kotlin/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt:89:11)
    at 5   shared                              0x000000010995a9a0 kfun:kotlinx.serialization#encodeToString__at__kotlinx.serialization.StringFormat(0:0){0§<kotlin.Any?>}kotlin.String + 72
    at 6   shared                              0x0000000108b83a4c kfun:energy.octopus.octopusenergy.common.settings.ObservableMarketSettings#<set-market>(energy.octopus.octopusenergy.common.featureFlag.Market?){} + 300 (/Users/kevin/code/Flapjack/sharedCore/src/commonMain/kotlin/energy/octopus/octopusenergy/common/settings/MarketSettings.kt:26:69)
    at 7   shared                              0x0000000108b61a38 kfun:energy.octopus.octopusenergy.common.config.SettingsMarketSelectionRepository#setCurrentMarket(energy.octopus.octopusenergy.common.featureFlag.Market){} + 232 (/Users/kevin/code/Flapjack/sharedCore/src/commonMain/kotlin/energy/octopus/octopusenergy/common/config/MarketSelectionRepository.kt:21:9)
    at 8   shared                              0x0000000109046458 kfun:energy.octopus.octopusenergy.usermanagement.viewmodels.LogInViewModel.emitUpdatedMarket#internal + 276 (/Users/kevin/code/Flapjack/shared/src/commonMain/kotlin/energy/octopus/octopusenergy/usermanagement/viewmodels/LogInViewModel.kt:103:35)
    at 9   shared                              0x0000000109045f84 kfun:energy.octopus.octopusenergy.usermanagement.viewmodels.LogInViewModel#onAction(energy.octopus.octopusenergy.usermanagement.viewmodels.LogInViewModel.UiAction){} + 1408 (/Users/kevin/code/Flapjack/shared/src/commonMain/kotlin/energy/octopus/octopusenergy/usermanagement/viewmodels/LogInViewModel.kt:93:37)
    at 10  shared                              0x000000010944275c objc2kotlin.16316 + 168 (/<compiler-generated>:1:0)
    at 11  Octopus Energy                      0x000000010089ca30 $s14Octopus_Energy14LoginViewModelC15onMarketUpdated6marketySo06SharedG0_p_tF + 100 (/Users/kevin/code/Flapjack/iosApp/iosApp/Domain/Login/LoginViewModel.swift:180:25)
    at 12  Octopus Energy                      0x0000000100ab4a44 $s14Octopus_Energy9LoginViewV14marketSelectorQrvg7SwiftUI05TupleD0VyAE6HStackVyAGyAE6SpacerV_AE0D0PAEE5frame5width6height9alignmentQr12CoreGraphics7CGFloatVSg_AuE9AlignmentVtFQOyAIyAGy8Symmetry12SYMTextLabelV_AE6ToggleVyAE4TextVGtGG_Qo_AmEEAnopQQrAU_AuWtFQOyAmEE8onChange2of7performQrqd___yqd__ctSQRd__lFQOyAX7SYMMenuVySayAA0C6MarketVGA13_G_A13_SgQo__Qo_tGG_AKtGyXEfU_A19_yXEfU_yA16_cfU1_ + 232 (/Users/kevin/code/Flapjack/iosApp/iosApp/Domain/Login/LoginView.swift:73:31)
    at 13  Octopus Energy                      0x0000000100ab4a9c $s14Octopus_Energy11LoginMarketVSgIegg_ADIegn_TR + 40
    at 14  SwiftUI                             0x00000001b98edfa8 $s7SwiftUI21ValueActionDispatcher33_BE73C7B2ED535BE0F08A3F8A8FCF9339LLV06updateC0yyFyycfU1_TA + 56
    at 15  SwiftUI                             0x00000001b947bc84 $sIeg_ytIegr_TR + 20
    at 16  SwiftUI                             0x00000001b94596ac $s7SwiftUI6UpdateO3endyyFZ + 504
    at 17  SwiftUI                             0x00000001b926d1dc $sSo9NSRunLoopC7SwiftUIE14flushObserversyyFZ + 168
    at 18  SwiftUI                             0x00000001b926d12c $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_ + 12
    at 19  SwiftUI                             0x00000001b9266b88 $ss5Error_pIgzo_ytsAA_pIegrzo_TR95$sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_yyXEfU_Tf3npf_n + 20
    at 20  libswiftObjectiveC.dylib            0x00000001b64a4f50 $s10ObjectiveC15autoreleasepool8invokingxxyKXE_tKlF + 56
    at 21  SwiftUI                             0x00000001b926d110 $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_ + 48
    at 22  SwiftUI                             0x00000001b926d24c $sSo9NSRunLoopC7SwiftUIE11addObserveryyyycFZySo05CFRunbF3RefaSg_So0gB8ActivityVSvSgtcfU_To + 52
    at 23  CoreFoundation                      0x000000018034ef9c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
    at 24  CoreFoundation                      0x0000000180349724 __CFRunLoopDoObservers + 556
    at 25  CoreFoundation                      0x0000000180349c84 __CFRunLoopRun + 976
    at 26  CoreFoundation                      0x00000001803493a8 CFRunLoopRunSpecific + 572
    at 27  GraphicsServices                    0x000000018c03c5ec GSEventRunModal + 160
    at 28  UIKitCore                           0x0000000184d937ac -[UIApplication _run] + 992
a
I think you need to specify an explicit serializer https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#registered-subclasses
Please note that this example works only on JVM because of serializer function restrictions. For JS and Native, explicit serializer should be used:
format.encodeToString(PolymorphicSerializer(Project::class), data)
👍 1
k
Thanks!