Skip to content

Commit

Permalink
[* JSON*]
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Oct 4, 2012
1 parent 1f56a03 commit b516907
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions NSArray+mxcl.h
Expand Up @@ -13,6 +13,10 @@
// Do not use this if there a chance that all values are equal // Do not use this if there a chance that all values are equal
// or if most of the values are equal. // or if most of the values are equal.
- (id)shuffledArray; - (id)shuffledArray;

- (NSData *)JSONData;
- (NSString *)JSONString;

@end @end


@interface NSMutableArray (mxcl) @interface NSMutableArray (mxcl)
Expand Down
9 changes: 9 additions & 0 deletions NSArray+mxcl.m
Expand Up @@ -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 @end




Expand Down
5 changes: 4 additions & 1 deletion NSDictionary+mxcl.h
Expand Up @@ -3,6 +3,9 @@
@interface NSDictionary (mxcl) @interface NSDictionary (mxcl)


// returns nil if not a string or if string length is zero after trimming whitespace from both ends // 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 @end
12 changes: 9 additions & 3 deletions NSDictionary+mxcl.m
Expand Up @@ -7,9 +7,15 @@ - (id)massagedStringForKey:(id)key {
if (![s isKindOfClass:[NSString class]]) if (![s isKindOfClass:[NSString class]])
return nil; return nil;
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([s length] == 0) return s.length ? s : nil;
return nil; }
return s;
- (id)JSONData {
return [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:nil];
}

- (id)JSONString {
return [[NSString alloc] initWithData:[self JSONData] encoding:NSUTF8StringEncoding];
} }


@end @end
5 changes: 5 additions & 0 deletions NSObject+mxcl.h
@@ -1,5 +1,10 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>


@interface NSObject (mxcl) @interface NSObject (mxcl)

- (void)dumpSelectors; - (void)dumpSelectors;

+ (id)objectWithJSONData:(NSData *)json;
+ (id)objectWithJSONString:(NSString *)json;

@end @end
11 changes: 11 additions & 0 deletions NSObject+mxcl.m
@@ -1,11 +1,22 @@
#import "NSObject+mxcl.h" #import "NSObject+mxcl.h"
#import <objc/runtime.h>


@implementation NSObject (mxcl) @implementation NSObject (mxcl)

- (void)dumpSelectors { - (void)dumpSelectors {
unsigned n = 0; unsigned n = 0;
Method *methods = class_copyMethodList([self class], &n); Method *methods = class_copyMethodList([self class], &n);
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
NSLog(@"%@", NSStringFromSelector(method_getName(methods[i]))); NSLog(@"%@", NSStringFromSelector(method_getName(methods[i])));
free(methods); 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 @end

0 comments on commit b516907

Please sign in to comment.