Permalink
Browse files

[* JSON*]

  • Loading branch information...
1 parent 1f56a03 commit b5169078b210fd75eb76a26b34cd6ded3ba1a7a1 @mxcl committed Oct 4, 2012
Showing with 42 additions and 4 deletions.
  1. +4 −0 NSArray+mxcl.h
  2. +9 −0 NSArray+mxcl.m
  3. +4 −1 NSDictionary+mxcl.h
  4. +9 −3 NSDictionary+mxcl.m
  5. +5 −0 NSObject+mxcl.h
  6. +11 −0 NSObject+mxcl.m
View
@@ -13,6 +13,10 @@
// Do not use this if there a chance that all values are equal
// or if most of the values are equal.
- (id)shuffledArray;
+
+- (NSData *)JSONData;
+- (NSString *)JSONString;
+
@end
@interface NSMutableArray (mxcl)
View
@@ -57,6 +57,15 @@ - (id)shuffledArray {
}
}
}
+
+- (id)JSONData {
+ return [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];
+}
+
+- (id)JSONString {
+ return [[NSString alloc] initWithData:[self JSONData] encoding:NSUTF8StringEncoding];
+}
+
@end
View
@@ -3,6 +3,9 @@
@interface NSDictionary (mxcl)
// returns nil if not a string or if string length is zero after trimming whitespace from both ends
-- (id)massagedStringForKey:(id)key;
+- (NSString *)massagedStringForKey:(id)key;
+
+- (NSData *)JSONData;
+- (NSString *)JSONString;
@end
View
@@ -7,9 +7,15 @@ - (id)massagedStringForKey:(id)key {
if (![s isKindOfClass:[NSString class]])
return nil;
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- if ([s length] == 0)
- return nil;
- return s;
+ return s.length ? s : nil;
+}
+
+- (id)JSONData {
+ return [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];
+}
+
+- (id)JSONString {
+ return [[NSString alloc] initWithData:[self JSONData] encoding:NSUTF8StringEncoding];
}
@end
View
@@ -1,5 +1,10 @@
#import <Foundation/Foundation.h>
@interface NSObject (mxcl)
+
- (void)dumpSelectors;
+
++ (id)objectWithJSONData:(NSData *)json;
++ (id)objectWithJSONString:(NSString *)json;
+
@end
View
@@ -1,11 +1,22 @@
#import "NSObject+mxcl.h"
+#import <objc/runtime.h>
@implementation NSObject (mxcl)
+
- (void)dumpSelectors {
unsigned n = 0;
Method *methods = class_copyMethodList([self class], &n);
for (int i = 0; i < n; ++i)
NSLog(@"%@", NSStringFromSelector(method_getName(methods[i])));
free(methods);
}
+
++ (id)objectWithJSONData:(NSData *)json {
+ return [NSJSONSerialization JSONObjectWithData:json options:kNilOptions error:nil];
+}
+
++ (id)objectWithJSONString:(NSString *)json {
+ return [NSObject objectWithJSONData:[json dataUsingEncoding:NSUTF8StringEncoding]];
+}
+
@end

0 comments on commit b516907

Please sign in to comment.