Skip to content

Commit

Permalink
Parse NaN from object like {"n": NaN}
Browse files Browse the repository at this point in the history
Parsing is case-insensitive. Closes #24.
  • Loading branch information
ajdavis committed Apr 6, 2017
1 parent e21dd0a commit 7b153f1
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 7 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ compiler:
- gcc
- clang

script: make && make check
env:
- PARSE_NAN=1
- PARSE_NAN=0

script: ./compile-travis.sh
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ else
LIBFLAGS=$(DYLIBFLAGS)
endif

ifdef JSONSL_PARSE_NAN
CFLAGS+="-DJSONSL_PARSE_NAN"
endif

all: $(LIB_FQNAME)

install: all
Expand Down
7 changes: 7 additions & 0 deletions README.pod
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,13 @@ instead of the good 'ole C<char>. Of course you would need to
handle processing the stream correctly to make sure the multibyte
stream was complete.

=head2 NaN

By default, JSONSL does not consider an object like C<{"n": NaN}> to be valid
syntax. Compile with C<JSONSL_PARSE_NAN> defined to parse NaN as "not-a-number".
JSONSL will then execute your POP callback with C<state-E<gt>special_flags> set
to C<JSONSL_SPECIALf_NUMNOINT | JSONSL_SPECIALf_NAN> when it parses a NaN.

=head2 WINDOWS

JSONSL Now has a visual studio C<.sln> and C<.vcxproj> files in the
Expand Down
8 changes: 8 additions & 0 deletions compile-travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -ev

if [ "${PARSE_NAN}" = "1" ]; then
make JSONSL_PARSE_NAN=1 && make JSONSL_PARSE_NAN=1 check
else
make && make check
fi
Binary file modified json_samples.tgz
Binary file not shown.
24 changes: 21 additions & 3 deletions jsonsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,23 @@ jsonsl_feed(jsonsl_t jsn, const jsonsl_char_t *bytes, size_t nbytes)
VERIFY_SPECIAL("false");
} else if (state->special_flags == JSONSL_SPECIALf_NULL) {
VERIFY_SPECIAL("null");
#ifdef JSONSL_PARSE_NAN
} else if (state->special_flags == JSONSL_SPECIALf_NAN) {
/* like VERIFY_SPECIAL but case-insensitive */
if (tolower(CUR_CHAR) != "nan"[jsn->pos - state->pos_begin]) {
INVOKE_ERROR(SPECIAL_EXPECTED);
}
} else if (state->special_flags & JSONSL_SPECIALf_NULL ||
state->special_flags & JSONSL_SPECIALf_NAN) {
/* previous char was "n", are we parsing null or nan? */
if (CUR_CHAR != 'u') {
state->special_flags &= ~JSONSL_SPECIALf_NULL;
}

if (tolower(CUR_CHAR) != 'a') {
state->special_flags &= ~JSONSL_SPECIALf_NAN;
}
#endif
}
INCR_METRIC(SPECIAL_FASTPATH);
CONTINUE_NEXT_CHAR();
Expand Down Expand Up @@ -1425,11 +1442,12 @@ static unsigned short Special_Table[0x100] = {
/* 0x37 */ JSONSL_SPECIALf_UNSIGNED /* <7> */, /* 0x37 */
/* 0x38 */ JSONSL_SPECIALf_UNSIGNED /* <8> */, /* 0x38 */
/* 0x39 */ JSONSL_SPECIALf_UNSIGNED /* <9> */, /* 0x39 */
/* 0x3a */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x59 */
/* 0x5a */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x65 */
/* 0x3a */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4d */
/* 0x4e */ JSONSL__NAN_PROXY /* <N> */, /* 0x4e */
/* 0x4f */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x65 */
/* 0x66 */ JSONSL_SPECIALf_FALSE /* <f> */, /* 0x66 */
/* 0x67 */ 0,0,0,0,0,0,0, /* 0x6d */
/* 0x6e */ JSONSL_SPECIALf_NULL /* <n> */, /* 0x6e */
/* 0x6e */ JSONSL_SPECIALf_NULL|JSONSL__NAN_PROXY /* <n> */, /* 0x6e */
/* 0x6f */ 0,0,0,0,0, /* 0x73 */
/* 0x74 */ JSONSL_SPECIALf_TRUE /* <t> */, /* 0x74 */
/* 0x75 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x94 */
Expand Down
12 changes: 10 additions & 2 deletions jsonsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ typedef char jsonsl_char_t;
typedef unsigned char jsonsl_uchar_t;
#endif /* JSONSL_USE_WCHAR */

#ifdef JSONSL_PARSE_NAN
#define JSONSL__NAN_PROXY JSONSL_SPECIALf_NAN
#else
#define JSONSL__NAN_PROXY 0
#endif

/* Stolen from http-parser.h, and possibly others */
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
typedef __int8 int8_t;
Expand Down Expand Up @@ -143,7 +149,8 @@ typedef enum {
X(NULL, 1<<4) \
X(FLOAT, 1<<5) \
X(EXPONENT, 1<<6) \
X(NONASCII, 1<<7)
X(NONASCII, 1<<7) \
X(NAN, 1<<8)
typedef enum {
#define X(o,b) \
JSONSL_SPECIALf_##o = b,
Expand All @@ -165,7 +172,8 @@ typedef enum {
JSONSL_SPECIALf_BOOLEAN = (JSONSL_SPECIALf_TRUE|JSONSL_SPECIALf_FALSE),

/** Type is an "extended", not integral type (but numeric) */
JSONSL_SPECIALf_NUMNOINT = (JSONSL_SPECIALf_FLOAT|JSONSL_SPECIALf_EXPONENT)
JSONSL_SPECIALf_NUMNOINT =
(JSONSL_SPECIALf_FLOAT|JSONSL_SPECIALf_EXPONENT|JSONSL_SPECIALf_NAN)
} jsonsl_special_t;


Expand Down
3 changes: 2 additions & 1 deletion srcutil/genchartables.pl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
$special_begin[ord('-')] = 'JSONSL_SPECIALf_DASH';
$special_begin[ord('t')] = 'JSONSL_SPECIALf_TRUE';
$special_begin[ord('f')] = 'JSONSL_SPECIALf_FALSE';
$special_begin[ord('n')] = 'JSONSL_SPECIALf_NULL';
$special_begin[ord('n')] = 'JSONSL_SPECIALf_NULL|JSONSL__NAN_PROXY';
$special_begin[ord('N')] = 'JSONSL__NAN_PROXY';
$special_begin[ord($_)] = 'JSONSL_SPECIALf_UNSIGNED' for (0..9);
$special_begin[ord('0')] = 'JSONSL_SPECIALf_ZERO';

Expand Down
7 changes: 7 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ all: $(TESTMODS)
./unescape
./json_test ../share/jsc/pass*.json
JSONSL_FAIL_TESTS=1 ./json_test ../share/jsc/fail*.json
ifneq (,$(findstring JSONSL_PARSE_NAN,$(CFLAGS)))
./json_test ../share/nan/*.json
else
JSONSL_FAIL_TESTS=1 ./json_test ../share/nan/*.json
endif
@echo " Some tests were skipped."
@echo " See share/jsc/nyi_fail/README.skipped for details"
@echo "All Tests OK"

%: %.c
echo "LIBFLAGS ${LIBFLAGS}"
echo "LDFLAGS ${LDFLAGS}"
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

clean:
Expand Down

0 comments on commit 7b153f1

Please sign in to comment.