Permalink
Browse files

Tabs to spaces:

for i in `find **/*.h **/*.m`; do echo "Expanding $i" && expand -t 2
  $i >$i.tmp && mv $i.tmp $i; done
  • Loading branch information...
1 parent 7e4fa59 commit 78fb38fdf3f0e868665bc3f7fce33696e5414236 @gabriel committed Apr 15, 2010
View
48 Classes/NSObject+YAJL.m
@@ -36,44 +36,44 @@ @implementation NSObject (YAJL)
#pragma mark Gen
- (NSString *)yajl_JSONString {
- return [self yajl_JSONStringWithOptions:YAJLGenOptionsNone indentString:@" "];
+ return [self yajl_JSONStringWithOptions:YAJLGenOptionsNone indentString:@" "];
}
- (NSString *)yajl_JSONStringWithOptions:(YAJLGenOptions)options indentString:(NSString *)indentString {
- YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:options indentString:indentString];
- [gen object:self];
- NSString *buffer = [[gen buffer] retain];
- [gen release];
- return [buffer autorelease];
+ YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:options indentString:indentString];
+ [gen object:self];
+ NSString *buffer = [[gen buffer] retain];
+ [gen release];
+ return [buffer autorelease];
}
#pragma mark Parsing
- (id)yajl_JSON {
- NSError *error = nil;
- id JSON = [self yajl_JSON:&error];
- if (error) [NSException raise:YAJLParserException format:[error localizedDescription]];
- return JSON;
+ NSError *error = nil;
+ id JSON = [self yajl_JSON:&error];
+ if (error) [NSException raise:YAJLParserException format:[error localizedDescription]];
+ return JSON;
}
- (id)yajl_JSON:(NSError **)error {
- return [self yajl_JSONWithOptions:YAJLParserOptionsNone error:error];
+ return [self yajl_JSONWithOptions:YAJLParserOptionsNone error:error];
}
- (id)yajl_JSONWithOptions:(YAJLParserOptions)options error:(NSError **)error {
- NSData *data = nil;
- if ([self isKindOfClass:[NSData class]]) {
- data = (NSData *)self;
- } else if ([self respondsToSelector:@selector(dataUsingEncoding:)]) {
- data = [(id)self dataUsingEncoding:NSUTF8StringEncoding];
- } else {
- [NSException raise:YAJLParsingUnsupportedException format:@"Object of type (%@) must implement dataUsingEncoding: to be parsed", [self class]];
- }
-
- YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:options error:error];
- id root = [document.root retain];
- [document release];
- return [root autorelease];
+ NSData *data = nil;
+ if ([self isKindOfClass:[NSData class]]) {
+ data = (NSData *)self;
+ } else if ([self respondsToSelector:@selector(dataUsingEncoding:)]) {
+ data = [(id)self dataUsingEncoding:NSUTF8StringEncoding];
+ } else {
+ [NSException raise:YAJLParsingUnsupportedException format:@"Object of type (%@) must implement dataUsingEncoding: to be parsed", [self class]];
+ }
+
+ YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:options error:error];
+ id root = [document.root retain];
+ [document release];
+ return [root autorelease];
}
View
36 Classes/YAJLDocument.h
@@ -31,9 +31,9 @@
#include "YAJLParser.h"
typedef enum {
- YAJLDecoderCurrentTypeNone,
- YAJLDecoderCurrentTypeArray,
- YAJLDecoderCurrentTypeDict
+ YAJLDecoderCurrentTypeNone,
+ YAJLDecoderCurrentTypeArray,
+ YAJLDecoderCurrentTypeDict
} YAJLDecoderCurrentType;
extern NSInteger YAJLDocumentStackCapacity;
@@ -49,22 +49,22 @@ extern NSInteger YAJLDocumentStackCapacity;
@end
@interface YAJLDocument : NSObject <YAJLParserDelegate> {
-
- id root_; // NSArray or NSDictionary
- YAJLParser *parser_;
-
+
+ id root_; // NSArray or NSDictionary
+ YAJLParser *parser_;
+
__weak id<YAJLDocumentDelegate> delegate_;
-
- __weak NSMutableDictionary *dict_; // weak; if map in progress, points to the current map
- __weak NSMutableArray *array_; // weak; If array in progress, points the current array
- __weak NSString *key_; // weak; If map in progress, points to current key
-
- NSMutableArray *stack_;
- NSMutableArray *keyStack_;
-
- YAJLDecoderCurrentType currentType_;
-
- YAJLParserStatus parserStatus_;
+
+ __weak NSMutableDictionary *dict_; // weak; if map in progress, points to the current map
+ __weak NSMutableArray *array_; // weak; If array in progress, points the current array
+ __weak NSString *key_; // weak; If map in progress, points to current key
+
+ NSMutableArray *stack_;
+ NSMutableArray *keyStack_;
+
+ YAJLDecoderCurrentType currentType_;
+
+ YAJLParserStatus parserStatus_;
}
View
142 Classes/YAJLDocument.m
@@ -46,122 +46,122 @@ - (id)init {
}
- (id)initWithParserOptions:(YAJLParserOptions)parserOptions {
- if ((self = [super init])) {
- stack_ = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
- keyStack_ = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
- parserStatus_ = YAJLParserStatusNone;
- parser_ = [[YAJLParser alloc] initWithParserOptions:parserOptions];
- parser_.delegate = self;
- }
- return self;
+ if ((self = [super init])) {
+ stack_ = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
+ keyStack_ = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
+ parserStatus_ = YAJLParserStatusNone;
+ parser_ = [[YAJLParser alloc] initWithParserOptions:parserOptions];
+ parser_.delegate = self;
+ }
+ return self;
}
- (id)initWithData:(NSData *)data parserOptions:(YAJLParserOptions)parserOptions error:(NSError **)error {
- if ((self = [self initWithParserOptions:parserOptions])) {
- [self parse:data error:error];
- }
- return self;
+ if ((self = [self initWithParserOptions:parserOptions])) {
+ [self parse:data error:error];
+ }
+ return self;
}
- (void)dealloc {
- [stack_ release];
- [keyStack_ release];
- parser_.delegate = nil;
- [parser_ release];
- [root_ release];
- [super dealloc];
+ [stack_ release];
+ [keyStack_ release];
+ parser_.delegate = nil;
+ [parser_ release];
+ [root_ release];
+ [super dealloc];
}
- (YAJLParserStatus)parse:(NSData *)data error:(NSError **)error {
- parserStatus_ = [parser_ parse:data];
- if (error) *error = [parser_ parserError];
- return parserStatus_;
+ parserStatus_ = [parser_ parse:data];
+ if (error) *error = [parser_ parserError];
+ return parserStatus_;
}
#pragma mark Delegates
- (void)parser:(YAJLParser *)parser didAdd:(id)value {
- switch(currentType_) {
- case YAJLDecoderCurrentTypeArray:
- [array_ addObject:value];
+ switch(currentType_) {
+ case YAJLDecoderCurrentTypeArray:
+ [array_ addObject:value];
if ([delegate_ respondsToSelector:@selector(document:didAddObject:toArray:)])
[delegate_ document:self didAddObject:value toArray:array_];
- break;
- case YAJLDecoderCurrentTypeDict:
- NSParameterAssert(key_);
- [dict_ setObject:value forKey:key_];
+ break;
+ case YAJLDecoderCurrentTypeDict:
+ NSParameterAssert(key_);
+ [dict_ setObject:value forKey:key_];
if ([delegate_ respondsToSelector:@selector(document:didSetObject:forKey:inDictionary:)])
[delegate_ document:self didSetObject:value forKey:key_ inDictionary:dict_];
- [self _popKey];
- break;
- }
+ [self _popKey];
+ break;
+ }
}
- (void)parser:(YAJLParser *)parser didMapKey:(NSString *)key {
- key_ = key;
- [keyStack_ addObject:key_]; // Push
+ key_ = key;
+ [keyStack_ addObject:key_]; // Push
}
- (void)_popKey {
- key_ = nil;
- [keyStack_ removeLastObject]; // Pop
- if ([keyStack_ count] > 0)
- key_ = [keyStack_ objectAtIndex:[keyStack_ count]-1];
+ key_ = nil;
+ [keyStack_ removeLastObject]; // Pop
+ if ([keyStack_ count] > 0)
+ key_ = [keyStack_ objectAtIndex:[keyStack_ count]-1];
}
- (void)parserDidStartDictionary:(YAJLParser *)parser {
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:YAJLDocumentStackCapacity];
- if (!root_) root_ = [dict retain];
- [stack_ addObject:dict]; // Push
- [dict release];
- dict_ = dict;
- currentType_ = YAJLDecoderCurrentTypeDict;
+ NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:YAJLDocumentStackCapacity];
+ if (!root_) root_ = [dict retain];
+ [stack_ addObject:dict]; // Push
+ [dict release];
+ dict_ = dict;
+ currentType_ = YAJLDecoderCurrentTypeDict;
}
- (void)parserDidEndDictionary:(YAJLParser *)parser {
- id value = [[stack_ objectAtIndex:[stack_ count]-1] retain];
+ id value = [[stack_ objectAtIndex:[stack_ count]-1] retain];
NSDictionary *dict = dict_;
- [self _pop];
- [self parser:parser didAdd:value];
- [value release];
+ [self _pop];
+ [self parser:parser didAdd:value];
+ [value release];
if ([delegate_ respondsToSelector:@selector(document:didAddDictionary:)])
[delegate_ document:self didAddDictionary:dict];
}
- (void)parserDidStartArray:(YAJLParser *)parser {
- NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
- if (!root_) root_ = [array retain];
- [stack_ addObject:array]; // Push
- [array release];
- array_ = array;
- currentType_ = YAJLDecoderCurrentTypeArray;
+ NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:YAJLDocumentStackCapacity];
+ if (!root_) root_ = [array retain];
+ [stack_ addObject:array]; // Push
+ [array release];
+ array_ = array;
+ currentType_ = YAJLDecoderCurrentTypeArray;
}
- (void)parserDidEndArray:(YAJLParser *)parser {
- id value = [[stack_ objectAtIndex:[stack_ count]-1] retain];
+ id value = [[stack_ objectAtIndex:[stack_ count]-1] retain];
NSArray *array = array_;
- [self _pop];
- [self parser:parser didAdd:value];
- [value release];
+ [self _pop];
+ [self parser:parser didAdd:value];
+ [value release];
if ([delegate_ respondsToSelector:@selector(document:didAddArray:)])
[delegate_ document:self didAddArray:array];
}
- (void)_pop {
- [stack_ removeLastObject];
- array_ = nil;
- dict_ = nil;
- currentType_ = YAJLDecoderCurrentTypeNone;
-
- id value = nil;
- if ([stack_ count] > 0) value = [stack_ objectAtIndex:[stack_ count]-1];
- if ([value isKindOfClass:[NSArray class]]) {
- array_ = (NSMutableArray *)value;
- currentType_ = YAJLDecoderCurrentTypeArray;
- } else if ([value isKindOfClass:[NSDictionary class]]) {
- dict_ = (NSMutableDictionary *)value;
- currentType_ = YAJLDecoderCurrentTypeDict;
- }
+ [stack_ removeLastObject];
+ array_ = nil;
+ dict_ = nil;
+ currentType_ = YAJLDecoderCurrentTypeNone;
+
+ id value = nil;
+ if ([stack_ count] > 0) value = [stack_ objectAtIndex:[stack_ count]-1];
+ if ([value isKindOfClass:[NSArray class]]) {
+ array_ = (NSMutableArray *)value;
+ currentType_ = YAJLDecoderCurrentTypeArray;
+ } else if ([value isKindOfClass:[NSDictionary class]]) {
+ dict_ = (NSMutableDictionary *)value;
+ currentType_ = YAJLDecoderCurrentTypeDict;
+ }
}
@end
View
6 Classes/YAJLGen.h
@@ -36,8 +36,8 @@ extern NSString *const YAJLGenInvalidObjectException;
@constant YAJLGenOptionsBeautify
*/
enum {
- YAJLGenOptionsNone = 0,
- YAJLGenOptionsBeautify = 1 << 0,
+ YAJLGenOptionsNone = 0,
+ YAJLGenOptionsBeautify = 1 << 0,
YAJLGenOptionsIgnoreUnknownTypes = 1 << 1, // Ignore unknown types (will use null value)
YAJLGenOptionsIncludeUnsupportedTypes = 1 << 2, // Handle non-JSON types (including NSDate, NSData, NSURL)
};
@@ -59,7 +59,7 @@ typedef NSUInteger YAJLGenOptions;
- NSURL -> URL (absolute) string
*/
@interface YAJLGen : NSObject {
- yajl_gen gen_;
+ yajl_gen gen_;
YAJLGenOptions genOptions_;
}
View
118 Classes/YAJLGen.m
@@ -35,52 +35,52 @@
@implementation YAJLGen
- (id)init {
- return [self initWithGenOptions:YAJLGenOptionsNone indentString:@""];
+ return [self initWithGenOptions:YAJLGenOptionsNone indentString:@""];
}
- (id)initWithGenOptions:(YAJLGenOptions)genOptions indentString:(NSString *)indentString {
- if ((self = [super init])) {
+ if ((self = [super init])) {
genOptions_ = genOptions;
- yajl_gen_config cfg = {
- ((genOptions & YAJLGenOptionsBeautify) ? 1 : 0),
- [indentString UTF8String]
- };
- gen_ = yajl_gen_alloc(&cfg, NULL);
- }
- return self;
+ yajl_gen_config cfg = {
+ ((genOptions & YAJLGenOptionsBeautify) ? 1 : 0),
+ [indentString UTF8String]
+ };
+ gen_ = yajl_gen_alloc(&cfg, NULL);
+ }
+ return self;
}
-- (void)dealloc {
- if (gen_ != NULL) yajl_gen_free(gen_);
- [super dealloc];
+- (void)dealloc {
+ if (gen_ != NULL) yajl_gen_free(gen_);
+ [super dealloc];
}
-- (void)object:(id)obj {
- if ([obj respondsToSelector:@selector(JSON)]) {
- return [self object:[obj JSON]];
- } else if ([obj isKindOfClass:[NSArray class]]) {
- [self startArray];
- for(id element in obj)
- [self object:element];
- [self endArray];
- } else if ([obj isKindOfClass:[NSDictionary class]]) {
- [self startDictionary];
- for(id key in obj) {
- [self object:key];
- [self object:[obj objectForKey:key]];
- }
- [self endDictionary];
- } else if ([obj isKindOfClass:[NSNumber class]]) {
- if ('c' != *[obj objCType]) {
- [self number:obj];
- } else {
- [self bool:[obj boolValue]];
- }
- } else if ([obj isKindOfClass:[NSString class]]) {
- [self string:obj];
- } else if ([obj isKindOfClass:[NSNull class]]) {
- [self null];
- } else {
+- (void)object:(id)obj {
+ if ([obj respondsToSelector:@selector(JSON)]) {
+ return [self object:[obj JSON]];
+ } else if ([obj isKindOfClass:[NSArray class]]) {
+ [self startArray];
+ for(id element in obj)
+ [self object:element];
+ [self endArray];
+ } else if ([obj isKindOfClass:[NSDictionary class]]) {
+ [self startDictionary];
+ for(id key in obj) {
+ [self object:key];
+ [self object:[obj objectForKey:key]];
+ }
+ [self endDictionary];
+ } else if ([obj isKindOfClass:[NSNumber class]]) {
+ if ('c' != *[obj objCType]) {
+ [self number:obj];
+ } else {
+ [self bool:[obj boolValue]];
+ }
+ } else if ([obj isKindOfClass:[NSString class]]) {
+ [self string:obj];
+ } else if ([obj isKindOfClass:[NSNull class]]) {
+ [self null];
+ } else {
BOOL unknownType = NO;
if (genOptions_ & YAJLGenOptionsIncludeUnsupportedTypes) {
@@ -106,57 +106,57 @@ - (void)object:(id)obj {
[self null]; // Use null value for unknown type if we are ignoring
}
}
- }
+ }
}
- (void)null {
- yajl_gen_null(gen_);
+ yajl_gen_null(gen_);
}
- (void)bool:(BOOL)b {
- yajl_gen_bool(gen_, b);
+ yajl_gen_bool(gen_, b);
}
- (void)number:(NSNumber *)number {
- NSString *s = [number stringValue];
- unsigned int length = [s lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- const char *c = [s UTF8String];
- yajl_gen_number(gen_, c, length);
+ NSString *s = [number stringValue];
+ unsigned int length = [s lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ const char *c = [s UTF8String];
+ yajl_gen_number(gen_, c, length);
}
- (void)string:(NSString *)s {
- unsigned int length = [s lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- const unsigned char *c = (const unsigned char *)[s UTF8String];
- yajl_gen_string(gen_, c, length);
+ unsigned int length = [s lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+ const unsigned char *c = (const unsigned char *)[s UTF8String];
+ yajl_gen_string(gen_, c, length);
}
- (void)startDictionary {
- yajl_gen_map_open(gen_);
+ yajl_gen_map_open(gen_);
}
- (void)endDictionary {
- yajl_gen_map_close(gen_);
+ yajl_gen_map_close(gen_);
}
- (void)startArray {
- yajl_gen_array_open(gen_);
+ yajl_gen_array_open(gen_);
}
- (void)endArray {
- yajl_gen_array_close(gen_);
+ yajl_gen_array_close(gen_);
}
- (void)clear {
- yajl_gen_clear(gen_);
+ yajl_gen_clear(gen_);
}
- (NSString *)buffer {
- const unsigned char *buf;
- unsigned int len;
- yajl_gen_get_buf(gen_, &buf, &len);
- NSString *s = [NSString stringWithUTF8String:(const char*)buf];
- return s;
-}
+ const unsigned char *buf;
+ unsigned int len;
+ yajl_gen_get_buf(gen_, &buf, &len);
+ NSString *s = [NSString stringWithUTF8String:(const char*)buf];
+ return s;
+}
@end
View
30 Classes/YAJLParser.h
@@ -55,18 +55,18 @@ typedef enum {
@constant YAJLParserOptionsCheckUTF8 Invalid UTF8 strings will cause a parse error
*/
enum {
- YAJLParserOptionsNone = 0,
- YAJLParserOptionsAllowComments = 1 << 0, // Allows comments in JSON
- YAJLParserOptionsCheckUTF8 = 1 << 1, // If YES will verify UTF-8
+ YAJLParserOptionsNone = 0,
+ YAJLParserOptionsAllowComments = 1 << 0, // Allows comments in JSON
+ YAJLParserOptionsCheckUTF8 = 1 << 1, // If YES will verify UTF-8
YAJLParserOptionsStrictPrecision = 1 << 2, // If YES will force strict precision and return integer overflow error
};
typedef NSUInteger YAJLParserOptions;
enum {
- YAJLParserStatusNone = 0,
- YAJLParserStatusOK = 1,
- YAJLParserStatusInsufficientData = 2,
- YAJLParserStatusError = 3
+ YAJLParserStatusNone = 0,
+ YAJLParserStatusOK = 1,
+ YAJLParserStatusInsufficientData = 2,
+ YAJLParserStatusError = 3
};
typedef NSUInteger YAJLParserStatus;
@@ -95,14 +95,14 @@ typedef NSUInteger YAJLParserStatus;
@interface YAJLParser : NSObject {
-
- yajl_handle handle_;
-
- id <YAJLParserDelegate> delegate_; // weak
-
- YAJLParserOptions parserOptions_;
-
- NSError *parserError_;
+
+ yajl_handle handle_;
+
+ id <YAJLParserDelegate> delegate_; // weak
+
+ YAJLParserOptions parserOptions_;
+
+ NSError *parserError_;
}
@property (assign, nonatomic) id <YAJLParserDelegate> delegate;
View
190 Classes/YAJLParser.m
@@ -65,46 +65,46 @@ - (id)init {
}
- (id)initWithParserOptions:(YAJLParserOptions)parserOptions {
- if ((self = [super init])) {
- parserOptions_ = parserOptions;
- }
- return self;
+ if ((self = [super init])) {
+ parserOptions_ = parserOptions;
+ }
+ return self;
}
- (void)dealloc {
- if (handle_ != NULL) {
- yajl_free(handle_);
- handle_ = NULL;
- }
-
- [parserError_ release];
- [super dealloc];
+ if (handle_ != NULL) {
+ yajl_free(handle_);
+ handle_ = NULL;
+ }
+
+ [parserError_ release];
+ [super dealloc];
}
#pragma mark Error Helpers
- (NSError *)_errorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value {
- NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey];
+ NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:message forKey:NSLocalizedDescriptionKey];
if (value) [userInfo setObject:value forKey:YAJLParserValueKey];
- return [NSError errorWithDomain:YAJLErrorDomain code:code userInfo:userInfo];
+ return [NSError errorWithDomain:YAJLErrorDomain code:code userInfo:userInfo];
}
- (void)_cancelWithErrorForStatus:(NSInteger)code message:(NSString *)message value:(NSString *)value {
- self.parserError = [self _errorForStatus:code message:message value:value];
+ self.parserError = [self _errorForStatus:code message:message value:value];
}
#pragma mark YAJL Callbacks
int yajl_null(void *ctx) {
- [(id)ctx _add:[NSNull null]];
- return 1;
+ [(id)ctx _add:[NSNull null]];
+ return 1;
}
int yajl_boolean(void *ctx, int boolVal) {
- NSNumber *number = [[NSNumber alloc] initWithBool:(BOOL)boolVal];
- [(id)ctx _add:number];
- [number release];
- return 1;
+ NSNumber *number = [[NSNumber alloc] initWithBool:(BOOL)boolVal];
+ [(id)ctx _add:number];
+ [number release];
+ return 1;
}
// Instead of using yajl_integer, and yajl_double we use yajl_number and parse
@@ -113,13 +113,13 @@ int yajl_boolean(void *ctx, int boolVal) {
// and is up to the parser?
//int yajl_integer(void *ctx, long integerVal) {
-// [(id)ctx _add:[NSNumber numberWithLong:integerVal]];
-// return 1;
+// [(id)ctx _add:[NSNumber numberWithLong:integerVal]];
+// return 1;
//}
//
//int yajl_double(void *ctx, double doubleVal) {
-// [(id)ctx _add:[NSNumber numberWithDouble:doubleVal]];
-// return 1;
+// [(id)ctx _add:[NSNumber numberWithDouble:doubleVal]];
+// return 1;
//}
int ParseDouble(void *ctx, const char *buf, const char *numberVal, unsigned int numberLen) {
@@ -137,15 +137,15 @@ int ParseDouble(void *ctx, const char *buf, const char *numberVal, unsigned int
}
int yajl_number(void *ctx, const char *numberVal, unsigned int numberLen) {
- char buf[numberLen+1];
- memcpy(buf, numberVal, numberLen);
- buf[numberLen] = 0;
-
- if (memchr(numberVal, '.', numberLen) || memchr(numberVal, 'e', numberLen) || memchr(numberVal, 'E', numberLen)) {
+ char buf[numberLen+1];
+ memcpy(buf, numberVal, numberLen);
+ buf[numberLen] = 0;
+
+ if (memchr(numberVal, '.', numberLen) || memchr(numberVal, 'e', numberLen) || memchr(numberVal, 'E', numberLen)) {
return ParseDouble(ctx, buf, numberVal, numberLen);
- } else {
- long long i = strtoll((const char *) buf, NULL, 10);
- if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE) {
+ } else {
+ long long i = strtoll((const char *) buf, NULL, 10);
+ if ((i == LLONG_MIN || i == LLONG_MAX) && errno == ERANGE) {
if (([(id)ctx parserOptions] & YAJLParserOptionsStrictPrecision) == YAJLParserOptionsStrictPrecision) {
NSString *s = [[NSString alloc] initWithBytes:numberVal length:numberLen encoding:NSUTF8StringEncoding];
[(id)ctx _cancelWithErrorForStatus:YAJLParserErrorCodeIntegerOverflow message:[NSString stringWithFormat:@"integer overflow on '%@'", s] value:s];
@@ -155,47 +155,47 @@ int yajl_number(void *ctx, const char *numberVal, unsigned int numberLen) {
// If we integer overflow lets try double precision for HUGE_VAL > double > LLONG_MAX
return ParseDouble(ctx, buf, numberVal, numberLen);
}
- }
- NSNumber *number = [[NSNumber alloc] initWithLongLong:i];
- [(id)ctx _add:number];
- [number release];
- }
-
- return 1;
+ }
+ NSNumber *number = [[NSNumber alloc] initWithLongLong:i];
+ [(id)ctx _add:number];
+ [number release];
+ }
+
+ return 1;
}
int yajl_string(void *ctx, const unsigned char *stringVal, unsigned int stringLen) {
- NSString *s = [[NSString alloc] initWithBytes:stringVal length:stringLen encoding:NSUTF8StringEncoding];
- [(id)ctx _add:s];
- [s release];
- return 1;
+ NSString *s = [[NSString alloc] initWithBytes:stringVal length:stringLen encoding:NSUTF8StringEncoding];
+ [(id)ctx _add:s];
+ [s release];
+ return 1;
}
int yajl_map_key(void *ctx, const unsigned char *stringVal, unsigned int stringLen) {
- NSString *s = [[NSString alloc] initWithBytes:stringVal length:stringLen encoding:NSUTF8StringEncoding];
- [(id)ctx _mapKey:s];
- [s release];
- return 1;
+ NSString *s = [[NSString alloc] initWithBytes:stringVal length:stringLen encoding:NSUTF8StringEncoding];
+ [(id)ctx _mapKey:s];
+ [s release];
+ return 1;
}
int yajl_start_map(void *ctx) {
- [(id)ctx _startDictionary];
- return 1;
+ [(id)ctx _startDictionary];
+ return 1;
}
int yajl_end_map(void *ctx) {
- [(id)ctx _endDictionary];
- return 1;
+ [(id)ctx _endDictionary];
+ return 1;
}
int yajl_start_array(void *ctx) {
- [(id)ctx _startArray];
- return 1;
+ [(id)ctx _startArray];
+ return 1;
}
int yajl_end_array(void *ctx) {
- [(id)ctx _endArray];
- return 1;
+ [(id)ctx _endArray];
+ return 1;
}
static yajl_callbacks callbacks = {
@@ -215,63 +215,63 @@ int yajl_end_array(void *ctx) {
#pragma mark -
- (void)_add:(id)value {
- [delegate_ parser:self didAdd:value];
+ [delegate_ parser:self didAdd:value];
}
- (void)_mapKey:(NSString *)key {
- [delegate_ parser:self didMapKey:key];
+ [delegate_ parser:self didMapKey:key];
}
- (void)_startDictionary {
- [delegate_ parserDidStartDictionary:self];
+ [delegate_ parserDidStartDictionary:self];
}
- (void)_endDictionary {
- [delegate_ parserDidEndDictionary:self];
+ [delegate_ parserDidEndDictionary:self];
}
-- (void)_startArray {
- [delegate_ parserDidStartArray:self];
+- (void)_startArray {
+ [delegate_ parserDidStartArray:self];
}
- (void)_endArray {
- [delegate_ parserDidEndArray:self];
+ [delegate_ parserDidEndArray:self];
}
- (YAJLParserStatus)parse:(NSData *)data {
- if (!handle_) {
- yajl_parser_config cfg = {
- ((parserOptions_ & YAJLParserOptionsAllowComments) ? 1 : 0), // allowComments: if nonzero, javascript style comments will be allowed in the input (both /* */ and //)
- ((parserOptions_ & YAJLParserOptionsCheckUTF8) ? 1 : 0) // checkUTF8: if nonzero, invalid UTF8 strings will cause a parse error
- };
-
- handle_ = yajl_alloc(&callbacks, &cfg, NULL, self);
- if (!handle_) {
- self.parserError = [self _errorForStatus:YAJLParserErrorCodeAllocError message:@"Unable to allocate YAJL handle" value:nil];
- return YAJLParserStatusError;
- }
- }
-
- yajl_status status = yajl_parse(handle_, [data bytes], [data length]);
- if (status == yajl_status_client_canceled) {
- // We cancelled because we encountered an error here in the client;
- // and parserError should be already set
- NSAssert(self.parserError, @"Client cancelled, but we have no parserError set");
- return YAJLParserStatusError;
- } else if (status == yajl_status_error) {
- unsigned char *errorMessage = yajl_get_error(handle_, 1, [data bytes], [data length]);
- NSString *errorString = [NSString stringWithUTF8String:(char *)errorMessage];
- self.parserError = [self _errorForStatus:status message:errorString value:nil];
- yajl_free_error(handle_, errorMessage);
- return YAJLParserStatusError;
- } else if (status == yajl_status_insufficient_data) {
- return YAJLParserStatusInsufficientData;
- } else if (status == yajl_status_ok) {
- return YAJLParserStatusOK;
- } else {
- self.parserError = [self _errorForStatus:status message:[NSString stringWithFormat:@"Unexpected status %d", status] value:nil];
- return YAJLParserStatusError;
- }
+ if (!handle_) {
+ yajl_parser_config cfg = {
+ ((parserOptions_ & YAJLParserOptionsAllowComments) ? 1 : 0), // allowComments: if nonzero, javascript style comments will be allowed in the input (both /* */ and //)
+ ((parserOptions_ & YAJLParserOptionsCheckUTF8) ? 1 : 0) // checkUTF8: if nonzero, invalid UTF8 strings will cause a parse error
+ };
+
+ handle_ = yajl_alloc(&callbacks, &cfg, NULL, self);
+ if (!handle_) {
+ self.parserError = [self _errorForStatus:YAJLParserErrorCodeAllocError message:@"Unable to allocate YAJL handle" value:nil];
+ return YAJLParserStatusError;
+ }
+ }
+
+ yajl_status status = yajl_parse(handle_, [data bytes], [data length]);
+ if (status == yajl_status_client_canceled) {
+ // We cancelled because we encountered an error here in the client;
+ // and parserError should be already set
+ NSAssert(self.parserError, @"Client cancelled, but we have no parserError set");
+ return YAJLParserStatusError;
+ } else if (status == yajl_status_error) {
+ unsigned char *errorMessage = yajl_get_error(handle_, 1, [data bytes], [data length]);
+ NSString *errorString = [NSString stringWithUTF8String:(char *)errorMessage];
+ self.parserError = [self _errorForStatus:status message:errorString value:nil];
+ yajl_free_error(handle_, errorMessage);
+ return YAJLParserStatusError;
+ } else if (status == yajl_status_insufficient_data) {
+ return YAJLParserStatusInsufficientData;
+ } else if (status == yajl_status_ok) {
+ return YAJLParserStatusOK;
+ } else {
+ self.parserError = [self _errorForStatus:status message:[NSString stringWithFormat:@"Unexpected status %d", status] value:nil];
+ return YAJLParserStatusError;
+ }
}
@end
View
4 SBJSON/NSString+SBJSON.m
@@ -57,7 +57,7 @@ - (id)JSONValueWithOptions:(NSDictionary *)opts
[NSException raise:@"enojson"
format:@"Failed to parse '%@' as JSON", self];
- return nil;
+ return nil;
}
- (id)JSONFragmentValue
@@ -73,7 +73,7 @@ - (id)JSONFragmentValue
[NSException raise:@"enofragment"
format:@"Failed to parse '%@' as a JSON fragment", self];
- return nil;
+ return nil;
}
@end
View
4 SBJSON/SBJSONScanner.m
@@ -214,7 +214,7 @@ - (BOOL)scanRestOfArray:(NSMutableArray **)o
} while (*c == ',' && c++);
[self raise:enocomma format:@"Expected , or ] while parsing array"];
- return NO; // got rid of compiler warning
+ return NO; // got rid of compiler warning
}
- (BOOL)scanDictionary:(NSDictionary **)o
@@ -265,7 +265,7 @@ - (BOOL)scanRestOfDictionary:(NSMutableDictionary **)o
} while (*c == ',' && c++);
[self raise:enocomma format:@"Expected , or } while parsing dictionary"];
- return NO; // JAE just got rid of compiler warning.
+ return NO; // JAE just got rid of compiler warning.
}
- (BOOL)scanRestOfString:(NSMutableString **)o
View
18 Tests/NSDataTest.m
@@ -16,15 +16,15 @@ @interface NSDataTest : YAJLTestCase
@implementation NSDataTest
- (void)testExample {
- NSData *data = [[self loadString:@"example"] dataUsingEncoding:NSUTF8StringEncoding];
- id JSON = [data yajl_JSON];
- GHTestLog([JSON description]);
-
- GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
- NSDictionary *glossary = [JSON objectForKey:@"glossary"];
- GHAssertNotNil(glossary, nil);
- NSString *title = [glossary objectForKey:@"title"];
- GHAssertEqualStrings(title, @"example glossary", nil);
+ NSData *data = [[self loadString:@"example"] dataUsingEncoding:NSUTF8StringEncoding];
+ id JSON = [data yajl_JSON];
+ GHTestLog([JSON description]);
+
+ GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
+ NSDictionary *glossary = [JSON objectForKey:@"glossary"];
+ GHAssertNotNil(glossary, nil);
+ NSString *title = [glossary objectForKey:@"title"];
+ GHAssertEqualStrings(title, @"example glossary", nil);
}
@end
View
48 Tests/NSObjectTest.m
@@ -19,35 +19,35 @@ @interface CustomJSONObject : NSObject
@implementation NSObjectTest
- (void)testDictionary {
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
- [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil], @"key2",
- nil];
-
- NSString *JSONString = [dict yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "];
-
- NSString *expected = [self loadString:@"gen_expected1"];
- GHTestLog(JSONString);
- GHAssertEqualStrings(JSONString, expected, nil);
+ NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
+ [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil], @"key2",
+ nil];
+
+ NSString *JSONString = [dict yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "];
+
+ NSString *expected = [self loadString:@"gen_expected1"];
+ GHTestLog(JSONString);
+ GHAssertEqualStrings(JSONString, expected, nil);
}
- (void)testArray {
- NSArray *array = [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
- [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil];
-
- NSString *JSONString = [array yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "];
-
- NSString *expected = [self loadString:@"object_expected_array"];
- GHTestLog(JSONString);
- GHAssertEqualStrings(JSONString, expected, nil);
+ NSArray *array = [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
+ [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil];
+
+ NSString *JSONString = [array yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "];
+
+ NSString *expected = [self loadString:@"object_expected_array"];
+ GHTestLog(JSONString);
+ GHAssertEqualStrings(JSONString, expected, nil);
}
- (void)testCustom {
- CustomJSONObject *obj = [[[CustomJSONObject alloc] init] autorelease];
- NSString *JSONString = [obj yajl_JSONString];
- NSString *expected = @"[\"Test\"]";
- GHTestLog(JSONString);
- GHAssertEqualStrings(JSONString, expected, nil);
+ CustomJSONObject *obj = [[[CustomJSONObject alloc] init] autorelease];
+ NSString *JSONString = [obj yajl_JSONString];
+ NSString *expected = @"[\"Test\"]";
+ GHTestLog(JSONString);
+ GHAssertEqualStrings(JSONString, expected, nil);
}
- (void)testComments {
@@ -80,7 +80,7 @@ - (void)testPrecision {
@implementation CustomJSONObject
- (id)JSON {
- return [NSArray arrayWithObject:@"Test"];
+ return [NSArray arrayWithObject:@"Test"];
}
@end
View
20 Tests/NSStringTest.m
@@ -16,16 +16,16 @@ @interface NSStringTest : YAJLTestCase
@implementation NSStringTest
- (void)testExample {
- NSString *exampleString = [self loadString:@"example"];
- GHTestLog(@"Example string: %@", exampleString);
- id JSON = [exampleString yajl_JSON];
- GHTestLog([JSON description]);
-
- GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
- NSDictionary *glossary = [JSON objectForKey:@"glossary"];
- GHAssertNotNil(glossary, nil);
- NSString *title = [glossary objectForKey:@"title"];
- GHAssertEqualStrings(title, @"example glossary", nil);
+ NSString *exampleString = [self loadString:@"example"];
+ GHTestLog(@"Example string: %@", exampleString);
+ id JSON = [exampleString yajl_JSON];
+ GHTestLog([JSON description]);
+
+ GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
+ NSDictionary *glossary = [JSON objectForKey:@"glossary"];
+ GHAssertNotNil(glossary, nil);
+ NSString *title = [glossary objectForKey:@"title"];
+ GHAssertEqualStrings(title, @"example glossary", nil);
}
@end
View
60 Tests/PerfTest.m
@@ -16,39 +16,39 @@ @implementation PerfTest
#define kPerfTestCount 200
- (void)_testYAJLParser {
- NSString *examplePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"];
- NSData *data = [[NSData dataWithContentsOfFile:examplePath options:NSUncachedRead error:nil] retain];
-
- NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
- for(NSInteger i = 0; i < kPerfTestCount; i++) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsNone];
- if (![parser parse:data]) GHFail(@"Failed to parse: %@", parser.parserError);
- [parser release];
- [pool release];
- }
- NSTimeInterval took = [NSDate timeIntervalSinceReferenceDate] - start;
- GHTestLog(@"Took %0.4f sec", took);
- [data release];
+ NSString *examplePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"];
+ NSData *data = [[NSData dataWithContentsOfFile:examplePath options:NSUncachedRead error:nil] retain];
+
+ NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
+ for(NSInteger i = 0; i < kPerfTestCount; i++) {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsNone];
+ if (![parser parse:data]) GHFail(@"Failed to parse: %@", parser.parserError);
+ [parser release];
+ [pool release];
+ }
+ NSTimeInterval took = [NSDate timeIntervalSinceReferenceDate] - start;
+ GHTestLog(@"Took %0.4f sec", took);
+ [data release];
}
- (void)_testSBJSON {
- NSString *examplePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"];
- NSData *testData = [NSData dataWithContentsOfFile:examplePath options:NSUncachedRead error:nil];
- NSString *testString = [[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding];
-
- NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
- for(NSInteger i = 0; i < kPerfTestCount; i++) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- id value = [testString JSONValue];
- if (!value) GHFail(@"No result");
- [pool release];
- }
- NSTimeInterval took = [NSDate timeIntervalSinceReferenceDate] - start;
- GHTestLog(@"Took %0.4f", took);
-
- [testString release];
-
+ NSString *examplePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"json"];
+ NSData *testData = [NSData dataWithContentsOfFile:examplePath options:NSUncachedRead error:nil];
+ NSString *testString = [[NSString alloc] initWithData:testData encoding:NSUTF8StringEncoding];
+
+ NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
+ for(NSInteger i = 0; i < kPerfTestCount; i++) {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+ id value = [testString JSONValue];
+ if (!value) GHFail(@"No result");
+ [pool release];
+ }
+ NSTimeInterval took = [NSDate timeIntervalSinceReferenceDate] - start;
+ GHTestLog(@"Took %0.4f", took);
+
+ [testString release];
+
}
@end
View
10 Tests/TwitterTest.m
@@ -17,12 +17,12 @@ @implementation TwitterTest
- (void)testCoordinates {
NSString *twitterJSONString = [self loadString:@"twitter"];
- id JSON = [twitterJSONString yajl_JSON];
- GHTestLog([JSON description]);
+ id JSON = [twitterJSONString yajl_JSON];
+ GHTestLog([JSON description]);
- GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
- NSDictionary *geo = [JSON objectForKey:@"geo"];
- GHAssertNotNil(geo, nil);
+ GHAssertTrue([JSON isKindOfClass:[NSDictionary class]], nil);
+ NSDictionary *geo = [JSON objectForKey:@"geo"];
+ GHAssertNotNil(geo, nil);
NSArray *coordinates = [geo objectForKey:@"coordinates"];
NSNumber *coord1 = [coordinates objectAtIndex:0];
NSNumber *coord2 = [coordinates objectAtIndex:1];
View
94 Tests/YAJLDocumentTest.m
@@ -19,81 +19,81 @@ - (YAJLDocument *)_loadDocument:(NSString *)sampleName parserOptions:(YAJLParser
return document;
}
-- (void)test {
- NSError *error = nil;
+- (void)test {
+ NSError *error = nil;
YAJLDocument *document = [self _loadDocument:@"example" parserOptions:0 error:&error];
- if (error) GHFail(@"Error: %@", error);
- GHTestLog(@"Root: %@", document.root);
+ if (error) GHFail(@"Error: %@", error);
+ GHTestLog(@"Root: %@", document.root);
}
- (void)testStreaming {
- YAJLDocument *document = [[YAJLDocument alloc] init];
+ YAJLDocument *document = [[YAJLDocument alloc] init];
- NSError *error = nil;
- NSData *data1 = [self loadData:@"stream_array1"];
- YAJLParserStatus status1 = [document parse:data1 error:&error];
- GHAssertTrue(status1 == YAJLParserStatusInsufficientData, nil);
- if (error) GHFail(@"Error: %@", error);
- GHTestLog(@"First part: %@", document.root);
-
- NSData *data2 = [self loadData:@"stream_array2"];
- YAJLParserStatus status2 = [document parse:data2 error:&error];
- GHAssertTrue(status2 == YAJLParserStatusOK, nil);
- if (error) GHFail(@"Error: %@", error);
+ NSError *error = nil;
+ NSData *data1 = [self loadData:@"stream_array1"];
+ YAJLParserStatus status1 = [document parse:data1 error:&error];
+ GHAssertTrue(status1 == YAJLParserStatusInsufficientData, nil);
+ if (error) GHFail(@"Error: %@", error);
+ GHTestLog(@"First part: %@", document.root);
+
+ NSData *data2 = [self loadData:@"stream_array2"];
+ YAJLParserStatus status2 = [document parse:data2 error:&error];
+ GHAssertTrue(status2 == YAJLParserStatusOK, nil);
+ if (error) GHFail(@"Error: %@", error);
- GHTestLog(@"Root: %@", document.root);
- [document release];
+ GHTestLog(@"Root: %@", document.root);
+ [document release];
}
- (void)testDoubleOverflow {
NSError *error = nil;
YAJLDocument *document = [self _loadDocument:@"overflow" parserOptions:0 error:&error];
GHAssertNotNil(document, nil);
GHTestLog(@"Root: %@", document.root);
- YAJLParserStatus status = document.parserStatus;
- GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
-
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
+ YAJLParserStatus status = document.parserStatus;
+ GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
+
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
GHAssertEquals([error code], (NSInteger)YAJLParserErrorCodeDoubleOverflow, nil);
GHAssertEqualStrings([[error userInfo] objectForKey:YAJLParserValueKey], @"1.79769e+309", nil);
- } else {
- GHFail(@"Should have error");
- }
+ } else {
+ GHFail(@"Should have error");
+ }
}
- (void)testOverflow2 {
NSError *error = nil;
YAJLDocument *document = [self _loadDocument:@"overflow2" parserOptions:0 error:&error];
GHTestLog(@"Root: %@", document.root);
- YAJLParserStatus status = document.parserStatus;
- GHAssertEquals(status, (NSUInteger)YAJLParserStatusOK, @"Should have OK status");
+ YAJLParserStatus status = document.parserStatus;
+ GHAssertEquals(status, (NSUInteger)YAJLParserStatusOK, @"Should have OK status");
GHAssertEqualObjects([document.root objectForKey:@"key1"], [NSNumber numberWithDouble:12343434343434343434343434344343434.0], nil);
GHAssertEqualObjects([document.root objectForKey:@"key2"], [NSNumber numberWithDouble:343434343434344343434343434.0], nil);
}
// This sample.json is too insane; Will need to revisit
- (void)_testEqualToSBJSON {
-
- NSData *data = [self loadData:@"sample"];
- GHTestLog(@"Sample data size: %d", [data length]);
-
- NSError *error = nil;
+
+ NSData *data = [self loadData:@"sample"];
+ GHTestLog(@"Sample data size: %d", [data length]);
+
+ NSError *error = nil;
- // Build from YAJL
- YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:0 error:&error];
- if (error) GHFail(@"Error: %@", error);
- id YAJLRoot = [document.root retain];
- // Save
- [NSKeyedArchiver archiveRootObject:YAJLRoot toFile:[self directoryWithPath:@"yajl_sample.plist"]];
- [document release];
-
- // Build from SBJSON
- NSString *testString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- id SBJSONRoot = [[testString JSONValue] retain];
- // Save
- [NSKeyedArchiver archiveRootObject:SBJSONRoot toFile:[self directoryWithPath:@"sbjson_sample.plist"]];
- [testString release];
+ // Build from YAJL
+ YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:0 error:&error];
+ if (error) GHFail(@"Error: %@", error);
+ id YAJLRoot = [document.root retain];
+ // Save
+ [NSKeyedArchiver archiveRootObject:YAJLRoot toFile:[self directoryWithPath:@"yajl_sample.plist"]];
+ [document release];
+
+ // Build from SBJSON
+ NSString *testString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
+ id SBJSONRoot = [[testString JSONValue] retain];
+ // Save
+ [NSKeyedArchiver archiveRootObject:SBJSONRoot toFile:[self directoryWithPath:@"sbjson_sample.plist"]];
+ [testString release];
}
View
136 Tests/YAJLGenTest.m
@@ -16,84 +16,84 @@ @interface YAJLGenTest : YAJLTestCase {}
@implementation YAJLGenTest
- (void)testGen1 {
- YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsBeautify indentString:@" "];
- [gen startDictionary];
- [gen string:@"key2"];
- [gen startArray];
- [gen string:@"arrayValue1"];
- [gen bool:YES];
- [gen bool:NO];
- [gen null];
- [gen number:[NSNumber numberWithInteger:1]];
- [gen number:[NSNumber numberWithDouble:234234.234234]];
- [gen endArray];
- [gen endDictionary];
- NSString *buffer = [gen buffer];
- [gen release];
-
- NSString *expected = [self loadString:@"gen_expected1"];
-
- GHTestLog(buffer);
- GHAssertEqualStrings(buffer, expected, nil);
+ YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsBeautify indentString:@" "];
+ [gen startDictionary];
+ [gen string:@"key2"];
+ [gen startArray];
+ [gen string:@"arrayValue1"];
+ [gen bool:YES];
+ [gen bool:NO];
+ [gen null];
+ [gen number:[NSNumber numberWithInteger:1]];
+ [gen number:[NSNumber numberWithDouble:234234.234234]];
+ [gen endArray];
+ [gen endDictionary];
+ NSString *buffer = [gen buffer];
+ [gen release];
+
+ NSString *expected = [self loadString:@"gen_expected1"];
+
+ GHTestLog(buffer);
+ GHAssertEqualStrings(buffer, expected, nil);
}
- (void)testGen2 {
- YAJLGen *gen = [[YAJLGen alloc] init];
- [gen startDictionary];
- [gen string:@"key2"];
- [gen startArray];
- [gen string:@"arrayValue1"];
- [gen bool:YES];
- [gen bool:NO];
- [gen null];
- [gen number:[NSNumber numberWithInteger:1]];
- [gen number:[NSNumber numberWithDouble:234234.234234]];
- [gen endArray];
- [gen endDictionary];
- NSString *buffer = [gen buffer];
- [gen clear];
- [gen release];
-
- NSString *expected = [self loadString:@"gen_expected2"];
-
- GHTestLog(buffer);
- GHAssertEqualStrings(buffer, expected, nil);
+ YAJLGen *gen = [[YAJLGen alloc] init];
+ [gen startDictionary];
+ [gen string:@"key2"];
+ [gen startArray];
+ [gen string:@"arrayValue1"];
+ [gen bool:YES];
+ [gen bool:NO];
+ [gen null];
+ [gen number:[NSNumber numberWithInteger:1]];
+ [gen number:[NSNumber numberWithDouble:234234.234234]];
+ [gen endArray];
+ [gen endDictionary];
+ NSString *buffer = [gen buffer];
+ [gen clear];
+ [gen release];
+
+ NSString *expected = [self loadString:@"gen_expected2"];
+
+ GHTestLog(buffer);
+ GHAssertEqualStrings(buffer, expected, nil);
}
- (void)testGenObject1 {
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
- [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil], @"key2",
- nil];
-
- YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsBeautify indentString:@" "];
- [gen object:dict];
- NSString *buffer = [gen buffer];
- [gen release];
-
- NSString *expected = [self loadString:@"gen_expected1"];
- GHTestLog(buffer);
- GHAssertEqualStrings(buffer, expected, nil);
+ NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
+ [NSArray arrayWithObjects:@"arrayValue1", [NSNumber numberWithBool:YES], [NSNumber numberWithBool:NO], [NSNull null],
+ [NSNumber numberWithInteger:1], [NSNumber numberWithDouble:234234.234234], nil], @"key2",
+ nil];
+
+ YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsBeautify indentString:@" "];
+ [gen object:dict];
+ NSString *buffer = [gen buffer];
+ [gen release];
+
+ NSString *expected = [self loadString:@"gen_expected1"];
+ GHTestLog(buffer);
+ GHAssertEqualStrings(buffer, expected, nil);
}
- (void)testGenObjectUnknownType {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate dateWithTimeIntervalSince1970:1], @"date", nil];
YAJLGen *gen = [[YAJLGen alloc] init];
- GHAssertThrows([gen object:dict], nil);
- [gen release];
+ GHAssertThrows([gen object:dict], nil);
+ [gen release];
}
- (void)testGenObjectIgnoreUnknownType {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSDate dateWithTimeIntervalSince1970:1], @"date", nil];
YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsIgnoreUnknownTypes indentString:@""];
- [gen object:dict];
- NSString *buffer = [gen buffer];
- [gen release];
-
- NSString *expected = [self loadString:@"gen_expected_ignore_unknown1"];
- GHTestLog(buffer);
- GHAssertEqualStrings(buffer, expected, nil);
+ [gen object:dict];
+ NSString *buffer = [gen buffer];
+ [gen release];
+
+ NSString *expected = [self loadString:@"gen_expected_ignore_unknown1"];
+ GHTestLog(buffer);
+ GHAssertEqualStrings(buffer, expected, nil);
}
- (void)testGenObjectPListTypes {
@@ -105,13 +105,13 @@ - (void)testGenObjectPListTypes {
nil];
YAJLGen *gen = [[YAJLGen alloc] initWithGenOptions:YAJLGenOptionsIncludeUnsupportedTypes indentString:@""];
- [gen object:array];
- NSString *buffer = [gen buffer];
- [gen release];
-
- NSString *expected = [self loadString:@"gen_expected_plist1"];
- GHTestLog(buffer);
- GHAssertEqualStrings(buffer, expected, nil);
+ [gen object:array];
+ NSString *buffer = [gen buffer];
+ [gen release];
+
+ NSString *expected = [self loadString:@"gen_expected_plist1"];
+ GHTestLog(buffer);
+ GHAssertEqualStrings(buffer, expected, nil);
}
@end
View
196 Tests/YAJLParserTest.m
@@ -12,140 +12,140 @@ @interface YAJLParserTest : YAJLTestCase {}
@implementation YAJLParserTest
-- (void)test {
- YAJLParser *parser = [[YAJLParser alloc] init];
- [parser parse:[self loadData:@"example"]];
-
- NSError *error = [parser parserError];
- if (error) {
- NSLog(@"Parse error:\n%@", error);
- GHFail(@"Error: %@", error);
- }
-
- [parser release];
+- (void)test {
+ YAJLParser *parser = [[YAJLParser alloc] init];
+ [parser parse:[self loadData:@"example"]];
+
+ NSError *error = [parser parserError];
+ if (error) {
+ NSLog(@"Parse error:\n%@", error);
+ GHFail(@"Error: %@", error);
+ }
+
+ [parser release];
}
- (void)testError {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- YAJLParserStatus status = [parser parse:[self loadData:@"error"]];
- GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
- } else {
- GHFail(@"Should have error");
- }
-
- [parser release];
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
+ YAJLParserStatus status = [parser parse:[self loadData:@"error"]];
+ GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
+ } else {
+ GHFail(@"Should have error");
+ }
+
+ [parser release];
}
- (void)testError2 {
YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- YAJLParserStatus status = [parser parse:[self loadData:@"error2"]];
- GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
+ YAJLParserStatus status = [parser parse:[self loadData:@"error2"]];
+ GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
GHAssertTrue([[error localizedDescription] hasPrefix:@"parse error: invalid object key (must be a string)\n"], nil);
- } else {
- GHFail(@"Should have error");
- }
-
- [parser release];
+ } else {
+ GHFail(@"Should have error");
+ }
+
+ [parser release];
}
- (void)testComments {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsAllowComments];
- YAJLParserStatus status = [parser parse:[self loadData:@"comments"]];
- GHAssertTrue(status == YAJLParserStatusOK, @"Should have OK status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
- GHFail(@"Error: %@", error);
- }
-
- [parser release];
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsAllowComments];
+ YAJLParserStatus status = [parser parse:[self loadData:@"comments"]];
+ GHAssertTrue(status == YAJLParserStatusOK, @"Should have OK status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
+ GHFail(@"Error: %@", error);
+ }
+
+ [parser release];
}
- (void)testFailOnComments {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- YAJLParserStatus status = [parser parse:[self loadData:@"comments"]];
- GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
- } else {
- GHFail(@"Should have error");
- }
-
- [parser release];
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
+ YAJLParserStatus status = [parser parse:[self loadData:@"comments"]];
+ GHAssertTrue(status == YAJLParserStatusError, @"Should have error status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
+ } else {
+ GHFail(@"Should have error");
+ }
+
+ [parser release];
}
- (void)testDoubleOverflow {
- YAJLParser *parser = [[YAJLParser alloc] init];
- YAJLParserStatus status = [parser parse:[self loadData:@"overflow"]];
- GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
+ YAJLParser *parser = [[YAJLParser alloc] init];
+ YAJLParserStatus status = [parser parse:[self loadData:@"overflow"]];
+ GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
GHAssertEquals([error code], (NSInteger)YAJLParserErrorCodeDoubleOverflow, nil);
GHAssertEqualStrings([[error userInfo] objectForKey:YAJLParserValueKey], @"1.79769e+309", nil);
- } else {
- GHFail(@"Should have error");
- }
-
- [parser release];
+ } else {
+ GHFail(@"Should have error");
+ }
+
+ [parser release];
}
- (void)testLongLongOverflow {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsStrictPrecision];
- YAJLParserStatus status = [parser parse:[self loadData:@"overflow_longlong"]];
- GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
-
- NSError *error = [parser parserError];
- if (error) {
- GHTestLog(@"Parse error:\n%@", error);
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsStrictPrecision];
+ YAJLParserStatus status = [parser parse:[self loadData:@"overflow_longlong"]];
+ GHAssertEquals(status, (NSUInteger)YAJLParserStatusError, @"Should have error status");
+
+ NSError *error = [parser parserError];
+ if (error) {
+ GHTestLog(@"Parse error:\n%@", error);
GHAssertEquals([error code], (NSInteger)YAJLParserErrorCodeIntegerOverflow, nil);
GHAssertEqualStrings([[error userInfo] objectForKey:YAJLParserValueKey], @"9223372036854775807", nil);
- } else {
- GHFail(@"Should have error");
- }
-
- [parser release];
+ } else {
+ GHFail(@"Should have error");
+ }
+
+ [parser release];
}
- (void)testOverflow2 {
YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- YAJLParserStatus status = [parser parse:[self loadData:@"overflow2"]];
- GHAssertTrue(status == YAJLParserStatusOK, @"Should have ok status");
+ YAJLParserStatus status = [parser parse:[self loadData:@"overflow2"]];
+ GHAssertTrue(status == YAJLParserStatusOK, @"Should have ok status");
[parser release];
}
- (void)testStreaming {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- YAJLParserStatus status;
- status = [parser parse:[self loadData:@"stream1"]];
- GHAssertTrue(status == YAJLParserStatusInsufficientData, @"Should have insufficient data");
-
- status = [parser parse:[self loadData:@"stream2"]];
- GHAssertTrue(status == YAJLParserStatusOK, @"Should have finished");
- [parser release];
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
+ YAJLParserStatus status;
+ status = [parser parse:[self loadData:@"stream1"]];
+ GHAssertTrue(status == YAJLParserStatusInsufficientData, @"Should have insufficient data");
+
+ status = [parser parse:[self loadData:@"stream2"]];
+ GHAssertTrue(status == YAJLParserStatusOK, @"Should have finished");
+ [parser release];
}
// TODO(gabe): Should error if you try to re-use
- (void)_testParseMoreThanOnce {
- YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
- parser.delegate = self;
- YAJLParserStatus status;
- status = [parser parse:[self loadData:@"example"]];
- GHAssertTrue(status == YAJLParserStatusOK, @"Should be OK");
- status = [parser parse:[self loadData:@"example"]];
- GHAssertTrue(status == YAJLParserStatusError, @"Should error");
+ YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:0];
+ parser.delegate = self;
+ YAJLParserStatus status;
+ status = [parser parse:[self loadData:@"example"]];
+ GHAssertTrue(status == YAJLParserStatusOK, @"Should be OK");
+ status = [parser parse:[self loadData:@"example"]];
+ GHAssertTrue(status == YAJLParserStatusError, @"Should error");
}
@end
View
14 Tests/YAJLTestCase.m
@@ -12,20 +12,20 @@
@implementation YAJLTestCase
- (NSData *)loadData:(NSString *)name {
- NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
+ NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
GHAssertNotNil(path, @"Invalid name for load data");
- return [NSData dataWithContentsOfFile:path options:NSUncachedRead error:nil];
+ return [NSData dataWithContentsOfFile:path options:NSUncachedRead error:nil];
}
- (NSString *)loadString:(NSString *)name {
- return [[[NSString alloc] initWithData:[self loadData:name] encoding:NSUTF8StringEncoding] autorelease];
+ return [[[NSString alloc] initWithData:[self loadData:name] encoding:NSUTF8StringEncoding] autorelease];
}
- (NSString *)directoryWithPath:(NSString *)path {
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:path];
- GHTestLog(@"Using path: %@", fullPath);
- return fullPath;
+ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
+ NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:path];
+ GHTestLog(@"Using path: %@", fullPath);
+ return fullPath;
}

0 comments on commit 78fb38f

Please sign in to comment.