From 14817420a74c30888786f203b1f008df41021736 Mon Sep 17 00:00:00 2001 From: robertmryan Date: Wed, 15 May 2013 13:58:26 -0400 Subject: [PATCH 1/2] Added JKParseOptionPermitLeadingZero option --- CHANGELOG.md | 2 ++ JSONKit.h | 5 +++-- JSONKit.m | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) 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: From cb7879f0c5834a70cc2e7980ab2e76933d1907c1 Mon Sep 17 00:00:00 2001 From: robertmryan Date: Wed, 15 May 2013 14:14:28 -0400 Subject: [PATCH 2/2] Added JKParseOptionPermitLeadingZero option to permit leading zeros when parsing numeric values. --- README.md | 1 + 1 file changed, 1 insertion(+) 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