I have some data that is an array containing dictionaries with one key. When trying to decode the data I get an exception. See the following sample data:
"Gms":[{"Id":141635},{"Id":147175},{"Id":146000},{"Id":146205}]
I'd traced down the issue to "decodeArrayOfClass:". There is a line in there that does the following:
if([[innerDictionary allKeys] count] > 1){
innerDictionary = [NSDictionary dictionaryWithObject:[self topJsonObject] forKey:@"object"];
}
Basically, if there is only one key it's being ignored.
However, the fix isn't as simple as changing the conditional. This behavior seems intentional to support a feature I don't really understand. When you encode an array it gets translated to a dictionary with one key where the key is the class and the value is the actual array. So if your JSON input is "List":["one","two"] when you encode it the resulting JSON is "List":[{"__NSCFString":["one","two"]}]
In encodeObject:forKey: there is a conditional that seems to try to suppress this transformation, but as far as I can tell it never works because the conditional is always true:
if(![[className substringToIndex:2] isEqualToString:@"NS"] ||
![[className substringToIndex:2] isEqualToString:@"__"]){
In order for the conditional to fail, the class name would have to start with __ and also start with NS.
In general, it seems like this feature of camelstringing class names and making assuptions about dictionaries with 1 key isn't the way to solve whatever feature is being implemented. Maybe the JSONEncoder also needs something like addAlias so when it runs into that class in arrays and sets it can know how to create the json instead of using some inferred rule that seems to break other types of data.