Have example/jsonetc read and write extra commas
diff --git a/example/jsonfindptrs/jsonfindptrs.cc b/example/jsonfindptrs/jsonfindptrs.cc
index 2a83d35..7eb845c 100644
--- a/example/jsonfindptrs/jsonfindptrs.cc
+++ b/example/jsonfindptrs/jsonfindptrs.cc
@@ -108,6 +108,7 @@
"Flags:\n"
" -d=NUM -max-output-depth=NUM\n"
" -s -strict-json-pointer-syntax\n"
+ " -input-json-extra-comma\n"
"\n"
"The input.json filename is optional. If absent, it reads from stdin.\n"
"\n"
@@ -151,6 +152,9 @@
"This JSON implementation also rejects integer values outside ±M, where\n"
"M is ((1<<53)-1), also known as JavaScript's Number.MAX_SAFE_INTEGER.\n"
"\n"
+ "The -input-json-extra-comma flag allows input like \"[1,2,]\", with a\n"
+ "comma after the final element of a JSON list or dictionary.\n"
+ "\n"
"----\n"
"\n"
"The -s or -strict-json-pointer-syntax flag restricts the output lines\n"
@@ -180,6 +184,7 @@
int remaining_argc;
char** remaining_argv;
+ bool input_json_extra_comma;
uint32_t max_output_depth;
bool strict_json_pointer_syntax;
} g_flags = {0};
@@ -224,6 +229,10 @@
}
return g_usage;
}
+ if (!strcmp(arg, "input-json-extra-comma")) {
+ g_flags.input_json_extra_comma = true;
+ continue;
+ }
if (!strcmp(arg, "s") || !strcmp(arg, "strict-json-pointer-syntax")) {
g_flags.strict_json_pointer_syntax = true;
continue;
@@ -282,12 +291,15 @@
m_status =
m_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
- // Uncomment these lines to enable the WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X
- // option, discussed in a separate comment.
- //
- // if (m_status.is_ok()) {
- // m_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X, true);
- // }
+ if (m_status.is_ok()) {
+ // Uncomment this line to enable the WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X
+ // option, discussed in a separate comment.
+ // m_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X, true);
+
+ if (g_flags.input_json_extra_comma) {
+ m_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true);
+ }
+ }
}
Result peek() { return peek_or_next(false); }
diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc
index 5421d2f..550d060 100644
--- a/example/jsonptr/jsonptr.cc
+++ b/example/jsonptr/jsonptr.cc
@@ -157,6 +157,8 @@
" -s -strict-json-pointer-syntax\n"
" -t -tabs\n"
" -fail-if-unsandboxed\n"
+ " -input-json-extra-comma\n"
+ " -output-json-extra-comma\n"
"\n"
"The input.json filename is optional. If absent, it reads from stdin.\n"
"\n"
@@ -174,6 +176,14 @@
"on its own line. Configure this with the -c / -compact-output, -i=NUM /\n"
"-indent=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n"
"\n"
+ "The -input-json-extra-comma flag allows input like \"[1,2,]\", with a\n"
+ "comma after the final element of a JSON list or dictionary.\n"
+ "\n"
+ "The -output-json-extra-comma flag writes extra commas, regardless of\n"
+ "whether the input had it. Extra commas are non-compliant with the JSON\n"
+ "specification but many parsers accept it and it can produce simpler\n"
+ "line-based diffs. This flag is ignored when -compact-output is set.\n"
+ "\n"
"----\n"
"\n"
"The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n"
@@ -559,7 +569,9 @@
bool compact_output;
bool fail_if_unsandboxed;
size_t indent;
+ bool input_json_extra_comma;
uint32_t max_output_depth;
+ bool output_json_extra_comma;
char* query_c_string;
bool strict_json_pointer_syntax;
bool tabs;
@@ -623,6 +635,14 @@
}
return g_usage;
}
+ if (!strcmp(arg, "input-json-extra-comma")) {
+ g_flags.input_json_extra_comma = true;
+ continue;
+ }
+ if (!strcmp(arg, "output-json-extra-comma")) {
+ g_flags.output_json_extra_comma = true;
+ continue;
+ }
if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) {
while (*arg++ != '=') {
}
@@ -692,6 +712,10 @@
TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0)
.message());
+ if (g_flags.input_json_extra_comma) {
+ g_dec.set_quirk_enabled(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA, true);
+ }
+
// Consume an optional whitespace trailer. This isn't part of the JSON spec,
// but it works better with line oriented Unix tools (such as "echo 123 |
// jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
@@ -869,7 +893,11 @@
if ((g_ctx != context::in_list_after_bracket) &&
(g_ctx != context::in_dict_after_brace) &&
!g_flags.compact_output) {
- TRY(write_dst("\n", 1));
+ if (g_flags.output_json_extra_comma) {
+ TRY(write_dst(",\n", 2));
+ } else {
+ TRY(write_dst("\n", 1));
+ }
for (uint32_t i = 0; i < g_depth; i++) {
TRY(write_dst(
g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING,