Permalink
Browse files

Initial commit.

  • Loading branch information...
Tim Burks
Tim Burks committed Nov 30, 2008
0 parents commit 88c9160a54142b3872d1508691078dcb3b5032f9
Showing with 2,589 additions and 0 deletions.
  1. +19 −0 CREDITS
  2. +41 −0 Nukefile
  3. +11 −0 README
  4. +76 −0 extra/bench.m
  5. +1 −0 extra/make
  6. +189 −0 objc/JSON.h
  7. +802 −0 objc/JSON.m
  8. +10 −0 test/README
  9. +14 −0 test/format/HumanReadable.json
  10. +1 −0 test/format/input.json
  11. +22 −0 test/json.org/1.json
  12. +44 −0 test/json.org/1.plist
  13. +11 −0 test/json.org/2.json
  14. +37 −0 test/json.org/2.plist
  15. +26 −0 test/json.org/3.json
  16. +54 −0 test/json.org/3.plist
  17. +88 −0 test/json.org/4.json
  18. +185 −0 test/json.org/4.plist
  19. +27 −0 test/json.org/5.json
  20. +1 −0 test/jsonchecker/fail1.json
  21. +1 −0 test/jsonchecker/fail10.json
  22. +1 −0 test/jsonchecker/fail11.json
  23. +1 −0 test/jsonchecker/fail12.json
  24. +1 −0 test/jsonchecker/fail13.json
  25. +1 −0 test/jsonchecker/fail14.json
  26. +1 −0 test/jsonchecker/fail15.json
  27. +1 −0 test/jsonchecker/fail16.json
  28. +1 −0 test/jsonchecker/fail17.json
  29. +1 −0 test/jsonchecker/fail18.json
  30. +1 −0 test/jsonchecker/fail19.json
  31. +1 −0 test/jsonchecker/fail2.json
  32. +1 −0 test/jsonchecker/fail20.json
  33. +1 −0 test/jsonchecker/fail21.json
  34. +1 −0 test/jsonchecker/fail22.json
  35. +1 −0 test/jsonchecker/fail23.json
  36. +1 −0 test/jsonchecker/fail24.json
  37. +1 −0 test/jsonchecker/fail25.json
  38. +1 −0 test/jsonchecker/fail26.json
  39. +2 −0 test/jsonchecker/fail27.json
  40. +2 −0 test/jsonchecker/fail28.json
  41. +1 −0 test/jsonchecker/fail29.json
  42. +1 −0 test/jsonchecker/fail3.json
  43. +1 −0 test/jsonchecker/fail30.json
  44. +1 −0 test/jsonchecker/fail31.json
  45. +1 −0 test/jsonchecker/fail32.json
  46. +1 −0 test/jsonchecker/fail33.json
  47. +1 −0 test/jsonchecker/fail4.json
  48. +1 −0 test/jsonchecker/fail5.json
  49. +1 −0 test/jsonchecker/fail6.json
  50. +1 −0 test/jsonchecker/fail7.json
  51. +1 −0 test/jsonchecker/fail8.json
  52. +1 −0 test/jsonchecker/fail9.json
  53. +58 −0 test/jsonchecker/pass1.json
  54. +1 −0 test/jsonchecker/pass2.json
  55. +6 −0 test/jsonchecker/pass3.json
  56. +16 −0 test/original/Errors.h
  57. +269 −0 test/original/Errors.m
  58. +12 −0 test/original/Pretty.h
  59. +42 −0 test/original/Pretty.m
  60. +12 −0 test/original/Types.h
  61. +146 −0 test/original/Types.m
  62. +13 −0 test/rfc4627/a.json
  63. +31 −0 test/rfc4627/a.plist
  64. +22 −0 test/rfc4627/b.json
  65. +42 −0 test/rfc4627/b.plist
  66. +61 −0 test/test_examples.nu
  67. +32 −0 test/types/array.plist
  68. +53 −0 test/types/number.plist
  69. +35 −0 test/types/object.plist
  70. +45 −0 test/types/string.plist
