diff --git a/CHANGELOG.md b/CHANGELOG.md index a23cefd..d57e92f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ### New Features +* Version 1.5 introduces new option, `JKParseOptionPermitLeadingZero` which permits leading zeros when parsing numeric objects. + * When `JKSerializeOptionPretty` is enabled, JSONKit now sorts the keys. * Normally, JSONKit can only serialize NSNull, NSNumber, NSString, NSArray, and NSDictioonary like objects. It is now possible to serialize an object of any class via either a delegate or a `^` block. diff --git a/JSONKit.h b/JSONKit.h index 71bd0c3..1fa965a 100644 --- a/JSONKit.h +++ b/JSONKit.h @@ -102,7 +102,7 @@ typedef unsigned int NSUInteger; #endif #define JSONKIT_VERSION_MAJOR 1 -#define JSONKIT_VERSION_MINOR 4 +#define JSONKIT_VERSION_MINOR 5 typedef NSUInteger JKFlags; @@ -121,7 +121,8 @@ enum { JKParseOptionUnicodeNewlines = (1 << 1), JKParseOptionLooseUnicode = (1 << 2), JKParseOptionPermitTextAfterValidJSON = (1 << 3), - JKParseOptionValidFlags = (JKParseOptionComments | JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode | JKParseOptionPermitTextAfterValidJSON), + JKParseOptionPermitLeadingZero = (1 << 4), + JKParseOptionValidFlags = (JKParseOptionComments | JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode | JKParseOptionPermitTextAfterValidJSON | JKParseOptionPermitLeadingZero), }; typedef JKFlags JKParseOptionFlags; diff --git a/JSONKit.m b/JSONKit.m index 0e9331f..9891d78 100644 --- a/JSONKit.m +++ b/JSONKit.m @@ -1643,8 +1643,8 @@ static int jk_parse_number(JKParseState *parseState) { switch(numberState) { case JSONNumberStateWholeNumberStart: if (currentChar == '-') { numberState = JSONNumberStateWholeNumberMinus; isNegative = 1; break; } - case JSONNumberStateWholeNumberMinus: if (currentChar == '0') { numberState = JSONNumberStateWholeNumberZero; break; } - else if( (currentChar >= '1') && (currentChar <= '9')) { numberState = JSONNumberStateWholeNumber; break; } + case JSONNumberStateWholeNumberMinus: if (currentChar == '0' && !(parseState->parseOptionFlags & JKParseOptionPermitLeadingZero)) { numberState = JSONNumberStateWholeNumberZero; break; } + else if( (currentChar >= '0') && (currentChar <= '9')) { numberState = JSONNumberStateWholeNumber; break; } else { /* XXX Add error message */ numberState = JSONNumberStateError; break; } case JSONNumberStateExponentStart: if( (currentChar == '+') || (currentChar == '-')) { numberState = JSONNumberStateExponentPlusMinus; break; } case JSONNumberStateFractionalNumberStart: diff --git a/README.md b/README.md index 8b4a4d9..78c1333 100644 --- a/README.md +++ b/README.md @@ -221,6 +221,7 @@ The objectWith… methods return immutable collection objects 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. + JKParseOptionPermitLeadingZeroNormally, leading zeros on unquoted numeric values is interpreted as a parsing failure. This option allows for leading zeros to be ignored and not cause a parsing error. ### Serializing Interface