Giter Site home page Giter Site logo

jsonkit's Introduction

JSONKit

JSONKit is dual licensed under either the terms of the BSD License, or alternatively under the terms of the Apache License, Version 2.0.
Copyright © 2011, John Engelhart.

A Very High Performance Objective-C JSON Library

UPDATE: (2011/12/18) The benchmarks below were performed before Apples NSJSONSerialization was available (as of Mac OS X 10.7 and iOS 5). The obvious question is: Which is faster, NSJSONSerialization or JSONKit? According to this site, JSONKit is faster than NSJSONSerialization. Some quick "back of the envelope" calculations using the numbers reported, JSONKit appears to be approximately 25% to 40% faster than NSJSONSerialization, which is pretty significant.

Parsing Serializing
Deserialize from JSON Serialize to JSON
23% Faster than Binary .plist ! 549% Faster than Binary .plist !
  • Benchmarking was performed on a MacBook Pro with a 2.66GHz Core 2.
  • All JSON libraries were compiled with gcc-4.2 -DNS_BLOCK_ASSERTIONS -O3 -arch x86_64.
  • Timing results are the average of 1,000 iterations of the user + system time reported by getrusage.
  • The JSON used was twitter_public_timeline.json from samsoffes / json-benchmarks.
  • Since the .plist format does not support serializing NSNull, the null values in the original JSON were changed to "null".
  • The experimental branch contains the gzip compression changes.
    • JSONKit automagically links to libz.dylib on the fly at run time– no manual linking required.
    • Parsing / deserializing will automagically decompress a buffer if it detects a gzip signature header.
    • You can compress / gzip the serialized JSON by passing JKSerializeOptionCompress to -JSONDataWithOptions:error:.

JSON versus PLIST, the Ultimate Showdown benchmarks the common JSON libraries and compares them to Apples .plist format.


JavaScript Object Notation, or JSON, is a lightweight, text-based, serialization format for structured data that is used by many web-based services and API's. It is defined by RFC 4627.

JSON provides the following primitive types:

  • null
  • Boolean true and false
  • Number
  • String
  • Array
  • Object (a.k.a. Associative Arrays, Key / Value Hash Tables, Maps, Dictionaries, etc.)

These primitive types are mapped to the following Objective-C Foundation classes:

JSON Objective-C
null NSNull
true and false NSNumber
Number NSNumber
String NSString
Array NSArray
Object NSDictionary

JSONKit uses Core Foundation internally, and it is assumed that Core Foundation ≡ Foundation for every equivalent base type, i.e. CFStringNSString.

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

JSON To Objective-C Primitive Mapping Details

  • The JSON specification is somewhat ambiguous about the details and requirements when it comes to Unicode, and it does not specify how Unicode issues and errors should be handled. Most of the ambiguity stems from the interpretation and scope RFC 4627 Section 3, Encoding: JSON text SHALL be encoded in Unicode. It is the authors opinion and interpretation that the language of RFC 4627 requires that a JSON implementation MUST follow the requirements specified in the Unicode Standard, and in particular the Conformance chapter of the Unicode Standard, which specifies requirements related to handling, interpreting, and manipulating Unicode text.

    The default behavior for JSONKit is strict RFC 4627 conformance. It is the authors opinion and interpretation that RFC 4627 requires JSON to be encoded in Unicode, and therefore JSON that is not legal Unicode as defined by the Unicode Standard is invalid JSON. Therefore, JSONKit will not accept JSON that contains ill-formed Unicode. The default, strict behavior implies that the JKParseOptionLooseUnicode option is not enabled.

    When the JKParseOptionLooseUnicode option is enabled, JSONKit follows the specifications and recommendations given in The Unicode 6.0 standard, Chapter 3 - Conformance, section 3.9 Unicode Encoding Forms. As a general rule of thumb, the Unicode code point U+FFFD is substituted for any ill-formed Unicode encountered. JSONKit attempts to follow the recommended Best Practice for Using U+FFFD: Replace each maximal subpart of an ill-formed subsequence by a single U+FFFD.

    The following Unicode code points are treated as ill-formed Unicode, and if JKParseOptionLooseUnicode is enabled, cause U+FFFD to be substituted in their place:

    U+0000.
    U+D800 thru U+DFFF, inclusive.
    U+FDD0 thru U+FDEF, inclusive.
    U+nFFFE and U+nFFFF, where n is from 0x0 to 0x10

    The code points U+FDD0 thru U+FDEF, U+nFFFE, and U+nFFFF (where n is from 0x0 to 0x10), are defined as Noncharacters by the Unicode standard and "should never be interchanged".

    An exception is made for the code point U+0000, which is legal Unicode. The reason for this is that this particular code point is used by C string handling code to specify the end of the string, and any such string handling code will incorrectly stop processing a string at the point where U+0000 occurs. Although reasonable people may have different opinions on this point, it is the authors considered opinion that the risks of permitting JSON Strings that contain U+0000 outweigh the benefits. One of the risks in allowing U+0000 to appear unaltered in a string is that it has the potential to create security problems by subtly altering the semantics of the string which can then be exploited by a malicious attacker. This is similar to the issue of arbitrarily deleting characters from Unicode text.

    RFC 4627 allows for these limitations under section 4, Parsers: An implementation may set limits on the length and character contents of strings. While the Unicode Standard permits the mutation of the original JSON (i.e., substituting U+FFFD for ill-formed Unicode), RFC 4627 is silent on this issue. It is the authors opinion and interpretation that RFC 4627, section 3 – Encoding, JSON text SHALL be encoded in Unicode. implies that such mutations are not only permitted but MUST be expected by any strictly conforming RFC 4627 JSON implementation. The author feels obligated to note that this opinion and interpretation may not be shared by others and, in fact, may be a minority opinion and interpretation. You should be aware that any mutation of the original JSON may subtly alter its semantics and, as a result, may have security related implications for anything that consumes the final result.

    It is important to note that JSONKit will not delete characters from the JSON being parsed as this is a requirement specified by the Unicode Standard. Additional information can be found in the Unicode Security FAQ and Unicode Technical Report #36 - Unicode Security Consideration, in particular the section on non-visual security.

  • The JSON specification does not specify the details or requirements for JSON String values, other than such strings must consist of Unicode code points, nor does it specify how errors should be handled. While JSONKit makes an effort (subject to the reasonable caveats above regarding Unicode) to preserve the parsed JSON String exactly, it can not guarantee that NSString will preserve the exact Unicode semantics of the original JSON String.

    JSONKit does not perform any form of Unicode Normalization on the parsed JSON Strings, but can not make any guarantees that the NSString class will not perform Unicode Normalization on the parsed JSON String used to instantiate the NSString object. The NSString class may place additional restrictions or otherwise transform the JSON String in such a way so that the JSON String is not bijective with the instantiated NSString object. In other words, JSONKit can not guarantee that when you round trip a JSON String to a NSString and then back to a JSON String that the two JSON Strings will be exactly the same, even though in practice they are. For clarity, "exactly" in this case means bit for bit identical. JSONKit can not even guarantee that the two JSON Strings will be Unicode equivalent, even though in practice they will be and would be the most likely cause for the two round tripped JSON Strings to no longer be bit for bit identical.

  • JSONKit maps true and false to the CFBoolean values kCFBooleanTrue and kCFBooleanFalse, respectively. Conceptually, CFBoolean values can be thought of, and treated as, NSNumber class objects. The benefit to using CFBoolean is that true and false JSON values can be round trip deserialized and serialized without conversion or promotion to a NSNumber with a value of 0 or 1.

  • The JSON specification does not specify the details or requirements for JSON Number values, nor does it specify how errors due to conversion should be handled. In general, JSONKit will not accept JSON that contains JSON Number values that it can not convert with out error or loss of precision.

    For non-floating-point numbers (i.e., JSON Number values that do not include a . or e|E), JSONKit uses a 64-bit C primitive type internally, regardless of whether the target architecture is 32-bit or 64-bit. For unsigned values (i.e., those that do not begin with a -), this allows for values up to 264-1 and up to -263 for negative values. As a special case, the JSON Number -0 is treated as a floating-point number since the underlying floating-point primitive type is capable of representing a negative zero, whereas the underlying twos-complement non-floating-point primitive type can not. JSON that contains Number values that exceed these limits will fail to parse and optionally return a NSError object. The functions strtoll() and strtoull() are used to perform the conversions.

    The C double primitive type, or IEEE 754 Double 64-bit floating-point, is used to represent floating-point JSON Number values. JSON that contains floating-point Number values that can not be represented as a double (i.e., due to over or underflow) will fail to parse and optionally return a NSError object. The function strtod() is used to perform the conversion. Note that the JSON standard does not allow for infinities or NaN (Not a Number). The conversion and manipulation of floating-point values is non-trivial. Unfortunately, RFC 4627 is silent on how such details should be handled. You should not depend on or expect that when a floating-point value is round tripped that it will have the same textual representation or even compare equal. This is true even when JSONKit is used as both the parser and creator of the JSON, let alone when transferring JSON between different systems and implementations.

  • For JSON Objects (or NSDictionary in JSONKit nomenclature), RFC 4627 says The names within an object SHOULD be unique (note: name is a key in JSONKit nomenclature). At this time the JSONKit behavior is undefined for JSON that contains names within an object that are not unique. However, JSONKit currently tries to follow a "the last key / value pair parsed is the one chosen" policy. This behavior is not finalized and should not be depended on.

    The previously covered limitations regarding JSON Strings have important consequences for JSON Objects since JSON Strings are used as the key. The JSON specification does not specify the details or requirements for JSON Strings used as keys in JSON Objects, specifically what it means for two keys to compare equal. Unfortunately, because RFC 4627 states JSON text SHALL be encoded in Unicode., this means that one must use the Unicode Standard to interpret the JSON, and the Unicode Standard allows for strings that are encoded with different Unicode Code Points to "compare equal". JSONKit uses NSString exclusively to manage the parsed JSON Strings. Because NSString uses Unicode as its basis, there exists the possibility that NSString may subtly and silently convert the Unicode Code Points contained in the original JSON String in to a Unicode equivalent string. Due to this, the JSONKit behavior for JSON Strings used as keys in JSON Objects that may be Unicode equivalent but not binary equivalent is undefined.

    See also:
        W3C - Requirements for String Identity Matching and String Indexing

