[swift-users] NSJSONSerialization barfs writing a Dictionary parsed from JSON: "Invalid type in JSON write (_SwiftValue)"
Jens Alfke
jens at mooseyard.com
Fri Nov 18 13:37:19 CST 2016
A developer using the framework I work on just reported <https://forums.couchbase.com/t/exception-caught-in-map-block-of-view-invalid-type-in-json-write--swiftvalue/10808> an exception which is triggered by NSJSONSerialization (see backtrace below.) Basically an NSDictionary gets parsed from JSON data (in Obj-C), passed to a Swift function, which then calls another function (written in Obj-C) that serializes it back to JSON data. That last step is crashing due to some problem with bridging.
The events leading up to this are:
1. The framework (written in Obj-C) parses some JSON data using NSJSONSerialization, resulting in an NSDictionary.
2. The NSDictionary is passed as a parameter to a block; the parameter is typed as `NSDictionary*`.
3. The block is part of the application, which is written in Swift.
4. The block in turn passes that NSDictionary to a function in the framework (through a parameter is typed as `id`)
5. The framework uses NSJSONSerialization to convert that id to data.
Step 5 is triggering an assertion failure in Foundation. Apparently it failed to un-bridge a Swift value in an array back into an NSObject. (From the contents of the dictionary I know that the single-object array in question contains a string.)
Any idea what’s going on here? I’ve never seen this happen before, and this part of our framework has been around for 2 ½ years. People have successfully built Swift apps using the framework. (This app was built with Xcode 8.0; don’t know what the OS is, though.)
Invalid type in JSON write (_SwiftValue)
3 Foundation 0x0000000111263284 _writeJSONValue + 668
4 Foundation 0x00000001112636d9 ___writeJSONArray_block_invoke + 132
5 CoreFoundation 0x0000000118e1ce2a -[__NSSingleObjectArrayI enumerateObjectsWithOptions:usingBlock:] + 58
6 Foundation 0x00000001112633df _writeJSONArray + 330
7 Foundation 0x0000000111263204 _writeJSONValue + 540
8 Foundation 0x0000000111262f94 -[_NSJSONWriter dataWithRootObject:options:error:] + 124
9 Foundation 0x0000000111262e74 +[NSJSONSerialization dataWithJSONObject:options:error:] + 333
...
—Jens
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161118/7fc1c9fc/attachment.html>
More information about the swift-users
mailing list