NSJSONSerialization does not properly decode keys containing escaped unicode characters

Originator:cedric.luthi
Number:rdar://12830961 Date Originated:07-Dec-2012 01:31 AM
Status:Closed Resolved:
Product:iPhone SDK Product Version:6.0
Classification:Serious Bug Reproducible:Always
 
Summary:
NSJSONSerialization does not properly decode keys containing escaped unicode characters when these keys appear in several different dictionaries.

Steps to Reproduce:
1. Run this code
void test NSJSONSerialization(void)
{
    NSString *testFail1 = @"[{\"key\":5, \"key\\u00a9\":6}, {\"key\":7, \"key\\u00a9\":8}]";
    NSString *testFail2 = @"[{\"key\\u00a9\":6}, {\"key\\u00a9\":8}]";
    NSString *testOK = @"[{\"key\":5, \"key\u00a9\":6}, {\"key\":7, \"key\u00a9\":8}]";
    NSLog(@"testFail1: %@", testFail1);
    NSLog(@"testFail2: %@", testFail2);
    NSLog(@"testOK:    %@", testOK);
    int i = 1;
    for (NSString *json in @[ testFail1, testFail2, testOK ])
    {
        NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];
        printf("%d. %s\n", i, [[(object ?: error) description] UTF8String]);
        i++;
    }
}

Expected Results:
For testFail1 the result should be exactly the same as testOK, i.e.
(
        {
        key = 5;
        "key\U00a9" = 6;
    },
        {
        key = 7;
        "key\U00a9" = 8;
    }
)

For testFail2 the result should be:
(
        {
        "key\U00a9" = 6;
    },
        {
        "key\U00a9" = 8;
    }
)


Actual Results:
For testFail1 the second element of the array is completely wrong:
(
        {
        key = 5;
        "key\U00a9" = 6;
    },
        {
        key = 8;
    }
)

For testFail2, the key of the second element of the array is empty:
(
        {
        "key\U00a9" = 6;
    },
        {
        "" = 8;
    }
)


Regression:
The exact same problem also occurs on OS X 10.8.2

Notes:
The same problem was discussed here: http://stackoverflow.com/questions/12842481/nsjsonserialization-results-in-exc-bad-access

Duplicates

Number Status Originator Product Title
rdar://13256524 Duplicate/12830961 kirby iOS SDK NSJSONSerialization Crashes on Dup Field Names

Comments

11-Jun-2013 02:33 AM Apple Developer Bug Reporting Team

We believe this issue has been addressed in iOS 7 beta 1 (11A4372q). Please verify with this release, and update your report with the results.

By cedric.luthi at Aug. 21, 2013, 7:43 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!