Start of JSON5 support

Added yajl_allow_json5 config flag, pass it around.
Added -5 option to yajl_test and yajlTestConverter.pl
This commit is contained in:
Andrew Johnson
2020-07-06 23:45:47 -05:00
parent d381a936b5
commit ae604b2a55
6 changed files with 29 additions and 8 deletions

View File

@@ -95,6 +95,7 @@ yajl_config(yajl_handle h, int option, ...)
case yajl_allow_trailing_garbage:
case yajl_allow_multiple_values:
case yajl_allow_partial_values:
case yajl_allow_json5:
if (va_arg(ap, int)) h->flags |= opt;
else h->flags &= ~opt;
break;
@@ -128,7 +129,8 @@ yajl_parse(yajl_handle hand, const unsigned char * jsonText,
if (hand->lexer == NULL) {
hand->lexer = yajl_lex_alloc(&(hand->alloc),
hand->flags & yajl_allow_comments,
!(hand->flags & yajl_dont_validate_strings));
!(hand->flags & yajl_dont_validate_strings),
hand->flags & yajl_allow_json5);
}
if (hand->lexer == NULL) {
return yajl_status_error;
@@ -151,7 +153,8 @@ yajl_complete_parse(yajl_handle hand)
if (hand->lexer == NULL) {
hand->lexer = yajl_lex_alloc(&(hand->alloc),
hand->flags & yajl_allow_comments,
!(hand->flags & yajl_dont_validate_strings));
!(hand->flags & yajl_dont_validate_strings),
hand->flags & yajl_allow_json5);
}
return yajl_do_finish(hand);

View File

@@ -87,6 +87,9 @@ struct yajl_lexer_t {
/* shall we allow comments? */
unsigned int allowComments;
/* are we parsing JSON5? */
unsigned int allowJson5;
/* shall we validate utf8 inside strings? */
unsigned int validateUTF8;
@@ -102,7 +105,8 @@ struct yajl_lexer_t {
yajl_lexer
yajl_lex_alloc(yajl_alloc_funcs * alloc,
unsigned int allowComments, unsigned int validateUTF8)
unsigned int allowComments, unsigned int validateUTF8,
unsigned int allowJson5)
{
yajl_lexer lxr = (yajl_lexer) YA_MALLOC(alloc, sizeof(struct yajl_lexer_t));
if (lxr == NULL) {
@@ -113,6 +117,7 @@ yajl_lex_alloc(yajl_alloc_funcs * alloc,
lxr->buf = yajl_buf_alloc(alloc);
lxr->allowComments = allowComments;
lxr->validateUTF8 = validateUTF8;
lxr->allowJson5 = allowJson5;
lxr->alloc = alloc;
return lxr;
}

View File

@@ -53,7 +53,8 @@ extern "C" {
yajl_lexer yajl_lex_alloc(yajl_alloc_funcs * alloc,
unsigned int allowComments,
unsigned int validateUTF8);
unsigned int validateUTF8,
unsigned int allowJson5);
void yajl_lex_free(yajl_lexer lexer);

View File

@@ -155,7 +155,13 @@ extern "C" {
* yajl will enter an error state (premature EOF). Setting this
* flag suppresses that check and the corresponding error.
*/
yajl_allow_partial_values = 0x10
yajl_allow_partial_values = 0x10,
/**
* The JSON5 standard allows additional formats for numbers, strings
* and object keys which are not permitted in the JSON standard.
* Setting this flag enables JSON5 formats in the lexer and parser.
*/
yajl_allow_json5 = 0x20,
} yajl_option;
/** Allow the modification of parser options subsequent to handle

View File

@@ -22,14 +22,17 @@ my $caseFile = 'yajlTestCases.pm';
my @cases;
for my $file (@files) {
$file =~ m|/([afn][cgmp]_)?([^/]*)\.json$|;
$file =~ m|/([afn][5cgmp]_)?([^/]*)\.json$|;
my $allow = $1;
my $name = $2;
next if $name eq '';
my $case = { name => $name };
if ($allow eq 'ac_') {
if ($allow eq 'a5_') {
$case->{opts} = ['-5'];
}
elsif ($allow eq 'ac_') {
$case->{opts} = ['-c'];
}
elsif ($allow eq 'ag_') {

View File

@@ -154,6 +154,7 @@ static void usage(const char * progname)
"usage: %s [options]\n"
"Parse input from stdin as JSON and ouput parsing details "
"to stdout\n"
" -5 allow JSON5\n"
" -b set the read buffer size\n"
" -c allow comments\n"
" -g allow *g*arbage after valid JSON text\n"
@@ -197,7 +198,9 @@ main(int argc, char ** argv)
/* check arguments. We expect exactly one! */
for (i=1;i<argc;i++) {
if (!strcmp("-c", argv[i])) {
if (!strcmp("-5", argv[i])) {
yajl_config(hand, yajl_allow_json5, 1);
} else if (!strcmp("-c", argv[i])) {
yajl_config(hand, yajl_allow_comments, 1);
} else if (!strcmp("-b", argv[i])) {
if (++i >= argc) usage(argv[0]);