Skip to content

Commit

Permalink
Overhaul the feature macros
Browse files Browse the repository at this point in the history
Parent links and strict parsing are now the default behavior. New macros
JSMN_LOW_MEMORY and JSMN_NON_STRICT disable these behaviors.

JSMN_PARENT_LINKS still exists, but is defined by default unless
JSMN_LOW_MEMORY is defined.

JSMN_STRICT no longer exists. Instead, we have three new macros:

JSMN_PERMISSIVE_PRIMITIVES - Relaxes validation of primitives. Any
characters except whitespace and {}[],:" become allowed. (Normally, only
"true", "false", "null", and RFC 8259 numbers are permitted.)

JSMN_PERMISSIVE_STRINGS - Relaxes validation of strings. Any characters
allowed. (Normally, control characters (<0x20) and invalid escape
sequences are foridden.)

JSMN_PRIMITIVE_KEYS - Allows primitives to be used as object keys.

These can be defined individually, or defining JSMN_NON_STRICT will
cause all to be defined.

Tests have not yet been adapted for these changes.
  • Loading branch information
dominickpastore committed Jun 1, 2020
1 parent 44ee942 commit de2b56a
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ extern "C" {
#define JSMN_API extern
#endif

#ifndef JSMN_LOW_MEMORY
#ifndef JSMN_PARENT_LINKS
#define JSMN_PARENT_LINKS
#endif
#endif

#ifdef JSMN_NON_STRICT
#ifndef
#define JSMN_PERMISSIVE_PRIMITIVES
#endif
#ifndef
#define JSMN_PERMISSIVE_STRINGS
#endif
#ifndef
#define JSMN_PRIMITIVE_KEYS
#endif
#endif

/**
* JSON type identifier. Basic types are:
* o Object
Expand Down Expand Up @@ -168,7 +186,7 @@ static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
token->end = end;
}

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
static const char true_str[] = "true";
static const char false_str[] = "false";
static const char null_str[] = "null";
Expand Down Expand Up @@ -197,15 +215,15 @@ static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
const size_t num_tokens, int extend) {
jsmntok_t *token;
int start;
#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
int i;
if (extend) {
parser->pos = tokens[parser->toknext - 1].start;
}
#endif
start = parser->pos;

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
if (js[parser->pos] == 't') {
for (i = 1;
i < strlen(true_str);
Expand Down Expand Up @@ -440,7 +458,7 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
if (c == '\\' && parser->pos + 1 < len) {
int i;
parser->pos++;
#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_STRINGS
switch (js[parser->pos]) {
/* Allowed escaped symbols */
case '\"':
Expand Down Expand Up @@ -476,7 +494,7 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
#endif
}

#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_STRINGS
/* No control characters allowed in strict mode */
if (c >= 0 && c < 32) {
parser->pos = start;
Expand Down Expand Up @@ -675,7 +693,7 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
return JSMN_ERROR_INVAL;
}
break;
#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
/* In strict mode primitives are: numbers and booleans */
case '-':
case '0':
Expand Down Expand Up @@ -707,28 +725,28 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
parser->toksuper = parser->toknext - 1;
parser->state = JSMN_STATE_ROOT;
break;
#ifdef JSMN_STRICT
} else if (parser->state & (JSMN_DELIMITER | JSMN_KEY)) {
#else
#ifdef JSMN_PRIMITIVE_KEYS
} else if (parser->state & JSMN_DELIMITER) {
#else
} else if (parser->state & (JSMN_DELIMITER | JSMN_KEY)) {
#endif
return JSMN_ERROR_INVAL;
}
tokens[parser->toksuper].size++;
#ifdef JSMN_PARENT_LINKS
tokens[parser->toknext - 1].parent = parser->toksuper;
#endif
#ifdef JSMN_STRICT
parser->state |= JSMN_DELIMITER | JSMN_CAN_CLOSE;
#else
#ifdef JSMN_PRIMITIVE_KEYS
if (parser->state & JSMN_KEY) {
parser->state = JSMN_STATE_OBJ_COLON;
} else {
parser->state |= JSMN_DELIMITER | JSMN_CAN_CLOSE;
}
#else
parser->state |= JSMN_DELIMITER | JSMN_CAN_CLOSE;
#endif
break;
#ifdef JSMN_STRICT
#ifndef JSMN_PERMISSIVE_PRIMITIVES
/* Unexpected char in strict mode */
default:
return JSMN_ERROR_INVAL;
Expand Down

0 comments on commit de2b56a

Please sign in to comment.