skip icon indicating copy to clipboard operation
skip copied to clipboard

JSONSerialization.jsonObject may have a different behavior when using SkipFuse, a crash occurs

Open flymedllva opened this issue 5 months ago • 2 comments

Hi, trying to run a library using SkipFuse having a JSONSerialization.jsonObject call and casting to any Sendable & Hashable, I get different behavior on iOS and Android. On iOS everything works, but on Android it crashes

import Foundation

var d = """
    {"test":{"test":1}}
    """.data(using: .utf8)

public typealias JSONTestValue = any Sendable & Hashable

let f = try JSONSerialization.jsonObject(with: d!, options: [])
let g = f as! JSONTestValue
Could not cast value of type 'Swift.Dictionary<Swift.String, Any>' (0xb400006ff7905358) to 'Swift.Hashable' (0xb400006ff7905390).
Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 24845

Perhaps of course it's not Skip, but Swift...

flymedllva avatar Jul 05 '25 19:07 flymedllva

I'm guessing that the differences comes from Darwin platforms (e.g., iOS, macOS) use the legacy Objective-C NSDictionary as a return type for JSONSerialization.jsonObject, which is Hashable and Sendable, but the Android (and Linux) versions that don't include Objective-C have that call return [String: Any], which cannot be Hashable.

Can you try a conditional cast on let f = try JSONSerialization.jsonObject(with: d!, options: []) as? [String: Any] and see if it passes in the Android toolchain? And is there a particular reason you need it to be Hashable?

marcprux avatar Jul 05 '25 22:07 marcprux

I'm trying to run a library (https://github.com/apollographql/apollo-ios-dev/blob/v2-alpha/apollo-ios/Sources/Apollo/JSONSerializationFormat.swift#L16 there doing a new version with Swift 6 support and it seems important only response parsing doesn't work on Linux/Android) that already has this code written in it. If you use [String: Any] it does get better, but then you start having problems with AnyHashable, the code keeps trying to do a cast and gets a very strange result, I've put an Issue in the Swift repository with an example of what's going on: https://github.com/swiftlang/swift/issues/82819

flymedllva avatar Jul 05 '25 23:07 flymedllva