Under what circumstances does JSONSerialization.data(withJSONObject:) throw a catchable error? Under what circumstances does JSONSerialization.data(withJSONObject:) throw a catchable error? json json

Under what circumstances does JSONSerialization.data(withJSONObject:) throw a catchable error?


Turns out it’s the same situation as this question: you can create a Swift string that contains invalid unicode (what?!), and that causes an exception.

let bogusStr = String(    bytes: [0xD8, 0x00] as [UInt8],    encoding: String.Encoding.utf16BigEndian)!do  {    let rawBody = try JSONSerialization.data(        withJSONObject: ["foo": bogusStr], options: [])    }catch    {    // Exception lands us here    print("Caught error:", error)    }

Why does the example code in the original question crash, then, instead of also throwing an error?

Replying to a bug report, Apple informed me that you should call JSONSerialization.isValidJSONObject(_:) before data(withJSONObject:) if you don’t know for sure that the object is encodable, failing to do that is a misuse of the API, and that’s why they decided it should crash instead of throwing something catchable.