19 CREDITS
@@ -0,0 +1,19 @@
+Blake Seely
+ A lot of the inspiration to early versions of this framework
+ came from Blake's BSJSONAdditions project.
+
+Marc Lehmann
+ Part of the inspiration for my JSON work has been from Marc's
+ JSON::XS Perl module. I also adopted the way he organised the
+ tests. Having a separate fixture for testing error conditions
+ makes a lot of sense.
+
+Jens Alfke <jens@mooseyard.com> - http://mooseyard.com/Jens
+ Jens emailed me out of the blue one day with a couple of patches
+ that gave a speedup of 11x for generation and 5x for parsing of
+ the long (12k) JSON string I've been using for testing.
+
+Greg Bolsinga
+ Provided patches for dropping the dependency on AppKit and thus
+ truly making this a Foundation framework, and for building a
+ static library suitable for use with the iPhone.
@@ -0,0 +1,41 @@
+;;
+;; Nukefile for JSON
+;;
+;; Commands:
+;; nuke - builds TouchJSON as a framework
+;; nuke test - runs the unit tests in the NuTests directory
+;; nuke install - installs TouchJSON in /Library/Frameworks
+;; nuke clean - removes build artifacts
+;; nuke clobber - removes build artifacts and TouchJSON.framework
+;;
+;; The "nuke" build tool is installed with Nu (http://programming.nu)
+;;
+
+;; the @variables below are instance variables of a NukeProject.
+;; for details, see tools/nuke in the Nu source distribution.
+
+;; source files
+(set @m_files (filelist "^objc/.*.m$"))
+
+;; framework description
+(set @framework "JSON")
+(set @framework_identifier "nu.programming.json")
+(set @framework_creator_code "????")
+
+(set @cflags "-g -std=gnu99 -I Source")
+(set @ldflags "-framework Foundation")
+
+(compilation-tasks)
+(framework-tasks)
+
+(task "clobber" => "clean" is
+ (SH "rm -rf #{@framework_dir}")) ;; @framework_dir is defined by the nuke framework-tasks macro
+
+(task "default" => "framework")
+
+(task "install" => "framework" is
+ (SH "sudo rm -rf /Library/Frameworks/#{@framework}.framework")
+ (SH "ditto #{@framework}.framework /Library/Frameworks/#{@framework}.framework"))
+
+(task "test" => "framework" is
+ (SH "nutest test/test_*.nu"))
11 README
@@ -0,0 +1,11 @@
+
+This is a Nu-ish repackaging of Stig Brautaset's json-framework.
+
+--- Quoting Stig: ---
+JSON is a light-weight data interchange format. It's easy to read and
+write for humans and computers alike. This framework implements a strict
+JSON parser and generator in Objective-C.
+
+All resources related to this framework can be found at the project
+site: http://code.google.com/p/json-framework/
+---------------------
@@ -0,0 +1,76 @@
+/*
+Copyright (C) 25/04/2008 Stig Brautaset. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ Neither the name of the author nor the names of its contributors may be used
+ to endorse or promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#import <stdio.h>
+#import "JSON.h"
+
+#define COUNT 500
+
+int main(int argc, char **argv) {
+ NSAutoreleasePool *outer = [NSAutoreleasePool new];
+
+ if (argc != 2) {
+ printf("Usage: %s file-containing-json\n", argv[0]);
+ return 1;
+ }
+
+ SBJSON *json = [SBJSON new];
+ NSString *filename = [NSString stringWithCString:argv[1]];
+ NSString *repr = [NSString stringWithContentsOfFile:filename];
+
+ NSAutoreleasePool *inner = [NSAutoreleasePool new];
+ NSDate *start = [NSDate date];
+ for (int i = 0; i < COUNT; i++) {
+ [json objectWithString:repr error:NULL];
+ [json objectWithString:repr error:NULL];
+ [json objectWithString:repr error:NULL];
+ [json objectWithString:repr error:NULL];
+ [json objectWithString:repr error:NULL];
+ }
+ double duration = -[start timeIntervalSinceNow];
+ printf("Decode: %f\n", 5 * COUNT / duration);
+ [inner release];
+
+ id object = [repr JSONValue];
+ inner = [NSAutoreleasePool new];
+ start = [NSDate date];
+ for (int i = 0; i < COUNT; i++) {
+ [json stringWithObject:object error:NULL];
+ [json stringWithObject:object error:NULL];
+ [json stringWithObject:object error:NULL];
+ [json stringWithObject:object error:NULL];
+ [json stringWithObject:object error:NULL];
+ }
+ duration = -[start timeIntervalSinceNow];
+ printf("Encode: %f\n", 5 * COUNT / duration);
+ [inner release];
+
+ [outer release];
+ return 0;
+}
@@ -0,0 +1 @@
+gcc bench.m -I objc -framework JSON -std=gnu99 -framework Foundation
@@ -0,0 +1,189 @@
+/*
+Copyright (c) 2007, Stig Brautaset. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+and/or other materials provided with the distribution.
+
+Neither the name of the author nor the names of its contributors may be used
+to endorse or promote products derived from this software without specific
+prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/**
+@mainpage A strict JSON parser and generator for Objective-C
+
+JSON (JavaScript Object Notation) is a lightweight data-interchange
+format. This framework provides two apis for parsing and generating
+JSON. One standard object-based and a higher level api consisting of
+categories added to existing Objective-C classes.
+
+Learn more on the http://code.google.com/p/json-framework project site.
+*/
+
+#import <Foundation/Foundation.h>
+
+extern NSString * SBJSONErrorDomain;
+
+enum
+{
+ EUNSUPPORTED = 1,
+ EPARSENUM,
+ EPARSE,
+ EFRAGMENT,
+ ECTRL,
+ EUNICODE,
+ EDEPTH,
+ EESCAPE,
+ ETRAILCOMMA,
+ ETRAILGARBAGE,
+ EEOF,
+ EINPUT
+};
+
+/**
+@brief A strict JSON parser and generator
+
+This is the parser and generator underlying the categories added to
+NSString and various other objects.
+
+Objective-C types are mapped to JSON types and back in the following way:
+
+@li NSNull -> Null -> NSNull
+@li NSString -> String -> NSMutableString
+@li NSArray -> Array -> NSMutableArray
+@li NSDictionary -> Object -> NSMutableDictionary
+@li NSNumber (-initWithBool:) -> Boolean -> NSNumber -initWithBool:
+@li NSNumber -> Number -> NSDecimalNumber
+
+In JSON the keys of an object must be strings. NSDictionary keys need
+not be, but attempting to convert an NSDictionary with non-string keys
+into JSON will throw an exception.
+
+NSNumber instances created with the +numberWithBool: method are
+converted into the JSON boolean "true" and "false" values, and vice
+versa. Any other NSNumber instances are converted to a JSON number the
+way you would expect. JSON numbers turn into NSDecimalNumber instances,
+as we can thus avoid any loss of precision.
+
+Strictly speaking correctly formed JSON text must have <strong>exactly
+one top-level container</strong>. (Either an Array or an Object.) Scalars,
+i.e. nulls, numbers, booleans and strings, are not valid JSON on their own.
+It can be quite convenient to pretend that such fragments are valid
+JSON however, and this class lets you do so.
+
+This class does its best to be as strict as possible, both in what it
+accepts and what it generates. (Other than the above mentioned support
+for JSON fragments.) For example, it does not support trailing commas
+in arrays or objects. Nor does it support embedded comments, or
+anything else not in the JSON specification.
+
+*/
+@interface SBJSON : NSObject
+{
+ BOOL humanReadable;
+ BOOL sortKeys;
+ NSUInteger maxDepth;
+
+ @private
+ // Used temporarily during scanning/generation
+ NSUInteger depth;
+ const char *c;
+}
+
+/// Whether we are generating human-readable (multiline) JSON
+/**
+ Set whether or not to generate human-readable JSON. The default is NO, which produces
+ JSON without any whitespace. (Except inside strings.) If set to YES, generates human-readable
+ JSON with linebreaks after each array value and dictionary key/value pair, indented two
+ spaces per nesting level.
+ */
+@property BOOL humanReadable;
+
+/// Whether or not to sort the dictionary keys in the output
+/** The default is to not sort the keys. */
+@property BOOL sortKeys;
+
+/// The maximum depth the parser will go to
+/** Defaults to 512. */
+@property NSUInteger maxDepth;
+
+/// Return JSON representation of an array or dictionary
+- (NSString*)stringWithObject:(id)value error:(NSError**)error;
+
+/// Return JSON representation of any legal JSON value
+- (NSString*)stringWithFragment:(id)value error:(NSError**)error;
+
+/// Return the object represented by the given string
+- (id)objectWithString:(NSString*)jsonrep error:(NSError**)error;
+
+/// Return the fragment represented by the given string
+- (id)fragmentWithString:(NSString*)jsonrep error:(NSError**)error;
+
+/// Return JSON representation (or fragment) for the given object
+- (NSString*)stringWithObject:(id)value
+allowScalar:(BOOL)x
+error:(NSError**)error;
+
+/// Parse the string and return the represented object (or scalar)
+- (id)objectWithString:(id)value
+allowScalar:(BOOL)x
+error:(NSError**)error;
+
+@end
+
+/// Adds JSON generation to NSObject subclasses
+@interface NSObject (NSObject_SBJSON)
+
+/**
+ @brief Returns a string containing the receiver encoded as a JSON fragment.
+
+ This method is added as a category on NSObject but is only actually
+ supported for the following objects:
+ @li NSDictionary
+ @li NSArray
+ @li NSString
+ @li NSNumber (also used for booleans)
+ @li NSNull
+ */
+- (NSString *)JSONFragment;
+
+/**
+ @brief Returns a string containing the receiver encoded in JSON.
+
+ This method is added as a category on NSObject but is only actually
+ supported for the following objects:
+ @li NSDictionary
+ @li NSArray
+ */
+- (NSString *)JSONRepresentation;
+
+@end
+
+/// Adds JSON parsing to NSString
+@interface NSString (NSString_SBJSON)
+
+/// Returns the object represented in the receiver, or nil on error.
+- (id)JSONFragmentValue;
+
+/// Returns the dictionary or array represented in the receiver, or nil on error.
+- (id)JSONValue;
+
+@end
Oops, something went wrong.

0 comments on commit 88c9160

Please sign in to comment.