-1

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."

  • 1
    Show a specific example of a string being passed to the `message` parameter of `handleMessage` that is causing the problem. And please show the complete line of code using `NSJSONSerialization JSONObjectWithData`. – rmaddy May 10 '19 at 00:07
  • That tends to indicate that it is corrupt JSON in the first place. Either the value is encoded incorrectly, or the data is not what you think it is. In the sendMessage method, you do not need to trim a byte off the data -- that will make it invalid JSON. So you don't need the subdataWithRange line. Outside of that, we can't see the raw value being passed to NSJSONSerialization. – Carl Lindberg May 12 '19 at 21:51

2 Answers2

0

This is resolved. I discovered that the json was being passed through a separate json parser where it was not being utf8 encoded.

-2

Try using options:0 instead of options:NSJSONReadingMutableLeaves

https://developer.apple.com/documentation/foundation/nsjsonreadingoptions/nsjsonreadingmutableleaves?language=objc

Kai
  • 1
  • 1