Objective-C To JSON Primitive Mapping Details

  • When serializing, the top level container, and all of its children, are required to be strictly invariant during enumeration. This property is used to make certain optimizations, such as if a particular object has already been serialized, the result of the previous serialized UTF8 string can be reused (i.e., the UTF8 string of the previous serialization can simply be copied instead of performing all the serialization work again). While this is probably only of interest to those who are doing extraordinarily unusual things with the run-time or custom classes inheriting from the classes that JSONKit will serialize (i.e, a custom object whose value mutates each time it receives a message requesting its value for serialization), it also covers the case where any of the objects to be serialized are mutated during enumeration (i.e., mutated by another thread). The number of times JSONKit will request an objects value is non-deterministic, from a minimum of once up to the number of times it appears in the serialized JSON– therefore an object MUST NOT depend on receiving a message requesting its value each time it appears in the serialized output. The behavior is undefined if these requirements are violated.

  • The objects to be serialized MUST be acyclic. If the objects to be serialized contain circular references the behavior is undefined. For example,

    [arrayOne addObject:arrayTwo];
    [arrayTwo addObject:arrayOne];
    id json = [arrayOne JSONString];

    … will result in undefined behavior.

  • The contents of NSString objects are encoded as UTF8 and added to the serialized JSON. JSONKit assumes that NSString produces well-formed UTF8 Unicode and does no additional validation of the conversion. When JKSerializeOptionEscapeUnicode is enabled, JSONKit will encode Unicode code points that can be encoded as a single UTF16 code unit as \uXXXX, and will encode Unicode code points that require UTF16 surrogate pairs as \uhigh\ulow. While JSONKit makes every effort to serialize the contents of a NSString object exactly, modulo any RFC 4627 requirements, the NSString class uses the Unicode Standard as its basis for representing strings. You should be aware that the Unicode Standard defines string equivalence in a such a way that two strings that compare equal are not required to be bit for bit identical. Therefore there exists the possibility that NSString may mutate a string in such a way that it is Unicode equivalent, but not bit for bit identical with the original string.

  • The NSDictionary class allows for any object, which can be of any class, to be used as a key. JSON, however, only permits Strings to be used as keys. Therefore JSONKit will fail with an error if it encounters a NSDictionary that contains keys that are not NSString objects during serialization. More specifically, the keys must return YES when sent -isKindOfClass:[NSString class].

  • JSONKit will fail with an error if it encounters an object that is not a NSNull, NSNumber, NSString, NSArray, or NSDictionary class object during serialization. More specifically, JSONKit will fail with an error if it encounters an object where -isKindOfClass: returns NO for all of the previously mentioned classes.

  • JSON does not allow for Numbers that are ±Infinity or ±NaN. Therefore JSONKit will fail with an error if it encounters a NSNumber that contains such a value during serialization.

  • Objects created with [NSNumber numberWithBool:YES] and [NSNumber numberWithBool:NO] will be mapped to the JSON values of true and false, respectively. More specifically, an object must have pointer equality with kCFBooleanTrue or kCFBooleanFalse (i.e., if((id)object == (id)kCFBooleanTrue)) in order to be mapped to the JSON values true and false.

  • NSNumber objects that are not booleans (as defined above) are sent -objCType to determine what type of C primitive type they represent. Those that respond with a type from the set cislq are treated as long long, those that respond with a type from the set CISLQB are treated as unsigned long long, and those that respond with a type from the set fd are treated as double. NSNumber objects that respond with a type not in the set of types mentioned will cause JSONKit to fail with an error.

    More specifically, CFNumberGetValue(object, kCFNumberLongLongType, &longLong) is used to retrieve the value of both the signed and unsigned integer values, and the type reported by -objCType is used to determine whether the result is signed or unsigned. For floating-point values, CFNumberGetValue is used to retrieve the value using kCFNumberDoubleType for the type argument.

    Floating-point numbers are converted to their decimal representation using the printf format conversion specifier %.17g. Theoretically this allows up to a float, or IEEE 754 Single 32-bit floating-point, worth of precision to be represented. This means that for practical purposes, double values are converted to float values with the associated loss of precision. The RFC 4627 standard is silent on how floating-point numbers should be dealt with and the author has found that real world JSON implementations vary wildly in how they handle this issue. Furthermore, the %g format conversion specifier may convert floating-point values that can be exactly represented as an integer to a textual representation that does not include a . or e– essentially silently promoting a floating-point value to an integer value (i.e, 5.0 becomes 5). Because of these and many other issues surrounding the conversion and manipulation of floating-point values, you should not expect or depend on floating point values to maintain their full precision, or when round tripped, to compare equal.

Reporting Bugs

Please use the github.com JSONKit Issue Tracker to report bugs.

The author requests that you do not file a bug report with JSONKit regarding problems reported by the clang static analyzer unless you first manually verify that it is an actual, bona-fide problem with JSONKit and, if appropriate, is not "legal" C code as defined by the C99 language specification. If the clang static analyzer is reporting a problem with JSONKit that is not an actual, bona-fide problem and is perfectly legal code as defined by the C99 language specification, then the appropriate place to file a bug report or complaint is with the developers of the clang static analyzer.

