Public Member Functions

NSObject(YAJL) Class Reference

Generate JSON string from NSArray, NSDictionary or custom object or parse JSON from NSString or custom object. More...

#import <NSObject+YAJL.h>

List of all members.

Public Member Functions

(NSString *) - yajl_JSONString
 Create JSON string from object.
(NSString *) - yajl_JSONStringWithOptions:indentString:
 Create JSON string from object.
(id) - yajl_JSON
 Parse JSON (NSString or NSData or dataUsingEncoding:).
(id) - yajl_JSON:
 Parse JSON (NSString or NSData or dataUsingEncoding:) with out error.
(id) - yajl_JSONWithOptions:error:
 Parse JSON (NSString or NSData or dataUsingEncoding:) with options and out error.

Detailed Description

Generate JSON string from NSArray, NSDictionary or custom object or parse JSON from NSString or custom object.

Parse JSON:

 NSData *JSONData = [NSData dataWithContentsOfFile:@"example.json"];
 NSArray *arrayFromData = [JSONData yajl_JSON];
 
 NSString *JSONString = @"[\"Test\"]";
 NSArray *arrayFromString = [JSONString yajl_JSON];
 
 // With options and out error
 NSError *error = nil;
 NSArray *arrayFromString = [JSONString yajl_JSONWithOptions:YAJLParserOptionsAllowComments error:&error];

Generate JSON:

 NSDictionary *dict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
 NSString *JSONString = [dict yajl_JSONString];
 
 // Beautified with custon indent string
 NSArray *array = [NSArray arrayWithObjects:@"value1", @"value2", nil];
 NSString *JSONString = [dict yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@"    "];

Member Function Documentation

- (id) yajl_JSON  

Parse JSON (NSString or NSData or dataUsingEncoding:).

Returns:
JSON object
Exceptions:
YAJLParserException If a parse error occured
YAJLParsingUnsupportedException If not NSData or doesn't respond to dataUsingEncoding:
 NSString *JSONString = @"{'foo':['bar', true]}";
 id JSONValue = [JSONString yajl_JSON];
 
 NSData *JSONData = ...;
 id JSONValue = [JSONData yajl_JSON];
- (id) yajl_JSON: (NSError **)  error  

Parse JSON (NSString or NSData or dataUsingEncoding:) with out error.

If an error occurs, the returned object will be the current state of the object when the error occurred.

Parameters:
error Error to set if we failed to parse
Returns:
JSON object
Exceptions:
YAJLParserException If a parse error occured
YAJLParsingUnsupportedException If not NSData or doesn't respond to dataUsingEncoding:
 NSString *JSONString = @"{'foo':['bar', true]}";
 NSError *error = nil;
 [JSONString yajl_JSON:error];
 if (error) ...;
- (NSString *) yajl_JSONString  

Create JSON string from object.

Supported objects include: NSArray, NSDictionary, NSNumber, NSString, NSNull To override JSON value to encode (or support custom objects), implement (id)JSON; See YAJLCoding in YAJLGen.h

Exceptions:
YAJLGenInvalidObjectException If object is invalid
Returns:
JSON String
- (NSString *) yajl_JSONStringWithOptions: (YAJLGenOptions)  options
indentString: (NSString *)  indentString 

Create JSON string from object.

Supported objects include: NSArray, NSDictionary, NSNumber, NSString, NSNull To override JSON value to encode (or support custom objects), implement (id)JSON; See YAJLCoding in YAJLGen.h

Exceptions:
YAJLGenInvalidObjectException If object is invalid
Parameters:
options 
  • YAJLGenOptionsNone: No options
  • YAJLGenOptionsBeautify: Beautifiy JSON output
  • YAJLGenOptionsIgnoreUnknownTypes: Ignore unknown types (will use null value)
  • YAJLGenOptionsIncludeUnsupportedTypes: Handle non-JSON types (including NSDate, NSData, NSURL)
indentString 
Returns:
JSON String
- (id) yajl_JSONWithOptions: (YAJLParserOptions)  options
error: (NSError **)  error 

Parse JSON (NSString or NSData or dataUsingEncoding:) with options and out error.

If an error occurs, the returned object will be the current state of the object when the error occurred.

Parameters:
options Parse options

  • YAJLParserOptionsNone: No options
  • YAJLParserOptionsAllowComments: Javascript style comments will be allowed in the input (both / / and //)
  • YAJLParserOptionsCheckUTF8: Invalid UTF8 strings will cause a parse error
  • YAJLParserOptionsStrictPrecision: If YES will force strict precision and return integer overflow error
error Error to set if we failed to parse
Returns:
JSON object
Exceptions:
YAJLParserException If a parse error occured
YAJLParsingUnsupportedException If not NSData or doesn't respond to dataUsingEncoding:
 NSString *JSONString = @"{'foo':['bar', true]} // comment";
 NSError *error = nil;
 [JSONString yajl_JSONWithOptions:YAJLParserOptionsAllowComments error:error];
 if (error) ...;

The documentation for this class was generated from the following files:
 All Classes Functions Properties