Why does RKNSJSONSerialization crash on iOS 6? - ios

I'm seeing a crash in RKNSJSONSerialization on iOS 6 only - not on iOS 7. I'm using RestKit 0.20.3 and it happens fairly consistently for me. Even when I make the same request and get the same response for iOS 6/7, it works fine on iOS 7 but crashes on iOS 6.
Here's the crash - it's an EXC_BAD_ACCESS: http://crashes.to/s/2610b639062
The relevant (crashing) line in RestKit's RKNSJSONSerialization is the return:
+ (id)objectFromData:(NSData *)data error:(NSError **)error
{
return [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
}
So perhaps it's not RestKit at all - perhaps it's NSJSONSerialization.
I profiled the app with the Zombies tool and found this:
"An Objective-C message was sent to a deallocated 'CFString (immutable)' object (zombie) at address: 0x16851250."
Am I doing something wrong?

I've resolved this. The issue was that my JSON had duplicate keys in it, and iOS 6 couldn't handle that. The solution is to remove the duplicate keys from the JSON before trying to parse it with NSJSONSerialization on iOS 6. Apparently Apple has resolved this issue on iOS 7, since it doesn't crash there.
Related: NSJSONSerialization bug?

This seems to be a typo.
Assuming there is an object called error of type NSError, the call JSONObjectWithData: takes a pointer to a pointer as the last argument, i.e. with &.
return [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

Related

JSON Warning in Flickr Photo Manager

I am getting a warning in my iOS app. It doesn't seem to be affecting performance in iOS 6 but my app is crashing when loading the photos from Flickr in iOS 7 beta 2 and I'm not sure if it's an issue with the beta or perhaps something to do with this warning but I'd like to get rid of the warning anyway. Any help?
Implicit conversion from enumeration type 'enum NSJSONWritingOptions' to different enumeration type 'NSJSONReadingOptions' (aka 'enum NSJSONReadingOptions')
NSError *error;
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&error];
Pretty simple !! Just re-read the warning again. It says you are passing NSJSONWritingOptions option in options parameter while it expects NSJSONReadingOptions.
Change your line of code as below:
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
It should help you.
Core Background works on iOS 6 and iOS 7 beta 1,2. The project was inspired by the Yahoo Weather iOS App. ObjectiveFlickr is leveraged for Flickr integration.
https://github.com/justinmfischer/core-background

Parsing JSON using NSJSONSerialization

I am currently going through the Pragmatic iOS 6 book, and am having trouble understanding the following line of code explained in chapter 3 under the section about GCD:
NSJSONSerialization *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
//... some code here
NSArray *tweets = (NSArray *) jsonResponse; //<-- this line
Is it saying that the NSJSONSSerialization object could automatically return an instance of NSSArray, which is then stored in the tweets? I checked the Apple docs, but only saw items on restrictions using NSJSONSerialization, but not what objects it could then get converted to.
Thanks!
NSJSONSerialization can take a chunk of JSON data and turn into objects and it can do the same in the other direction that is by taking objects and converting them into the JSON data.
For easy understanding of NSJSONSerialization and interaction with Twitter, i would recommend you to use THIS and THIS tutorials.
Hope this helps!

exception error with json in xcode

I'm beginner in ios. I developing a call with JSON for access a data and insert after on UITableView.
In this point on my code
NSDictionary * dict = [[CJSONDeserializer deserializer ] deserialize:jsonData error:&error];
When compiling I obtained this error
2013-02-07 15:49:48.078 comercMobil[12933:c07] +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
2013-02-07 15:49:48.080 comercMobil[12933:c07] Exception +[CJSONScanner scannerWithData:]: unrecognized selector sent to class 0xe7b8
any suggestions? thanks for all
A couple of things:
This is not a compiler error, but rather a runtime error.
I don't think this is the line that's causing this error, since you're not calling scannerWithData here. I'd search your source for a reference to scannerWithData.
But, I agree with Sean that you should consider just using NSJSONSerialization.
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
Or, if you need support for iOS prior to version 5.0, SBJSON is very popular.

Issues getting json from webserver in iOS5

I have a app that receive a feed from a server through json.
I have been building it for iOS5, but in the last weeks testing with iOS6. I tested today with a iOS5 device, and everything crashed.
The code looks like this:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.myserver.com/news.json"]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
It worked without problems, but now it returns a null. The data is not null.
Cocoa error 3840 means
The data couldn’t be read because it has been corrupted. (No string
key for value in object around character 2.)
You should check your json with a validator like http://www.jsoneditoronline.org/ or http://jsonformatter.curiousconcept.com/
The issue was a duplicated key from the webserver. Now it works.
But it strange that the error is trigged in iOS5 and not in iOS6.

JSON: NSString stringWithContentsOfURL... with null in stream, crashes

Environment: iOS 4.3+ using Xcode 4.3+
I'm always getting an iOS/Xcode crash when I implement the following string that returns data with nulls in it:
[NSString stringWithContentsOfURL:myURL encoding:NSUTF8StringEncoding error:&error];
NSDictionary *myResult = (NSDictionary *)[myString JSONValue];
The result:
-[NSNull isEqualToString:]: unrecognized selector sent to instance 0x17315e8
When the data stream contains no nulls (mostly), it works fine.
Is this a known problem?
Any remedy?
You can also fix the data coming in from the source. In almost all of my web service interaction, I use COALESCE on queries and SP's to make sure that the data coming out is always consistent.

Resources