Skip to content

Commit

Permalink
Merge pull request #680 from casperisfine/trailing-comma
Browse files Browse the repository at this point in the history
Add option :allow_trailing_comma to JSON#parse
  • Loading branch information
byroot authored Nov 4, 2024
2 parents 4af700e + ef91aea commit 5ce7142
Show file tree
Hide file tree
Showing 8 changed files with 1,122 additions and 391 deletions.
910 changes: 677 additions & 233 deletions ext/json/ext/parser/parser.c

Large diffs are not rendered by default.

33 changes: 20 additions & 13 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ static ID i_json_creatable_p, i_json_create, i_create_id,
i_chr, i_deep_const_get, i_match, i_aset, i_aref,
i_leftshift, i_new, i_try_convert, i_uminus, i_encode;

static VALUE sym_max_nesting, sym_allow_nan, sym_symbolize_names, sym_freeze,
static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_symbolize_names, sym_freeze,
sym_create_additions, sym_create_id, sym_object_class, sym_array_class,
sym_decimal_class, sym_match_string;

Expand Down Expand Up @@ -383,6 +383,7 @@ typedef struct JSON_ParserStruct {
FBuffer fbuffer;
int max_nesting;
bool allow_nan;
bool allow_trailing_comma;
bool parsing_name;
bool symbolize_names;
bool freeze;
Expand Down Expand Up @@ -477,6 +478,8 @@ static void raise_parse_error(const char *format, const char *start)
}
}

action allow_trailing_comma { json->allow_trailing_comma }

action parse_name {
char *np;
json->parsing_name = true;
Expand All @@ -495,7 +498,7 @@ static void raise_parse_error(const char *format, const char *start)

main := (
begin_object
(pair (next_pair)*)? ignore*
(pair (next_pair)*((ignore* value_separator) when allow_trailing_comma)?)? ignore*
end_object
) @exit;
}%%
Expand Down Expand Up @@ -788,13 +791,15 @@ static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *resul
}
}

action allow_trailing_comma { json->allow_trailing_comma }

action exit { fhold; fbreak; }

next_element = value_separator ignore* begin_value >parse_value;

main := begin_array ignore*
((begin_value >parse_value ignore*)
(ignore* next_element ignore*)*)?
(ignore* next_element ignore*)*((value_separator ignore*) when allow_trailing_comma)?)?
end_array @exit;
}%%

Expand Down Expand Up @@ -1073,16 +1078,17 @@ static int configure_parser_i(VALUE key, VALUE val, VALUE data)
{
JSON_Parser *json = (JSON_Parser *)data;

if (key == sym_max_nesting) { json->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
else if (key == sym_allow_nan) { json->allow_nan = RTEST(val); }
else if (key == sym_symbolize_names) { json->symbolize_names = RTEST(val); }
else if (key == sym_freeze) { json->freeze = RTEST(val); }
else if (key == sym_create_id) { json->create_id = RTEST(val) ? val : Qfalse; }
else if (key == sym_object_class) { json->object_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_array_class) { json->array_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_decimal_class) { json->decimal_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_match_string) { json->match_string = RTEST(val) ? val : Qfalse; }
else if (key == sym_create_additions) {
if (key == sym_max_nesting) { json->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
else if (key == sym_allow_nan) { json->allow_nan = RTEST(val); }
else if (key == sym_allow_trailing_comma) { json->allow_trailing_comma = RTEST(val); }
else if (key == sym_symbolize_names) { json->symbolize_names = RTEST(val); }
else if (key == sym_freeze) { json->freeze = RTEST(val); }
else if (key == sym_create_id) { json->create_id = RTEST(val) ? val : Qfalse; }
else if (key == sym_object_class) { json->object_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_array_class) { json->array_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_decimal_class) { json->decimal_class = RTEST(val) ? val : Qfalse; }
else if (key == sym_match_string) { json->match_string = RTEST(val) ? val : Qfalse; }
else if (key == sym_create_additions) {
if (NIL_P(val)) {
json->create_additions = true;
json->deprecated_create_additions = true;
Expand Down Expand Up @@ -1358,6 +1364,7 @@ void Init_parser(void)

sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
sym_allow_trailing_comma = ID2SYM(rb_intern("allow_trailing_comma"));
sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));
sym_freeze = ID2SYM(rb_intern("freeze"));
sym_create_additions = ID2SYM(rb_intern("create_additions"));
Expand Down
Loading

0 comments on commit 5ce7142

Please sign in to comment.