I'm primarily a c# developer but I have been tasked with resolving a bug in an objective-c mac client. I am using nsjsonserialization to serialize the json object.
The bug occurs on deserialization if the json object contains the copyright symbol.
We were using sbjson and I switched to nsjsonserialization and that didn't resolve the issue. I don't know if I am doing it wrong or if I need to use a different serialization library. In c# I could just use newtonsoft. Is there a similarly standard json serialization library for objective-c that I should use?
Here is the serialization code:
-(void)sendMessage:(NSString *)method:(NSDictionary *)inData {
NSDictionary *outData = [[NSDictionary alloc] init];
@try {
JsonServiceComm *newCom = [[JsonServiceComm alloc] init];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", host, method]];
NSData *json = [NSJSONSerialization dataWithJSONObject:inData options:kNilOptions error:nil];
json = [json subdataWithRange:NSMakeRange(0, [json length] - 1)];
NSString* jsData = [NSString stringWithUTF8String:[json bytes]];
NSString *outData = [newCom userSpaceRequest:url :jsData];
} @catch (NSException* ex) {
NSLog(@"%@", [ex reason]);
}
}
Here is where it is being deserialized:
-(void)handleMessage:(NSString *)messageType message:(NSString *)message {
@autoreleasepool {
NSData *jsMessage = [message dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:jsMessage options:NSJSONReadingMutableLeaves error:&error];
}}
If the data does not contain a copyright symbol, *data is populated with a dictionary of values, however if it does contain the copyright symbol, *data comes out nil. The error returned is "Unexpected end of file while parsing object."