dataWithJSONObject: behavior changed in xcode 11?

We used to call [NSJSONSerialization dataWithJSONObject:data options:0 error:&error] directly. If data isn't json convertiable, the call will pass the invalid-json-format-error into the error pointer. That's how we catch the error.
But since we update xcode's version to 11 yesterday, we notice some of our test cases failed because for handling not json convertiable data, dataWithJSONObject decides to throw an exception. Our code isn't ready for that.
Does anyone also have this issue? Does Apple just silently change its API bahavior without noticing us?

Replies

Please share the cases where the behavior changes between Xcode 10 and 11.

This is what we have:

NSData *someData = data; // data here is not json compatiable
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:someData options:0 error:&error];


The above code works in xcode 10. But it will raise an exception at line 3 in xcode 11.

someData must not be a NSData. It can NSDictionary/ NSArray. If someData will not produce valid JSON, an exception is thrown. This exception is thrown prior to parsing and represents a programming error, not an internal error. You should check whether the input will produce valid JSON before calling this method by using isValidJSONObject:


Hope it helps.

Thanks for sharing. I tried your code with Xcode 9.4.1 and 10.1 with Command Line Tool project.


Xcode 9.4.1 -> Caused exception

Xcode 10.1 -> Caused exception


As already explained by kanishka_408, decides to throw an exception is the right behavior found both in Xcode 9.4.1 and 10.1.


There may be several versions of implementation of NSJSONSerialization, and you might have hit a buggy one in your Xcode 10 testing.

Or the example might not be the right one that would show different behavior.