Created
November 13, 2012 21:51
-
-
Save mattt/4068640 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static NSInteger NSJSONReadingFuckNSNulls = (1UL << 3); | |
@implementation NSJSONSerialization (FuckNulls) | |
+ (void)load { | |
Method originalMethod = class_getClassMethod(self, @selector(JSONObjectWithData:options:error:)); | |
IMP swizzledImplementation = imp_implementationWithBlock([^id (id _self, NSData *_data, NSJSONReadingOptions _opt, NSError **_error){ | |
NSInputStream *stream = [NSInputStream inputStreamWithData:_data]; | |
[stream open]; | |
id JSON = [self JSONObjectWithStream:stream options:_opt error:_error]; | |
if ((_opt & NSJSONReadingFuckNSNulls) && [JSON isKindOfClass:[NSDictionary class]]) { | |
NSMutableDictionary *mutableJSON = [JSON mutableCopy]; | |
[JSON enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
if ([obj isEqual:[NSNull null]]) { | |
[mutableJSON removeObjectForKey:key]; | |
} | |
}]; | |
JSON = mutableJSON; | |
} | |
return JSON; | |
} copy]); | |
method_setImplementation(originalMethod, swizzledImplementation); | |
} | |
@end |
i love you. i first come about nsnull today and spent the last 5 hours trying to figure it out. eventually after i did i just searched google "fuck nsnull". thank you for this.
fuck nsnull
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would use a much higher constant for
NSJSONReadingFuckNSNulls
than (1UL << 3). If Apple adds a reading option il will most probably be (1UL << 3) but it might not be Fuck NSNulls.