Skip to content

Cannot pass JsonObject containing JsonArray to Swift in a Lambda/Interface #2973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bitsycore opened this issue Apr 7, 2025 · 5 comments
Closed

Comments

@bitsycore
Copy link

bitsycore commented Apr 7, 2025

Describe the bug

When I try to pass a JsonObject to Swift i get the following error
Could not cast value of type "KListAsNSArray" to "Kotlinx_serialization_jsonJsonElement"
It seem that the interface List on JsonArray is taking over JsonElement in that situation.

To Reproduce

// Crash (also if it's an interface like MyListener { fun onAnswer(inData: JsonObject) }
fun test(listener: (JsonObject) -> Unit) {
    listener(
        buildJsonObject {
            put("test", "test")
            putJsonArray("test2") {
                add("test")
                add("test2")
            }
        }
    )
}

If it's directly a JsonArray it work

// Work
fun test2(listener: (JsonArray) -> Unit) {
    listener(
        buildJsonArray {
            add("test")
            add("test2")
        }
    )
}

If it's in a returned value it also work

// Work
fun test3() : JsonObject {
      return buildJsonObject { 
            put("test", "test")
            putJsonArray("test2") {
                  add("test")
                  add("test2")
            }
      }
}

Expected behavior

It should detect it as a jsonElement not a list and so work in this case

Environment

  • Kotlin version: 2.1.20
  • Library version: 1.7 and 1.8.1
  • Kotlin platforms: Native (Objc/Swift)
  • Gradle version: 8.11
  • Xcode 16.2 / iOS simulator 18.2
@sandwwraith
Copy link
Member

See #2919 (comment) — I think it is the same problem. You can also vote for the issue in the tracker here: https://youtrack.jetbrains.com/issue/KT-48079

@bitsycore
Copy link
Author

bitsycore commented Apr 7, 2025

Any workaround ?
It make kotlin.serialization.json not usable for a multiplatform library

@sandwwraith
Copy link
Member

As a workaround, you can write all JsonElement-related functions in Kotlin and use Swift only to call these functions. It is far from ideal, but this is how Kotlin/Swift interop is currently designed.

So if you have function like test3() in your example that only returns JsonObject or even a Map/String you should be fine

@bitsycore
Copy link
Author

It's for asynchrone call and it also crash with suspend/async :/

I wrapped JsonObject in a expect/actual that is JsonObject in Android but a wrapping class for iOS, it seem to work but it's annoying behavior

@sandwwraith
Copy link
Member

Closing in favor of https://youtrack.jetbrains.com/issue/KT-48079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants