johnezang / JSONKit

Objective-C JSON
6.21k stars 1.66k forks source link

JSONDecoder parseJSONData returns string "<null>" for json's null value #24

Open edwardIshaq opened 13 years ago

edwardIshaq commented 13 years ago

Hi there, I started using JSONKit library and I really enjoyed the speed of the parsing. its really fast as you guys say. but I am having a problem when parsing a JSON with one of the values is null: ... "somekey" : null

the dictionary in return will show up as

... "somekey" = "";

is there a way to fix this locally for now ?

edwardIshaq commented 13 years ago

I have a fix for the problem. its in the _JKDictionaryAddObject function:

static void _JKDictionaryAddObject(JKDictionary dictionary, NSUInteger keyHash, id key, id object) { NSCParameterAssert((dictionary != NULL) && (key != NULL) && (object != NULL) && (dictionary->count < dictionary->capacity) && (dictionary->entry != NULL)); NSUInteger keyEntry = keyHash % dictionary->capacity, idx = 0UL; for(idx = 0UL; idx < dictionary->capacity; idx++) { NSUInteger entryIdx = (keyEntry + idx) % dictionary->capacity; JKHashTableEntry atEntry = &dictionary->entry[entryIdx]; if(JK_EXPECT_F(atEntry->keyHash == keyHash) && JK_EXPECT_T(atEntry->key != NULL) && (JK_EXPECT_F(key == atEntry->key) || JK_EXPECT_F(CFEqual(atEntry->key, key)))) { _JKDictionaryRemoveObjectWithEntry(dictionary, atEntry); } if(JK_EXPECT_T(atEntry->key == NULL)) { NSCParameterAssert((atEntry->keyHash == 0UL) && (atEntry->object == NULL)); atEntry->key = key; if ([object isMemberOfClass:[NSNull class]]) { atEntry->object = @""; } else { atEntry->object = object; } atEntry->keyHash = keyHash; dictionary->count++; return; } }

// We should never get here. If we do, we -release the key / object because it's our responsibility. CFRelease(key); CFRelease(object); }

johnezang commented 13 years ago

It's not clear what the problem is from your description. The -description for NSNull is "<null>". This is what a NSDictionary will print / use when do something like NSLog(@"Dictionary: %@", aDictionary);. This is the normal, expected behavior.

The "fix" you suggested, however, is broken- it replaces a NSNull / JSON null value with a string that has a length of 0 / contains no characters / is empty. This is wrong- [NSNull null] and @"" are two distinct objects, and mean very different things. This type of fix will not be committed to JSONKit.

edwardIshaq commented 13 years ago

the problem is when writing the dictionary into a file while having "" as one of its values. it will fail consistently. I am aware of the NSNull and @"" differences but specifically for NSDictionary this will solve the problem of writing to file.

johnezang commented 13 years ago

(... on why I haven't closed this issue ...)

I haven't closed this issue because I consider it to be a specific case of a broader "feature request".

In short, I consider this to fall under the umbrella of "on the fly value transformation and substitution during deserialization and serialization". A generic way of handing that is fairly complicated, and implementing it in a way that is elegant is doubly hard. It's not going to be something that gets dealt with any time soon. Under the broader Umbrella feature request definition, this issue is related to issue #25.