Skip to content
Browse files

Updating docs

  • Loading branch information...
1 parent 2435db0 commit 54204611ea4bc1681ae42a15d043e341f1d35e9e @gabriel committed
Showing with 63 additions and 20 deletions.
  1. +37 −19 Classes/YAJL.h
  2. +1 −1 Makefile
  3. +25 −0 Tests/NSStringTest.m
View
56 Classes/YAJL.h
@@ -60,53 +60,69 @@
#import <YAJL/YAJL.h>
@endcode
- To parse JSON from an NSData (or NSString):
+ @section Examples Examples
+
+ @subsection Example1 To parse JSON from an NSData
@code
NSData *JSONData = [NSData dataWithContentsOfFile:@"example.json"];
NSArray *arrayFromData = [JSONData yajl_JSON];
+ @endcode
+
+ @subsection Example2 To parse JSON from an NSString
- NSString *JSONString = @"[\"Test\"]";
+ @code
+ NSString *JSONString = @"[1, 2, 3]";
NSArray *arrayFromString = [JSONString yajl_JSON];
+ @endcode
+
+ @subsection Example2 To parse JSON from an NSString (with out error and comments enabled)
+ @code
// With options and out error
+ NSString *JSONString = @"[1, 2, 3] // Allow comments";
NSError *error = nil;
NSArray *arrayFromString = [JSONString yajl_JSONWithOptions:YAJLParserOptionsAllowComments error:&error];
@endcode
- To generate JSON from an object:
+ @subsection Example3 To generate JSON from an object
@code
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
NSString *JSONString = [dict yajl_JSONString];
+ @endcode
+
+ @subsection Example4 To generate JSON from an object (beautified with custom indent):
+ @code
// Beautified with custon indent string
NSArray *array = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSString *JSONString = [dict yajl_JSONStringWithOptions:YAJLGenOptionsBeautify indentString:@" "];
+ @endcode
- To use the streaming (or SAX style) parser, use YAJLParser. For higher level (document) streaming, see below.
+ @subsection Example5 To use the streaming (or SAX style) parser, use YAJLParser
+ @code
NSData *data = [NSData dataWithContentsOfFile:@"example.json"];
YAJLParser *parser = [[YAJLParser alloc] initWithParserOptions:YAJLParserOptionsAllowComments];
parser.delegate = self;
[parser parse:data];
- if (parser.parserError) {
- NSLog(@"Error:\n%@", parser.parserError);
- }
+ if (parser.parserError)
+ NSLog(@"Error:\n%@", parser.parserError);
parser.delegate = nil;
[parser release];
// Include delegate methods from YAJLParserDelegate
- - (void)parserDidStartDictionary:(YAJLParser *)parser;
- - (void)parserDidEndDictionary:(YAJLParser *)parser;
+ - (void)parserDidStartDictionary:(YAJLParser *)parser { }
+ - (void)parserDidEndDictionary:(YAJLParser *)parser { }
- - (void)parserDidStartArray:(YAJLParser *)parser;
- - (void)parserDidEndArray:(YAJLParser *)parser;
+ - (void)parserDidStartArray:(YAJLParser *)parser { }
+ - (void)parserDidEndArray:(YAJLParser *)parser { }
- - (void)parser:(YAJLParser *)parser didMapKey:(NSString *)key;
- - (void)parser:(YAJLParser *)parser didAdd:(id)value;
+ - (void)parser:(YAJLParser *)parser didMapKey:(NSString *)key { }
+ - (void)parser:(YAJLParser *)parser didAdd:(id)value { }
@endcode
@subsection ParserOptions Parser Options
@@ -117,7 +133,7 @@
- YAJLParserOptionsCheckUTF8: Will verify UTF-8
- YAJLParserOptionsStrictPrecision: Will force strict precision and return integer overflow error, if number is greater than long long.
- @subsection StreamingExample Streaming Example (Parser)
+ @subsection Example6 Parsing as data becomes available
@code
YAJLParser *parser = [[[YAJLParser alloc] init] autorelease];
@@ -126,15 +142,17 @@
// A chunk of data comes...
YAJLParserStatus status = [parser parse:chunk1];
// 'status' should be YAJLParserStatusInsufficientData, if its not finished
- if (parser.parserError) ...;
+ if (parser.parserError)
+ NSLog(@"Error:\n%@", parser.parserError);
// Another chunk of data comes...
YAJLParserStatus status = [parser parse:chunk2];
// 'status' should be YAJLParserStatusOK if its finished
- if (parser.parserError) ...;
+ if (parser.parserError)
+ NSLog(@"Error:\n%@", parser.parserError);
@encode
- @subsection UsageDocument Usage (Document-style)
+ @subsection Example6 Document style parsing
To use the document style, use YAJLDocument. Usage should be very similar to NSXMLDocument.
@@ -147,7 +165,7 @@
[document release];
@endcode
- @subsection StreamingExampleDocument Streaming Example (Document)
+ @subsection Example7 Document style parsing as data becomes available
@code
YAJLDocument *document = [[YAJLDocument alloc] init];
@@ -169,7 +187,7 @@
- (void)document:(YAJLDocument *)document didSetObject:(id)object forKey:(id)key inDictionary:(NSDictionary *)dict { }
@endcode
- @section LoadJSONBundle Load JSON from Bundle
+ @subsection Example8 Load JSON from Bundle
@code
id JSONValue = [[NSBundle mainBundle] yajl_JSONFromResource:@"kegs.json"];
View
2 Makefile
@@ -6,7 +6,7 @@ docs:
cd ~/Library/Developer/Shared/Documentation/DocSets/ && tar zcvpf YAJL.docset.tgz YAJL.docset
mv ~/Library/Developer/Shared/Documentation/DocSets/YAJL.docset.tgz Documentation
-docs-gh: docs
+gh-pages: docs
git checkout gh-pages
cp -R Documentation/html/* .
rm -rf Documentation
View
25 Tests/NSStringTest.m
@@ -28,4 +28,29 @@ - (void)testExample {
GHAssertEqualStrings(title, @"example glossary", nil);
}
+- (void)testArrayNumbers {
+ NSString *JSONString = @"[1, 2, 3]";
+ NSArray *JSON = [JSONString yajl_JSON];
+ GHTestLog([JSON description]);
+ NSArray *expected = [NSArray arrayWithObjects:
+ [NSNumber numberWithInteger:1],
+ [NSNumber numberWithInteger:2],
+ [NSNumber numberWithInteger:3],
+ nil];
+ GHAssertEqualObjects(expected, JSON, nil);
+}
+
+- (void)testAllowComments {
+ NSString *JSONString = @"[1, 2, 3] // Allow comments";
+ NSError *error = nil;
+ NSArray *JSON = [JSONString yajl_JSONWithOptions:YAJLParserOptionsAllowComments error:&error];
+ GHTestLog([JSON description]);
+ NSArray *expected = [NSArray arrayWithObjects:
+ [NSNumber numberWithInteger:1],
+ [NSNumber numberWithInteger:2],
+ [NSNumber numberWithInteger:3],
+ nil];
+ GHAssertEqualObjects(expected, JSON, nil);
+}
+
@end

0 comments on commit 5420461

Please sign in to comment.
Something went wrong with that request. Please try again.