Skip to content
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

throw exception when i put a jsonobject into jsonobject #759

Closed
roths opened this issue Mar 15, 2020 · 5 comments
Closed

throw exception when i put a jsonobject into jsonobject #759

roths opened this issue Mar 15, 2020 · 5 comments

Comments

@roths
Copy link

roths commented Mar 15, 2020

class JsCall {
    companion object {
        fun test(): JsonObject {
            val res = JsonObject(mutableMapOf())
            val response = JsonObject(mutableMapOf())
            (response.content as MutableMap).put("data", res) 
            return response
        }
    }
}

I'm converting some android libraries to MPP libraries. There's some logic that puts a jsonobject inside a jsonobject. When i rewirte it with kx.serialization, i got cast error in IOS

error info:
Could not cast value of type 'SharedCodeKMapAsNSDictionary' (0x10460cee8) to 'SharedCodeKotlinx_serialization_runtimeJsonElement' (0x10460c4e8).

@roths roths closed this as completed Mar 15, 2020
@roths roths changed the title extension function throw exception when i put a jsonobject into jsonobject throw exceotion when i put a jsonobject into jsonobject Mar 15, 2020
@roths roths reopened this Mar 15, 2020
@roths roths changed the title throw exceotion when i put a jsonobject into jsonobject throw exception when i put a jsonobject into jsonobject Mar 15, 2020
@roths
Copy link
Author

roths commented Mar 15, 2020

i can put JsonPrimitive into JsonObject. the diff between JsonPrimitive and JsonObject is JsonObject inherit Map<String, JsonElement> and delegate by content. Maybe that's the problem

@sandwwraith
Copy link
Member

I'd believe this is due to downcasting to MutableMap, which is generally is a bad practice

@roths
Copy link
Author

roths commented Mar 16, 2020

I can't find a way to put a jsonobject into jsonobject if a dont cast to Map<>.the original jsonobject is passed by other SDK,i can not controller the builder, and i need to add some extra into it.

I tried to use a builder, but the result was the same
image

@roths
Copy link
Author

roths commented Mar 17, 2020

I know what the problem is. In IOS,JsonObject is not declared as a class. If you use JsonObject in the function signature, the compiler compiles it to NSDictionary type, but in the internal logic JsonObject is treated as JsonElement type, so when the return value of the function is declared JsonObject and the returned instance type is also JsonObject, it will report an error on IOS, but on Android is fine.
here is Kt file content:

class Test {
    companion object {
        // cause error
        // (NSDictionary<NSString *, SharedCodeKotlinx_serialization_runtimeJsonElement *> *)test2 __attribute__((swift_name("test2()")));
        fun test(): JsonObject {
            return json {
                "data" to json {
                    "status_code" to 0
                }
                "required" to jsonArray { +"s" }
            }
        }
        // success
        // (SharedCodeKotlinx_serialization_runtimeJsonElement *)test4 __attribute__((swift_name("test3()")));
        fun test2(): JsonElement {
            val status = JsonObject(mutableMapOf())
            status.put("status_code", JsonPrimitive(0))
            val result = JsonObject(mutableMapOf())
            result.put("data", status)
            result.put("required", jsonArray { +"s" })
            return result
        }
        // (NSDictionary<NSString *, ...runtimeJsonElement *> *)anyTestInput:(NSDictionary<NSString *, ...runtimeJsonElement *> *)
        // input __attribute__((swift_name("anyTest(input:)")));
        fun anyTest(input: JsonObject) = json {
            "output"  to input
        }

        fun toString(any: Any?): String {
            return any.toString()
        }
    }
}

here is header file content:
image

@sandwwraith
Copy link
Member

Seems JetBrains/kotlin-native#3991, which I mentioned in your other issue, is also the case here.

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

No branches or pull requests

3 participants