Important Details

  • JSONKit is not designed to be used with the Mac OS X Garbage Collection. The behavior of JSONKit when compiled with -fobjc-gc is undefined. It is extremely unlikely that Mac OS X Garbage Collection will ever be supported.

  • JSONKit is not designed to be used with Objective-C Automatic Reference Counting (ARC). The behavior of JSONKit when compiled with -fobjc-arc is undefined. The behavior of JSONKit compiled without ARC mixed with code that has been compiled with ARC is normatively undefined since at this time no analysis has been done to understand if this configuration is safe to use. At this time, there are no plans to support ARC in JSONKit. Although tenative, it is extremely unlikely that ARC will ever be supported, for many of the same reasons that Mac OS X Garbage Collection is not supported.

  • The JSON to be parsed by JSONKit MUST be encoded as Unicode. In the unlikely event you end up with JSON that is not encoded as Unicode, you must first convert the JSON to Unicode, preferably as UTF8. One way to accomplish this is with the NSString methods -initWithBytes:length:encoding: and -initWithData:encoding:.

  • Internally, the low level parsing engine uses UTF8 exclusively. The JSONDecoder method -objectWithData: takes a NSData object as its argument and it is assumed that the raw bytes contained in the NSData is UTF8 encoded, otherwise the behavior is undefined.

  • It is not safe to use the same instantiated JSONDecoder object from multiple threads at the same time. If you wish to share a JSONDecoder between threads, you are responsible for adding mutex barriers to ensure that only one thread is decoding JSON using the shared JSONDecoder object at a time.

Tips for speed

  • Enable the NS_BLOCK_ASSERTIONS pre-processor flag. JSONKit makes heavy use of NSCParameterAssert() internally to ensure that various arguments, variables, and other state contains only legal and expected values. If an assertion check fails it causes a run time exception that will normally cause a program to terminate. These checks and assertions come with a price: they take time to execute and do not contribute to the work being performed. It is perfectly safe to enable NS_BLOCK_ASSERTIONS as JSONKit always performs checks that are required for correct operation. The checks performed with NSCParameterAssert() are completely optional and are meant to be used in "debug" builds where extra integrity checks are usually desired. While your mileage may vary, the author has found that adding -DNS_BLOCK_ASSERTIONS to an -O2 optimization setting can generally result in an approximate 7-12% increase in performance.

  • Since the very low level parsing engine works exclusively with UTF8 byte streams, anything that is not already encoded as UTF8 must first be converted to UTF8. While JSONKit provides additions to the NSString class which allows you to conveniently convert JSON contained in a NSString, this convenience does come with a price. JSONKit must allocate an autoreleased NSMutableData large enough to hold the strings UTF8 conversion and then convert the string to UTF8 before it can begin parsing. This takes both memory and time. Once the parsing has finished, JSONKit sets the autoreleased NSMutableData length to 0, which allows the NSMutableData to release the memory. This helps to minimize the amount of memory that is in use but unavailable until the autorelease pool pops. Therefore, if speed and memory usage are a priority, you should avoid using the NSString convenience methods if possible.

  • If you are receiving JSON data from a web server, and you are able to determine that the raw bytes returned by the web server is JSON encoded as UTF8, you should use the JSONDecoder method -objectWithUTF8String:length: which immediately begins parsing the pointers bytes. In practice, every JSONKit method that converts JSON to an Objective-C object eventually calls this method to perform the conversion.

  • If you are using one of the various ways provided by the NSURL family of classes to receive JSON results from a web server, which typically return the results in the form of a NSData object, and you are able to determine that the raw bytes contained in the NSData are encoded as UTF8, then you should use either the JSONDecoder method objectWithData: or the NSData method -objectFromJSONData. If are going to be converting a lot of JSON, the better choice is to instantiate a JSONDecoder object once and use the same instantiated object to perform all your conversions. This has two benefits:

    1. The NSData method -objectFromJSONData creates an autoreleased JSONDecoder object to perform the one time conversion. By instantiating a JSONDecoder object once and using the objectWithData: method repeatedly, you can avoid this overhead.
    2. The instantiated object cache from the previous JSON conversion is reused. This can result in both better performance and a reduction in memory usage if the JSON your are converting is very similar. A typical example might be if you are converting JSON at periodic intervals that consists of real time status updates.
  • On average, the JSONData… methods are nearly four times faster than the JSONString… methods when serializing a NSDictionary or NSArray to JSON. The difference in speed is due entirely to the instantiation overhead of NSString.

  • If at all possible, use NSData instead of NSString methods when processing JSON. This avoids the sometimes significant conversion overhead that NSString performs in order to provide an object oriented interface for its contents. For many uses, using NSString is not needed and results in wasted effort– for example, using JSONKit to serialize a NSDictionary or NSArray to a NSString. This NSString is then passed to a method that sends the JSON to a web server, and this invariably requires converting the NSString to NSData before it can be sent. In this case, serializing the collection object directly to NSData would avoid the unnecessary conversions to and from a NSString object.

Parsing Interface

JSONDecoder Interface

The objectWith… methods return immutable collection objects and their respective mutableObjectWith… methods return mutable collection objects.

Note: The bytes contained in a NSData object MUST be UTF8 encoded.

Important: Methods will raise NSInvalidArgumentException if parseOptionFlags is not valid.
Important: objectWithUTF8String: and mutableObjectWithUTF8String: will raise NSInvalidArgumentException if string is NULL.
Important: objectWithData: and mutableObjectWithData: will raise NSInvalidArgumentException if jsonData is NULL.

+ (id)decoder;
+ (id)decoderWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
- (id)initWithParseOptions:(JKParseOptionFlags)parseOptionFlags;

- (void)clearCache;

