Comments (8)
Can you provide the JSON and some example code the demonstrates the problem?
From your description, it's not clear what the problem is. When you say "A call to removeObjectsForKeys will remove the objects but not the keys. So we end up with nil objects.", this is the expected behavior- when a NSDictionary
does not contain a key, -objectForKey:
will return nil
(aka NULL
).
from jsonkit.
Instead of removing the key/value pair as expected, the key remains in the mutable dictionary but the value is replaced by a null value. If I print the resulting object I can see: "myKey"=(null).
from jsonkit.
The behavior you describe is the expected behavior. The key does not "remain" in the dictionary, any key that does not exist in the dictionary returns NULL
(aka nil
). There are a number of internal checks in the form of NSCParameterAssert
that ensure that every key in the dictionary contains a non-NULL
object associated with it. If you did not compile JSONKit with NS_BLOCK_ASSERTIONS
, these checks are enabled, and would have caused an exception, which would have been logged to the console.
I suggest you try creating a NSMutableDictionary
with a number of keys/objects. Remove one of the keys, and see if the NSMutableDictionary
has the same behavior for the removed key.
from jsonkit.
Hey John,
I think what the OP is seeing is this (with JSONKit 2e6ccf4):
Code:
NSMutableDictionary *jkDict = [@"{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}" mutableObjectFromJSONString];
NSLog(@"jkDict = %@", jkDict);
NSMutableDictionary *objcDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"a", @"2", @"b", @"3", @"c", nil];
NSLog(@"objcDict = %@", objcDict);
[jkDict removeObjectForKey:@"b"];
[objcDict removeObjectForKey:@"b"];
NSLog(@"jkDict = %@", jkDict);
NSLog(@"objcDict = %@", objcDict);
Output:
2011-04-22 14:46:05.354 main[90231:903] jkDict = {
a = 1;
b = 2;
c = 3;
}
2011-04-22 14:46:05.357 main[90231:903] objcDict = {
a = 1;
b = 2;
c = 3;
}
2011-04-22 14:46:05.358 main[90231:903] jkDict = {
a = 1;
c = (null);
}
2011-04-22 14:46:05.358 main[90231:903] objcDict = {
a = 1;
c = 3;
}
from jsonkit.
Hey John,
I've tracked this down to the fix which fixed issue #8 :)
Specifically this line:
This comment:
// If the key was in the table, we would have found it by now.
Is incorrect if removeObjectForKey was used (like my example above).
dictionary->entry looks like this with the example:
[0] = {keyHash = 1062, key = @"a", object = "1"}
[1] = {keyHash = 0, key = NULL, object = NULL}
[2] = {keyHash = 1068, key = @"c", object = "3"}
So the loop terminates with NULL due to [1], but we need to keep going to find [2].
from jsonkit.
@omarkilani, the line you mentioned in your comment is one half of the problem. The real problem is in the removal logic- it (should have) "re-added" the items following the removed entry so that linear probe hash collisions didn't result in a NULL
gap in the middle of the linear probe hash collision.
Your example code was very useful- it happened to create a dictionary with 3 items with a capacity of 3 items, and a
, b
, and c
all just happened to be ≡ 0 mod 3. Sort of a "perfect storm" to exercise this bug.
I checked in a bug fix for this issue (used the new github issues feature to close this issue automagically with "Closes #17").
from jsonkit.
Thanks for the help reproducing the problem and fixing it.
Good job!
from jsonkit.
@johnezang, Thanks for the fix. Yeah, the real problem became obvious once I started delving in a bit more. I seem to get pretty lucky with just the right dictionaries to trigger things. :)
Thanks again.
from jsonkit.
Related Issues (20)
- Any popular replacement for JSONKit? HOT 2
- BSD two-clause or three-clause license? HOT 2
- crash at JSONKit.m:2598:77 HOT 2
- Error in serializing NSNumber HOT 2
- Supporting gzip decompression
- Swift HOT 7
- can not use with xcode 7 HOT 4
- Xcode 7 Before is use JSONKit in Object but now not use JSONKit in Xcode 7 HOT 7
- crash with iOS9 Xcode7 HOT 5
- Should be #import <JSONKit/JSONKit.h> or <GCJSONKit/JSONKit.h>? HOT 1
- please ignore
- Crash when NSDictionary to String json format. HOT 1
- about convert json to NSDictioanry (error if json string contains double field) HOT 7
- JsonKit message JSONDataWithOptions crashes in macOS Sierra HOT 1
- JSONKit Installation error in X-code 7.3.1 iOS 9.3 HOT 1
- iOS TRAP_TRACE crash
- JSONKit semantic issue after updating the pod HOT 3
- Tagged pointer format changes in iOS 14 break JSONKit on ARM64 HOT 8
- @serkrapiv Not necessarily verboten, just that they must be escaped. The first paragraph of 2.5 Strings says: HOT 1
- Yea
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonkit.