Skip to content

Commit

Permalink
64bit integers fix/support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirek Rusin authored and Mirek Rusin committed Feb 18, 2011
1 parent fb6fc15 commit 1a60d22
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
37 changes: 26 additions & 11 deletions CoreJSON/CoreJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,29 @@ inline int __JSONParserAppendBooleanWithInteger(void *context, int value) {
return 1;
}

//inline int __JSONParserAppendNumberWithBytes(void *context, const char *value, unsigned int length) {
// CFStringRef string = CFStringCreateWithBytes(NULL, (const UInt8 *)s, l, NSUTF8StringEncoding, 1);
//
// CFNumberFormatterRef formatter = ...
//
// CFRelease(string);
// return 1;
//}
inline int __JSONParserAppendNumberWithBytes(void *context, const char *value, unsigned int length) {
CoreJSONRef json = (CoreJSONRef)context;
CFNumberRef number = NULL;

// TODO: anybody how to do it properly?
bool looksLikeFloat = 0;
for (int i = 0; i < length; i++)
if (value[i] == '.' || value[i] == 'e' || value[i] == 'E')
looksLikeFloat = 1;

if (looksLikeFloat) {
double value_ = strtod((char *)value, NULL);
number = CFNumberCreate(json->allocator, kCFNumberDoubleType, &value_);
} else {
long long value_ = strtoll((char *)value, NULL, 0);
number = CFNumberCreate(json->allocator, kCFNumberLongLongType, &value_);
}

CFArrayAppendValue(json->elements, number);
__JSONStackAppendValueAtTop(json->stack, number);
CFRelease(number);
return 1;
}

inline int __JSONParserAppendNumberWithLong(void *context, long value) {
CoreJSONRef json = (CoreJSONRef)context;
Expand Down Expand Up @@ -300,9 +315,9 @@ inline CoreJSONRef JSONCreate(CFAllocatorRef allocator) {
json->yajlParserCallbacks.yajl_boolean = __JSONParserAppendBooleanWithInteger;

// Set number or integer and double. Never all 3.
json->yajlParserCallbacks.yajl_number = NULL;
json->yajlParserCallbacks.yajl_integer = __JSONParserAppendNumberWithLong;
json->yajlParserCallbacks.yajl_double = __JSONParserAppendNumberWithDouble;
json->yajlParserCallbacks.yajl_number = __JSONParserAppendNumberWithBytes;
json->yajlParserCallbacks.yajl_integer = NULL; // __JSONParserAppendNumberWithLong
json->yajlParserCallbacks.yajl_double = NULL; // __JSONParserAppendNumberWithDouble

json->yajlParserCallbacks.yajl_start_map = __JSONParserAppendMapStart;
json->yajlParserCallbacks.yajl_map_key = __JSONParserAppendMapKeyWithBytes;
Expand Down
1 change: 1 addition & 0 deletions CoreJSON/CoreJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ bool __JSONStackAppendKeyAtTop (__JSONStackRef stack, CFTypeRef
int __JSONParserAppendStringWithBytes (void *context, const unsigned char *value, unsigned int length);
int __JSONParserAppendNull (void *context);
int __JSONParserAppendBooleanWithInteger (void *context, int value);
int __JSONParserAppendNumberWithBytes (void *context, const char *value, unsigned int length);
int __JSONParserAppendNumberWithLong (void *context, long value);
int __JSONParserAppendNumberWithDouble (void *context, double value);
int __JSONParserAppendMapKeyWithBytes (void *context, const unsigned char *value, unsigned int length);
Expand Down
23 changes: 23 additions & 0 deletions CoreJSONTests/CoreJSONTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ - (void) testSimpleStuff {
}
}


- (void) testUTF8Strings {
{
CoreJSONRef json = JSONCreateWithString(NULL, (CFStringRef)@"[\"a’la\"]");
Expand All @@ -47,4 +48,26 @@ - (void) testUTF8Strings {
}
}

- (void) testLargeNumbers {
{
CoreJSONRef json = JSONCreateWithString(NULL, (CFStringRef)@"[4294967297, 9223372036854775807, -9223372036854775807]");
NSArray *array = (NSArray *)JSONGetObject(json);
STAssertTrue([array count] == 3, @"Array should have 3 elements");
STAssertEquals([[array objectAtIndex: 0] longLongValue], 4294967297, @"Large number 2^32+1 expected");
STAssertEquals([[array objectAtIndex: 1] longLongValue], 9223372036854775807, @"Large number 2^(64-1) expected");
STAssertEquals([[array objectAtIndex: 2] longLongValue], -9223372036854775807, @"Large number 2^(64-1) expected");
}
}

- (void) testFloats {
{
CoreJSONRef json = JSONCreateWithString(NULL, (CFStringRef)@"[3.14159265, 1.61803399, -57.2957795]");
NSArray *array = (NSArray *)JSONGetObject(json);
STAssertTrue([array count] == 3, @"Array should have 3 elements");
STAssertEquals([[array objectAtIndex: 0] doubleValue], 3.14159265, @"Large number 2^32+1 expected");
STAssertEquals([[array objectAtIndex: 1] doubleValue], 1.61803399, @"Large number 2^(64-1) expected");
STAssertEquals([[array objectAtIndex: 2] doubleValue], -57.2957795, @"Large number 2^(64-1) expected");
}
}

@end

0 comments on commit 1a60d22

Please sign in to comment.