- (id)objectWithUTF8String:(const unsigned char *)string length:(size_t)length;
- (id)objectWithUTF8String:(const unsigned char *)string length:(size_t)length error:(NSError **)error;
- (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(size_t)length;
- (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(size_t)length error:(NSError **)error;

- (id)objectWithData:(NSData *)jsonData;
- (id)objectWithData:(NSData *)jsonData error:(NSError **)error;
- (id)mutableObjectWithData:(NSData *)jsonData;
- (id)mutableObjectWithData:(NSData *)jsonData error:(NSError **)error;

NSString Interface

- (id)objectFromJSONString;
- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
- (id)mutableObjectFromJSONString;
- (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
- (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;

NSData Interface

- (id)objectFromJSONData;
- (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
- (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
- (id)mutableObjectFromJSONData;
- (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
- (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;

JKParseOptionFlags

Parsing OptionDescription
JKParseOptionNoneThis is the default if no other other parse option flags are specified, and the option used when a convenience method does not provide an argument for explicitly specifying the parse options to use. Synonymous with JKParseOptionStrict.
JKParseOptionStrictThe JSON will be parsed in strict accordance with the RFC 4627 specification.
JKParseOptionCommentsAllow C style // and /* … */ comments in JSON. This is a fairly common extension to JSON, but JSON that contains C style comments is not strictly conforming JSON.
JKParseOptionUnicodeNewlinesAllow Unicode recommended (?:\r\n|[\n\v\f\r\x85\p{Zl}\p{Zp}]) newlines in JSON. The JSON specification only allows the newline characters \r and \n, but this option allows JSON that contains the Unicode recommended newline characters to be parsed. JSON that contains these additional newline characters is not strictly conforming JSON.
JKParseOptionLooseUnicodeNormally the decoder will stop with an error at any malformed Unicode. This option allows JSON with malformed Unicode to be parsed without reporting an error. Any malformed Unicode is replaced with \uFFFD, or REPLACEMENT CHARACTER, as specified in The Unicode 6.0 standard, Chapter 3, section 3.9 Unicode Encoding Forms.
JKParseOptionPermitTextAfterValidJSONNormally, non-white-space that follows the JSON is interpreted as a parsing failure. This option allows for any trailing non-white-space to be ignored and not cause a parsing error.

Serializing Interface

The serializing interface includes NSString convenience methods for those that need to serialize a single NSString. For those that need this functionality, the NSString additions are much more convenient than having to wrap a single NSString in a NSArray, which then requires stripping the unneeded [] characters from the serialized JSON result. When serializing a single NSString, you can control whether or not the serialized JSON result is surrounded by quotation marks using the includeQuotes: argument:

Example Result Argument
a "test"... "a \"test\"..." includeQuotes:YES
a "test"... a \"test\"... includeQuotes:NO

Note: The NSString methods that do not include a includeQuotes: argument behave as if invoked with includeQuotes:YES.
Note: The bytes contained in the returned NSData object are UTF8 encoded.

NSArray and NSDictionary Interface

- (NSData *)JSONData;
- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
- (NSString *)JSONString;
- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;

NSString Interface

- (NSData *)JSONData;
- (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
- (NSString *)JSONString;
- (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;

JKSerializeOptionFlags

Serializing OptionDescription
JKSerializeOptionNoneThis is the default if no other other serialize option flags are specified, and the option used when a convenience method does not provide an argument for explicitly specifying the serialize options to use.
JKSerializeOptionPrettyNormally the serialized JSON does not include any unnecessary white-space. While this form is the most compact, the lack of any white-space means that it's something only another JSON parser could love. Enabling this option causes JSONKit to add additional white-space that makes it easier for people to read. Other than the extra white-space, the serialized JSON is identical to the JSON that would have been produced had this option not been enabled.
JKSerializeOptionEscapeUnicodeWhen JSONKit encounters Unicode characters in NSString objects, the default behavior is to encode those Unicode characters as UTF8. This option causes JSONKit to encode those characters as \uXXXX. For example,
["w∈L⟺y(∣y∣≤∣w∣)"]
becomes:
["w\u2208L\u27fa\u2203y(\u2223y\u2223\u2264\u2223w\u2223)"]
JKSerializeOptionEscapeForwardSlashesAccording to the JSON specification, the / (U+002F) character may be backslash escaped (i.e., \/), but it is not required. The default behavior of JSONKit is to not backslash escape the / character. Unfortunately, it was found some real world implementations of the ASP.NET Date Format require the date to be strictly encoded as \/Date(...)\/, and the only way to achieve this is through the use of JKSerializeOptionEscapeForwardSlashes. See github issue #21 for more information.

jsonkit's People

Contributors

atnan avatar johnezang avatar jparise avatar soffes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonkit's Issues

Crash on objectForKey

I'm using JSONKit to parse GitHub news feeds, and it is currently crashing on this JSON:

{
    public: true
    actor: "mojombo"
    repository: {
        integrate_branch: "staging"
        master_branch: "master"
        open_issues: 313
        has_downloads: false
        description: "The missing package manager for OS X."
        private: false
        fork: false
        owner: "mxcl"
        url: "https://github.com/mxcl/homebrew"
        pushed_at: "2011/06/27 17:08:07 -0700"
        created_at: "2009/05/20 12:38:37 -0700"
        homepage: "http://mxcl.github.com/homebrew"
        size: 23248
        has_wiki: true
        forks: 2328
        language: "Ruby"
        has_issues: true
        name: "homebrew"
    watchers: 5023
    }
    actor_attributes: {
        location: "San Francisco"
        gravatar_id: "25c7c18223fb42a4c6ae1c8db6f50f9b"
        company: "GitHub, Inc."
        email: "[email protected]"
        type: "User"
        blog: "http://tom.preston-werner.com"
        name: "Tom Preston-Werner"
        login: "mojombo"
    }
    created_at: "2011/06/27 18:36:50 -0700"
    payload: {
    }
    type: "ForkEvent"
    url: "https://github.com/mojombo/homebrew"
}

In my code, I'm doing:

NSDictionary *payload = [attributes valueForKey:@"payload"];
NSDictionary *repo = [payload valueForKey:@"repo"];

Because the payload dictionary is empty, the subsequent call to get the repo dictionary throws a divide by zero exception from the modulus operation on line 1009 of JSONKit.m:

  NSUInteger        keyHash = CFHash(aKey), keyEntry = (keyHash % dictionary->capacity), idx = 0UL;

NSAutoreleaseNoPool

Hi,

I can't figure out if this error is on my side or on the JSONKit's side but I get NSAutoreleaseNoPool errors for a whole bunch of objects:

date - appName[305:7e07] *** __NSAutoreleaseNoPool(): Object 0x5c590d0 of class __NSDate autoreleased with no pool in place - just leaking
" " Object 0x5c58f70 of class NSCFString autoreleased with no pool in place - just leaking
" " Object 0x5c57640 of class __NSCFData autoreleased with no pool in place - just leaking
" " Object 0x5c2bc30 of class NSURL autoreleased with no pool in place - just leaking
" " Object 0x5c58520 of class __NSArrayM autoreleased with no pool in place - just leaking
" " Object 0x5c2cad0 of class NSRecursiveLock autoreleased with no pool in place - just leaking
" " Object 0x5838ba0 of class NSPathStore2 autoreleased with no pool in place - just leaking
" " Object 0x584fac0 of class __NSArrayI autoreleased with no pool in place - just leaking
" " Object 0x5b74120 of class JKSerializer autoreleased with no pool in place - just leaking
" " Object 0x5c2cad0 of class NSRecursiveLock autoreleased with no pool in place - just leaking
" " Object 0x5c53f90 of class __NSCFDictionary autoreleased with no pool in place - just leaking
" " Object 0x58501c0 of class NSConcreteMutableData autoreleased with no pool in place - just leaking
" " Object 0x5850f60 of class JSONDecoder autoreleased with no pool in place - just leaking
" " Object 0x5837ee0 of class JKDictionary autoreleased with no pool in place - just leaking
" " Object 0x5c5af80 of class JSONDecoder autoreleased with no pool in place - just leaking

I get about 100 of each type! Anybody else?

Thanks for the help

Garbace Collector Unsupported

I'm using JSONKit in a Cocoa application and I started using the gc and realized that JSONKit is not supporting gc.
Is there any workaround or a beta version to enable it?

thanks

Supporting Automatic Reference Counting (ARC)

I would like to discuss your future plans for supporting Automatic Reference Counting (ARC). I see in commit c2146ff that you added a section to the README in which you state:

JSONKit is not designed to be used with Objective-C Automatic Reference Counting (ARC). The behavior of JSONKit when compiled with -fobjc-arc is undefined. The behavior of JSONKit compiled without ARC mixed with code that has been compiled with ARC is normatively undefined since at this time no analysis has been done to understand if this configuration is safe to use. At this time, there are no plans to support ARC in JSONKit. Although tenative, it is extremely unlikely that ARC will ever be supported, for many of the same reasons that Mac OS X Garbage Collection is not supported.

Is this the long-term plan? What are your reasons for not supporting ARC?

Note: Edited to remove unsubstantiated claims about ARC, as well as a dumb suggestion for creating an ARC branch of the code. No one wants to maintain two branches for something like this.

How to access elements of json

With the following jsonstring, I have access to pilots:

{
"1": {
"name": "XXX",
"pilots": {
"pilot": {
"name": "YYYY",
"image": "",
"years": "28"
},
"copilot": {
"name": "ZZZZ",
"image": "",
"years": "24"
}
}
},
"2": {
"name": "XXX",
"pilots": {
"pilot": {
"name": "YYYY",
"image": "",
"years": "28"
},
"copilot": {
"name": "ZZZZ",
"image": "",
"years": "24"
}
}
}
}

with this code get elements 1 and 2 but How to access pilot elements?

JSONDecoder *jDecoder = [JSONDecoder decoder];
NSDictionary *dVehicles = [jDecoder objecttWithData:responseData error:&msgError];

Thanks.

Crash on dealloc

I got a EXC_BAD_ACCESS occasionally on line 958:

- (void)dealloc
{
  if(JK_EXPECT_T(entry != NULL)) {
    NSUInteger atEntry = 0UL;
    for(atEntry = 0UL; atEntry < capacity; atEntry++) {
// following line: 958
      if(JK_EXPECT_T(entry[atEntry].key    != NULL)) { CFRelease(entry[atEntry].key);    entry[atEntry].key    = NULL; }
      if(JK_EXPECT_T(entry[atEntry].object != NULL)) { CFRelease(entry[atEntry].object); entry[atEntry].object = NULL; }
    }

    free(entry); entry = NULL;
  }

  [super dealloc];
}

Error decoding JSonString

Hello,

I have this JSON string:

{"data":
{77: {"number": 0, "photo": ""},
{78: {"number": 3, "photo": ""},
...
};

My code is:

JSONDecoder *jsDecoder = [JSONDecoder decoder];
NSArray *arrayElements = [jsDecoder objectWithData:responseDara error:&msgError];

To decode gives the error "Error Domain = JKErrorDomain Code = 1 " Expected to "STRING" or a '}', not '77' "
What is wrong?

JSONDecoder parseJSONData returns string "<null>" for json's null value

Hi there,
I started using JSONKit library and I really enjoyed the speed of the parsing. its really fast as you guys say.
but I am having a problem when parsing a JSON with one of the values is null:
...
"somekey" : null

the dictionary in return will show up as

...
"somekey" = "";

is there a way to fix this locally for now ?

Why is Unicode NULL codepoint invalid?

Why is the Unicode NULL codepoint, when properly escaped, invalid? That is, a string like "Testing\u0000Hello world"

isValidCodePoint returns sourceIllegal for the NULL character (ch == 0U). The headers say:

The code in isValidCodePoint() is derived from the ICU code in
utf.h for the macros U_IS_UNICODE_NONCHAR and U_IS_UNICODE_CHAR.

However, U_IS_UNICODE_CHAR(0) returns true.

In addition, RFC 4627 makes a passing reference to U+0000 as being allowed:

Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence

I can of course use the LooseUnicode option to replace it, but that's not ideal since it is valid JSON as far as I can tell.

NSDate support

Hello,
Can you please add support for NSDate type. I would think you could just map NSDate/CFDate to a String.

Thanks!

Possible performance regression

Hi John,

Not really a big deal and I'm not sure if this is related to the recent JKSerializer class, but I'm finding that encodes are 10-20% slower than my last test (with commit 524b9d7).

Do you see a similar drop?

Regards,
Omar

Unsupported Classes

Instead of (or in addition to) using blocks to serialize unsupported class types, perhaps a protocol could be used. Classes would implement this protocol in order to be serialized naturally.

removeObjectsForKeys doesn't properly remove objects

Hi,

There is a problem with the NSMutableDictionary returned by mutableObjectFromJSONString. A call to removeObjectsForKeys will remove the objects but not the keys. So we end up with nil objects. I also noticed side effects on other dictionary values that should not be affected.

Should it be overridden in the JKDictionary class like removeObjectForKey is?

Thanks,
JF

Serialize NSMutableData

It may be a user error, but I am unable serialize a dictionary containing an MSMutableData object.

When attempting to do so, I get

Error Domain=JKErrorDomain Code=-1 "Unable to serialize object class NSConcreteMutableData." UserInfo=0x4c31b50 {NSLocalizedDescription=Unable to serialize object class NSConcreteMutableData.}

Incorrect objectForKey results for keys not in table.

Hi John,

There's a bug in _JKDictionaryHashTableEntryForKey which returns an incorrect result (returns an object when it should return NULL) if the key falls into the same bucket as another key, but the hash and value don't match.

The loop does:

atEntry = &dictionary->entry[(keyEntry + idx) % dictionary->capacity];

But never clears atEntry if we get to the end of the loop and the key wasn't found.

Thanks!

Regards,
Omar

v1.4 memory leaks

test environment XCode4

line 2318

- (id)objectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length error:(NSError **)error
{
  if(parseState == NULL) { [NSException raise:NSInternalInconsistencyException format:@"parseState is NULL."];          }
  if(string     == NULL) { [NSException raise:NSInvalidArgumentException       format:@"The string argument is NULL."]; }

  return(_JKParseUTF8String(parseState, NO, string, (size_t)length, error));
}

Why must the caller retain the new object for JKArray methods

I browsed through your code and wondered why exactly the caller is responsible to retain the object that is going to be inserted into the array via _JKArray methods.
You're using CFRelease and -autorelease to manage retain counts in those methods, but no CFRetain or -retain. I'm sure there's a reason for that, but I could not figure out what it is.

Please enlighten me :)

best,
Ullrich

is JKSerializeOptionCompress functional

I tried this and I got back regular ole NSData. so I'm guessing this is not working at this time, can you confirm. or am i missing something else?
Jeremy

Missing @throw or incorrect method name

As of 862fa4d, line 2793 reads like this:

if((encodeState = (struct JKEncodeState *)calloc(1UL, sizeof(JKEncodeState))) == NULL) { [NSException exceptionWithName:NSMallocException reason:@"Unable to allocate state structure." userInfo:NULL]; return(NULL); }

That, in case of a memory problem will create the exception object and not do anything with it. I think is missing a @throw statement, or creating the exception using a +[NSException raise:format:] method (like is done in the rest of the file).

[NSException raise:NSMallocException format:@"Unable to allocate state structure."]

can not deserialize single number

Hello, shouldn't JSONKit deserialize a single number ?

Here is the error I get when I provide the single digit 1 to JSONKit:

    NSData *data = [@"1" dataUsingEncoding:NSUTF8StringEncoding];
    id object = [data objectFromJSONDataWithParseOptions:JKParseOptionStrict error:&error];

Can't print the description of a NIL object.
(gdb) po error
Error Domain=JKErrorDomain Code=-1 "Invalid number." UserInfo=0x5b076a0 {JKAtIndexKey=1, JKLineNumberKey=1, NSLocalizedDescription=Invalid number.}

Crash requesting jsondata from NSMutableDictionary created by JSONKit

When requesting jsondata from an NSMutableDictionary created by jsonkit, jsonkit will crash. It works fine for non mutable dictionaries.

Process:         someApp [33571]
Path:            /Users/USER/Library/Developer/Xcode/DerivedData/someApp-dzxzpbtokpnlvbfzxrgbssffgdva/Build/Products/Debug/someApp.app/Contents/MacOS/someApp
Identifier:      com.myCompany.someApp
Version:         1.0 (1)
Code Type:       X86-64 (Native)
Parent Process:  launchd [152]

Date/Time:       2011-03-14 11:02:51.272 -0700
OS Version:      Mac OS X 10.7 (11A390)
Report Version:  8

Interval Since Last Report:          349830 sec
Crashes Since Last Report:           1
Per-App Interval Since Last Report:  47732 sec
Per-App Crashes Since Last Report:   1
Anonymous UUID:                      92135CCF-450D-4DAA-8FF5-E194218A69AF

Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000069dcbc3

VM Regions Near 0x69dcbc3:
-->
   __TEXT                 0000000100000000-0000000100026000 [  152K] r-x/rwx SM=COW  /Users/USER/Library/Developer/Xcode/DerivedData/someApp-dzxzpbtokpnlvbfzxrgbssffgdva/Build/Products/Debug/someApp.app/Contents/MacOS/someApp

Application Specific Information:
objc[33571]: garbage collection is OFF
Performing @selector(someSelector:) from sender NSButton 0x1018170c0

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   com.myCompany.someApp            0x000000010000bab3 jk_encode_add_atom_to_buffer + 1075 (JSONKit.m:2605)
1   com.myCompany.someApp            0x000000010000ea8f jk_encode_add_atom_to_buffer + 13327 (JSONKit.m:2795)
2   com.myCompany.someApp            0x000000010000e3e8 jk_encode_add_atom_to_buffer + 11624 (JSONKit.m:2765)
3   com.myCompany.someApp            0x000000010000afd5 jk_encode + 581 (JSONKit.m:2831)
4   com.myCompany.someApp            0x000000010000ad83 -[NSArray(JSONKit) JSONDataWithOptions:error:] + 67 (JSONKit.m:2891)
5   com.myCompany.someApp            0x000000010000ad38 -[NSArray(JSONKit) JSONData] + 56 (JSONKit.m:2886)
6   com.myCompany.someApp            0x000000010000260d -[someAppAppDelegate someMethod:] + 157 (someAppAppDelegate.m:118)
7   com.apple.CoreFoundation       0x00007fff8ad626ad -[NSObject performSelector:withObject:] + 61
8   com.apple.AppKit               0x00007fff8998ed00 -[NSApplication sendAction:to:from:] + 139
9   com.apple.AppKit               0x00007fff8998ec32 -[NSControl sendAction:to:] + 88
10  com.apple.AppKit               0x00007fff89a14a4e -[NSCell _sendActionFrom:] + 137
11  com.apple.AppKit               0x00007fff89a14355 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
12  com.apple.AppKit               0x00007fff89a4313a -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
13  com.apple.AppKit               0x00007fff89a12e7b -[NSControl mouseDown:] + 786
14  com.apple.AppKit               0x00007fff899393fc -[NSWindow sendEvent:] + 6288
15  com.apple.AppKit               0x00007fff898704a6 -[NSApplication sendEvent:] + 4847
16  com.apple.AppKit               0x00007fff89808593 -[NSApplication run] + 541
17  com.apple.AppKit               0x00007fff8980133d NSApplicationMain + 860
18  com.myCompany.someApp            0x0000000100001d32 main + 34 (main.m:13)
19  com.myCompany.someApp            0x0000000100001d04 start + 52

Thread 1:
0   libsystem_kernel.dylib         0x00007fff8cb771b2 __workq_kernreturn + 10
1   libsystem_c.dylib              0x00007fff897414d9 _pthread_wqthread + 758
2   libsystem_c.dylib              0x00007fff89742599 start_wqthread + 13

Thread 2 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 2:
0   libsystem_kernel.dylib         0x00007fff8cb77806 kevent + 10
1   libdispatch.dylib              0x00007fff8cebaa26 _dispatch_mgr_invoke + 916
2   libdispatch.dylib              0x00007fff8cebb877 _dispatch_queue_invoke + 63
3   libdispatch.dylib              0x00007fff8cebb070 _dispatch_worker_thread2 + 198
4   libsystem_c.dylib              0x00007fff8974131f _pthread_wqthread + 316
5   libsystem_c.dylib              0x00007fff89742599 start_wqthread + 13

Thread 3:
0   libsystem_kernel.dylib         0x00007fff8cb771b2 __workq_kernreturn + 10
1   libsystem_c.dylib              0x00007fff897414d9 _pthread_wqthread + 758
2   libsystem_c.dylib              0x00007fff89742599 start_wqthread + 13

Thread 4:
0   libsystem_kernel.dylib         0x00007fff8cb771b2 __workq_kernreturn + 10
1   libsystem_c.dylib              0x00007fff897414d9 _pthread_wqthread + 758
2   libsystem_c.dylib              0x00007fff89742599 start_wqthread + 13

Thread 5 name:  com.apple.NSURLConnectionLoader
Thread 5:
0   libsystem_kernel.dylib         0x00007fff8cb756b6 mach_msg_trap + 10
1   libsystem_kernel.dylib         0x00007fff8cb74dad mach_msg + 73
2   com.apple.CoreFoundation       0x00007fff8ad291ec CFRunLoopServiceMachPort + 188
3   com.apple.CoreFoundation       0x00007fff8ac88a44 __CFRunLoopRun + 1204
4   com.apple.CoreFoundation       0x00007fff8ac88356 CFRunLoopRunSpecific + 230
5   com.apple.Foundation           0x00007fff86e77fca +[NSURLConnection(NSURLConnectionReallyInternal) _resourceLoadLoop:] + 335
6   com.apple.Foundation           0x00007fff86dff681 -[NSThread main] + 68
7   com.apple.Foundation           0x00007fff86dff5f9 __NSThread__main
+ 1575
8   libsystem_c.dylib              0x00007fff8973f804 _pthread_start + 335
9   libsystem_c.dylib              0x00007fff89742589 thread_start + 13

Thread 6 name:  com.apple.CFSocket.private
Thread 6:
0   libsystem_kernel.dylib         0x00007fff8cb76e12 __select + 10
1   com.apple.CoreFoundation       0x00007fff8aca7e1b __CFSocketManager + 1355
2   libsystem_c.dylib              0x00007fff8973f804 _pthread_start + 335
3   libsystem_c.dylib              0x00007fff89742589 thread_start + 13

Thread 0 crashed with X86 Thread State (64-bit):
 rax: 0x00000000069dcbc3  rbx: 0x00000001018170c0  rcx: 0x0000000000000000  rdx: 0x00007fff5fbfe468
 rdi: 0x00007fff5fbf8e00  rsi: 0x00007fff5fbf8f30  rbp: 0x00007fff5fbe69b0  rsp: 0x00007fff5fbdffa0
  r8: 0x0000000000000000   r9: 0x0000000100515d20  r10: 0x00007fff8ad62310  r11: 0x000000010011a540
 r12: 0x00007fff74c6b9f0  r13: 0x0000000101817400  r14: 0x000000010001e694  r15: 0x000000010182e0c0
 rip: 0x000000010000bab3  rfl: 0x0000000000010246  cr2: 0x00000000069dcbc3
Logical CPU: 4

Binary Images:
      0x100000000 -        0x100025ff7  com.myCompany.someApp (1.0 - 1) <50EC498D-ED9C-321A-B86B-224B4DA9AD55> /Users/USER/Library/Developer/Xcode/DerivedData/someApp-dzxzpbtokpnlvbfzxrgbssffgdva/Build/Products/Debug/someApp.app/Contents/MacOS/someAppsomeApp

     VM Region Summary:
ReadOnly portion of Libraries: Total=135.9M resident=70.0M(52%) swapped_out_or_unallocated=65.9M(48%)
Writable regions: Total=130.6M written=17.8M(14%) resident=27.5M(21%) swapped_out=0K(0%) unallocated=103.1M(79%)

REGION TYPE                      VIRTUAL
===========                      =======
CG backing stores                  5036K
CG image                             32K
CG raster data                      312K
CG shared images                   4496K
Carbon                             2324K
Core Image                           12K
CoreGraphics                         16K
MALLOC                             90.5M        see MALLOC ZONE table below
MALLOC freed, no zone              2588K
MALLOC guard page                    48K
MALLOC metadata                     540K
Memory tag=240                        4K
Memory tag=242                       12K
Memory tag=243                        8K
Memory tag=251                        8K
OpenCL                               12K
SQLite page cache                  1344K
STACK GUARD                        56.0M
Stack                              10.6M
VM_ALLOCATE                        16.1M
__CI_BITMAP                          72K
__DATA                             11.1M
__IMAGE                            1244K
__LINKEDIT                         43.4M
__TEXT                             92.5M
__UNICODE                           544K
mapped file                        35.5M
shared memory                       312K
===========                      =======
TOTAL                             374.3M

                                         VIRTUAL ALLOCATION      BYTES
MALLOC ZONE                                  SIZE      COUNT  ALLOCATED  % FULL
===========                               =======  =========  =========  ======
DefaultMallocZone_0x10003a000               80.4M      70135      13.7M     16%
DispatchContinuations_0x10006c000           6144K          1         32      0%
unnamed_zone_0x1004fa000                    4096K          8        480      0%
CoreGraphics_0x102000c00                      80K        618        12K     15%
x-alloc_0x1008a5400                            0K          2        224
DefaultPurgeableMallocZone_0x101fa8000         0K          0         0K
===========                               =======  =========  =========  ======
TOTAL                                       90.5M      70764      13.7M     15%

Model: MacPro5,1, BootROM MP51.007F.B00, 4 processors, Quad-Core Intel Xeon, 2.8 GHz, 8 GB, SMC 1.39f11
Graphics: ATI Radeon HD 5770, ATI Radeon HD 5770, PCIe, 1024 MB
Memory Module: DIMM 1, 2 GB, DDR3 ECC, 1066 MHz, 0x80AD, 0x484D54313235553742465238432D47372020
Memory Module: DIMM 2, 2 GB, DDR3 ECC, 1066 MHz, 0x80AD, 0x484D54313235553742465238432D47372020
Memory Module: DIMM 3, 2 GB, DDR3 ECC, 1066 MHz, 0x80AD, 0x484D54313235553742465238432D47372020
Memory Module: DIMM 4, 2 GB, DDR3 ECC, 1066 MHz, 0x80AD, 0x484D54313235553742465238432D47372020

JSONkit with arrays and objectatindex

Does JSONkit work with arrays? Ive got the following and using NSLogs I seem to have gotten my data correctly, but I am having some issues with tableview and the errors are new to me.

NSString *myJSON = @"http://www.example...";
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:myJSON]];
JSONDecoder *jsonKitDecoder = [JSONDecoder decoder];

myArray = [jsonKitDecoder objectWithData:jsonData]; //an array i declared in .h

the feed is an array of dictionaries and I've been able to use nested objectForKey, valueForKey and objectATIndex messages to get to all the data I want. I've even gotten it displayed in my tableview when the app first opens. The problem is as soon as i try and scroll the app crashes.

In the code editor, I get "Thread 1: Program received signal: "SIGABRT" on a line in titleForHEaderInSection: return [[myArray objectAtIndex:section] valueForKey:@"key"]; I've got the following right above that and that outputs to the log correctly, so its something to do with the scrolling and going to the next section thats off screen: NSLog(@"classser %@", [[[industries objectAtIndex:section] valueForKey:@"industry"]description]);

In the console I get "Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: (objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL)'". It takes me to line 745 of JSONKit.m which is an assertion in obejctatIndex ( NSParameterAssert((objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL)); ) I'm not adhering to, but I just don't know how as everything else I do shows my array and dictionaries as working.

Let me know what else you need as I'm still new to all this

Learning Question

Hi John,

I'm just trying to learn and understand your marvelous code :). I saw this and was confused by it. If you have a second I thought you might shed some light.

#if       defined (__GNUC__) && (__GNUC__ >= 4)
#define JK_ATTRIBUTES(attr, ...)        __attribute__((attr, ##__VA_ARGS__))
#define JK_EXPECTED(cond, expect)       __builtin_expect((long)(cond), (expect))
#define JK_EXPECT_T(cond)               JK_EXPECTED(cond, 1U)
#define JK_EXPECT_F(cond)               JK_EXPECTED(cond, 0U)
#define JK_PREFETCH(ptr)                __builtin_prefetch(ptr)
#else  // defined (__GNUC__) && (__GNUC__ >= 4) 
#define JK_ATTRIBUTES(attr, ...)
#define JK_EXPECTED(cond, expect)       (cond)
#define JK_EXPECT_T(cond)               (cond)
#define JK_EXPECT_F(cond)               (cond)
#define JK_PREFETCH(ptr)
#endif // defined (__GNUC__) && (__GNUC__ >= 4) 

JK_EXPECT_T looks like it's supposed to return true if the condition is true and JK_EXPECT_F looks like it's supposed to return true if the condition is false. But in the #else section they both return the same thing. Is this a bug or something that makes sense when defined (__GNUC__) && (__GNUC__ >= 4) is false?

objectWithData Snippet

Hello,
could you please show a snippet of basic decoding with objectWithData? thanks

I'm using the following code

self.decoder=[JSONDecoder decoderWithParseOptions:JKSerializeOptionNone];

NSError *error = nil;
NSObject *jsonObj = [decoder objectWithData:data error:&error];
if (data == nil || error) { // parsing or data error
    [self handleError:error];
    return;
}

// TODO: do something with NSObject

Crash on empty dictionary when trying to get a value in it

I'm getting a crash in the _JKDictionaryHashTableEntryForKey function on this line:

NSUInteger        keyHash = CFHash(aKey), keyEntry = (keyHash % dictionary->capacity), idx = 0UL;

I was calling valueForKeyPath: on a dictionary I got out of JSONKit that happened to have no entries. I think it's crashing because it's trying to mod the keyHash with zero.

Add a "stable" branch

Get around to adding a stable branch that can be the formal recommend branch for people that want to include JSONKit via a git submodule. The purpose would be to track "production quality and stable" changes to JSONKit inbetween version milestones.

The changes that would land in stable would be conservative, and people should always feel confident in syncing their local stable branch following versions of JSONKit with the latest commits to the stable branch in the github.com repository.

It might make some sense to create stable branches of different versions. This would allow people to "freeze" their projects JSONKit git submodule to a particular version, but still pick up important / critical fixes to that particular version without having to worry about whether changes required to implement new features for later versions interacting in unexpected ways with their app.

Comments from the community are welcome.

Possible memory leak in setObjectForKey

aKey = [ aKey copy ]; Looks like it could be leaking. We created about 30 objects then released them and ended up with a much larger footprint than we should have had in quick check.

Also in several places where you do anObject = [ anObject retain ]; should probably be [ anObject retain ]; since the assignment is technically considered "dead code" by the static analyzer.

Type mismatch

There is a warning converting CFHashCode to NSUInteger. I tried typecasting, but this craziness is out of my league :)

http://soff.me/4zWE

I'm using LLVM 2 by the way.

JSONString and NSNumbers

Encoding a dictionary with some NSNumbers gives me a JSON with all values ".17g". I have no idea what it means. Code:

NSMutableDictionary *json = [NSMutableDictionary dictionary];
[json setObject:n2 forKey:@"n2"];
[json setObject:n1 forKey:@"n1"];

NSError *error=nil;
NSString *p=[json JSONStringWithOptions:JKSerializeOptionNone error:&error];
NSLog(@"JSON: %@ %f %f",p,[n1 doubleValue],[n2 doubleValue]);

JSON output: '{"n2":.17g,"n1":.17g}'. No error.

LLVM GCC 4.2 with XCode 4. Debug build, no optimizations.

Crash when parsing empty (but valid) JSON "{}"

JSONKit crashes when parsing the string "{}" with a divide by zero error. The following change has fixed the issue for me:

--- a/Vendor/JSONKit/JSONKit.m
+++ b/Vendor/JSONKit/JSONKit.m
@@ -1005,7 +1005,8 @@ static void _JKDictionaryAddObject(JKDictionary *dictionary, NSUInteger keyHash,

static JKHashTableEntry *_JKDictionaryHashTableEntryForKey(JKDictionary *dictionary, id aKey) {
NSCParameterAssert((dictionary != NULL) && (dictionary->entry != NULL) && (dictionary->count <= dictionary->capacity));

  • if(aKey == NULL) { return(NULL); }
  • // Guard against a divide by zero here.
  • if(aKey == NULL || 0 == dictionary->capacity) { return(NULL); }
    NSUInteger keyHash = CFHash(aKey), keyEntry = (keyHash % dictionary->capacity), idx = 0UL;
    JKHashTableEntry *atEntry = NULL;
    for(idx = 0UL; idx < dictionary->capacity; idx++) {

Crash when creating -mutableCopy of parse result, then releasing the copy

  1. Parse JSON in NSData format by calling -objectFromJSONData
  2. Invoke -mutableCopy on the result
  3. Release (or autorelease) the result of -mutableCopy

The application will crash in JSONKit code. The crash point is as follows:

Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 CoreFoundation 0x00e8b057 _forwarding_ + 183
1 CoreFoundation 0x00e8af22 _CF_forwarding_prep_0 + 50
2 CoreFoundation 0x00e30a6c CFRelease + 92
3 CrashJSONKit 0x00001e7d jk_CFCallbackRelease + 17 (JSONKit.m:312)
4 CoreFoundation 0x00e53b22 __CFArrayReleaseValues + 178
5 CoreFoundation 0x00e30bcb _CFRelease + 251
6 CoreFoundation 0x00e55b8d _CFAutoreleasePoolPop + 237
7 Foundation 0x0001f443 -[NSAutoreleasePool release] + 167
8 UIKit 0x002d0bf2 _UIApplicationHandleEvent + 8792
9 GraphicsServices 0x0184fa36 PurpleEventCallback + 1550

I've created a minimal sample project which exhibits the crash. The above code is all that exists in it other than standard app setup code. It doesn't have any strange build flags or link against any non-vanilla frameworks.

Download the sample project at: http://dl.dropbox.com/u/102780/CrashJSONKit.zip

This is on Xcode 3.2.5 against iOS SDK 4.2.

Feature Request: Call backs for Deserializing

I would try to do this myself but the level of sophistication in JSONKit is far beyond me. Perhaps this wouldn't even be possible without really slowing JSONKit down?

Anyways, there are two problems I have that could be fixed by something like this.

  1. Our company's JSON passes dates as strings in the format "/Date(1305758490476)/" (This is something that .net does). It would be great if I could have a callback, see this is in the date format, and return an NSDate.
  2. I hate NSNulls! I know that sometimes you really need to keep nulls in your collections, however, usually NSNulls cause a lot more work. For example, I get a url for an image. I have to double check that it's not an NSNull before creating an NSURL from the string. What a pain! The other option I'm left with is going through all the objects manually and getting rid of NSNulls. So I would like to get a callback and return nil so the NSNull won't be added in.

Anyways, thoughts?

_JKDictionaryHashTableEntryForKey crashes for NULL capacity

I have had great success using JSONKit -- however today I was getting a crash in _JKDictionaryHashTableEntryForKey. As I dug in, it seems that dictionary->capacity was NULL, and the code was dying here:

NSUInteger keyHash = CFHash(aKey), keyEntry = (keyHash % dictionary->capacity), idx = 0UL;

I added a NULL check immediately above:

if(!dictionary->capacity) { return(NULL); }

and now things seem to be going through.

Jack

Integral double values are encoded as integers

if I run this code:

static NSString * key = @"doubleKey";
NSNumber * doubleNumber = [NSNumber numberWithDouble:22];
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setObject:doubleNumber forKey:key];
NSLog("%@", [dict JSONString]);

I get this json:

{"doubleKey":22}

I'm not intimately familiar with the JSON spec, but this encoding was causing me some problems where I expected a double to come back on decode, but an int came back.

I implemented a possible fix locally, but I'd like to hear feedback before I fork and submit a pull request. The fix is to change the format string at https://github.com/johnezang/JSONKit/blob/master/JSONKit.m#L2755, which is currently "%.17g" to "%#.17g" in order to force trailing zeroes for double values that are also whole numbers.

Type conversions and deprecated methods

With the following build options enabled, there are 21 errors:

Implicit Conversion to 32 Bit Type
Implicit Signedness Conversions
Suspicious Implicit Conversions
Overriding Deprecated Objective-C Methods

It would be awesome if JSONKit didn't cause errors with these options enabled. I tried fixing the issues, but your crazy C stuff is a bit over my head.

NSNull -> nil

Any plan on supporting this like TouchJSON and other parsers do? For now I've added an NSDictionary category that checks for null values and return nil instead. I've tried adding that directly to the parser but that code is way beyond my knowledge ^^

JKDictionary over releassed if inside a NSAutoreleasePool

I am using ASIHTTP to download gzip'd data, that is then passed to JSONkit.

ASI has memory issues that can be addressed by wrapping the calls in a
NSAutoreleasePool then releasing when done. I am downloading a LARGE ( very Large) amount of data and I release the NSAutoreleasePool after every download because ASI will eat a few MB in a few calls..
The side affect of this is the JSONKit calls are also inside the NSAutoreleasePool / release code. When I release my NSAutoreleasePool I receive and error saying JKDictionary release]; was over released and the app crashes.

Any ideas how this can be addressed?
Jeremy

Cannot serialize integers in Mac OS X 10.7

The following code will crash in Mac OS X Lion Developer Preview 2:

[[NSArray arrayWithObject:[NSNumber numberWithInteger:1]] JSONString]

The crash happens when you try to get the isa value for the number object because they seem to be optimized special pointers in the runtime now.

Add Unit Tests to repository

I'm assuming you have some form of automated tests, and I think these should be available in the repository. For those of us making modifications on the codebase it's pretty essential that we have some tests to make sure we are not breaking stuff.

Escaping backslash incorrectly when backslash is used to escape forward slash as in ASP.NET JSON date format

When JSONData is called on a NSDictionary that has a key value pair that has a string encoded with ASP.NET Date format (http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx) the backslash in the date format is incorrectly escaped with another backslash.

For example:

"/Date(1303502009425)/"

is encoded as:

"/Date(1303502009425)/"

This should not be the case because the JSON spec indicates that the forward slash can optionally be escaped.

Parsing JSON streams

What are your thoughts on adding support for parsing streams of JSON-encoded data? Would there be enough of a speed gain to warrant this type of parsing for webservices that return large amounts of data?

Thank you so much for this project. So far it has been both easy to use and incredibly fast.

License?

What license are you distributing this under? MIT, GPL, etc...

Memory leak in jk_cachedObjects

I observe memory leaks, which may be quite significant for me.

I do the following code in my view controller:

JSONDecoder *json = [JSONDecoder decoder];

if (![data isKindOfClass:[NSData class]])
{
    // Error occurred
    return;
}

// Ok, data is received, parse it
NSDictionary *parsed = [json objectWithData:data];

As soon as I move to another controller I get:

Leaked Object # Address Size Responsible Library Responsible Frame
NSCFString,5 < multiple > 144 Bytes App jk_cachedObjects
NSCFString,1 0x18ac40 32 Bytes App jk_cachedObjects
NSCFString,1 0x18b340 16 Bytes App jk_cachedObjects
NSCFString,1 0x18b7e0 32 Bytes App jk_cachedObjects
NSCFString,1 0x18bc90 32 Bytes App jk_cachedObjects
NSCFString,1 0x18cb60 32 Bytes App jk_cachedObjects

id Address Category Event Type RefCt Timestamp Size Responsible Library Responsible Caller
0 0x18ac40 CFString (immutable) Malloc 1 00:15.891.265 32 App jk_cachedObjects
1 0x18ac40 CFString (immutable) CFRetain 2 00:15.891.302 0 App jk_cachedObjects
2 0x18ac40 CFString (immutable) CFRetain 3 00:15.903.345 0 Foundation -[NSCFString retain]
3 0x18ac40 CFString (immutable) CFRelease 2 00:15.973.275 0 App -[JKDictionary dealloc]
4 0x18ac40 CFString (immutable) CFRelease 1 00:15.980.459 0 App -[JSONDecoder clearCache]

I shouldn't do the following, is it? At least it's not obvious that I should.

[json release];
[parsed release];

I parse lots of data (at least once each 5 minutes), so such leak is quite critical for me.
Can you be so kind as to dig into this? May be it's just my stupid fault, but it doesn't look like that for me right now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.