Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New JKParseOptionPermitLeadingZero option #127

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions JSONKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions JSONKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ The <code>objectWith&hellip;</code> methods return immutable collection objects
<tr><td valign="top"><code>JKParseOptionUnicodeNewlines</code></td><td>Allow Unicode recommended <code>(?:\r\n|[\n\v\f\r\x85\p{Zl}\p{Zp}])</code> newlines in JSON. The <a href="http://tools.ietf.org/html/rfc4627">JSON specification</a> only allows the newline characters <code>\r</code> and <code>\n</code>, but this option allows JSON that contains the <a href="http://en.wikipedia.org/wiki/Newline#Unicode">Unicode recommended newline characters</a> to be parsed. JSON that contains these additional newline characters is not strictly conforming JSON.</td></tr>
<tr><td valign="top"><code>JKParseOptionLooseUnicode</code></td><td>Normally 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 <code>\uFFFD</code>, or <code>REPLACEMENT CHARACTER</code>, as specified in <a href="http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf">The Unicode 6.0 standard, Chapter 3</a>, section 3.9 <em>Unicode Encoding Forms</em>.</td></tr>
<tr><td valign="top"><code>JKParseOptionPermitTextAfterValidJSON</code></td><td>Normally, <code>non-white-space</code> that follows the JSON is interpreted as a parsing failure. This option allows for any trailing <code>non-white-space</code> to be ignored and not cause a parsing error.</td></tr>
<tr><td valign="top"><code>JKParseOptionPermitLeadingZero</code></td><td>Normally, 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.</td></tr>
</table>

### Serializing Interface
Expand Down