CoreJSON is iOS, OSX (will also work on Linux) Core Foundation based fast parser and generator based on libyajl
C library.
Comparison with other JSON frameworks:
Tests performed with https://github.com/samsoffes/json-benchmarks
Parsing in Objective-C:
NSError *error = nil;
id object = (id)JSONCreateWithString(NULL, (CFStringRef)@"[foo, bar]", kJSONReadOptionsDefault, (CFErrorRef *)&error);
if (object) {
// Do something with object (NSArray)
[object release];
}
Parsing in C:
CFErrorRef error = NULL;
CFTypeRef object = JSONCreateWithString(NULL, CFSTR("[foo, bar]"), kJSONReadOptionsDefault, &error);
if (object) {
// Do something with object
CFRelease(object);
}
Generating in Objective-C:
NSArray *array = [NSArray arrayWithObjects: @"foo", @"bar", nil];
NSError *error = nil;
NSString *json = (id)JSONCreateString(NULL, array, kJSONWriteOptionsDefault, (CFErrorRef *)&error);
if (json) {
// Do something with json string
[json release];
}
[array release];
Generating in C:
CFTypeRef values[] = { CFSTR("foo"), CFSTR("bar") };
CFArrayRef array = CFArrayCreateMutable(NULL, values, 2, &kCFTypeArrayCallBacks);
CFErrorRef error = NULL;
CFStringRef json = JSONCreateString(NULL, array, kJSONWriteOptionsDefault, &error);
if (json) {
// Do something with json string
CFRelease(json);
}
CFRelease(array);
You should also take care of error
object
JSONReadOptions
:
kJSONReadOptionCheckUTF8 = 1
-- Check UTF8 stringskJSONReadOptionAllowComments = 2
-- Allow/* comments */
kJSONReadOptionsDefault = 0
-- Default options (don't check UTF8 strings and do not allow comments)kJSONReadOptionsCheckUTF8AndAllowComments = 3
-- Check UTF8 strings and allow comments
JSONWriteOptions
:
kJSONWriteOptionIndent = 1
-- Indent generated JSON stringkJSONWriteOptionsDefault = 0
-- Default options (do not indent JSON string)
There are just 2 files CoreJSON.h
and CoreJSON.c
you'll need together with libyajl
.
For your own (non Mac AppStore) OSX projects the quick way is to:
brew install yajl
- add
/usr/local/lib
toLibrary Search Path
and/usr/local/include
toHeader Search Path
- Just drop
CoreJSON.h
andCoreJSON.c
to your project and have fun
For OSX and iOS (Mac AppStore/AppStore) projects you need to include libyajl
and drop CoreJSON.h
and CoreJSON.c
files to your project.
One way to do it:
- Go to your project's directory (for which you're using
git
, right? ;) andgit submodule add git:https://github.com/mirek/CoreJSON.git CoreJSON
- From Xcode add
CoreJSON.h
andCoreJSON.c
files to your project - If you're already using
libyajl
in your project, you are good to go. If not, addlibyajl
files