mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Have example/jsonetc read and write extra commas
This commit is contained in:
1 parent
540a3b51e0
commit
c766bb72d6
2 files changed
+47
-7
No files matched your search
@@ -108,6 +108,7 @@ static const char* g_usage =
|
|||||||
"Flags:\n"
|
"Flags:\n"
|
||||||
" -d=NUM -max-output-depth=NUM\n"
|
" -d=NUM -max-output-depth=NUM\n"
|
||||||
" -s -strict-json-pointer-syntax\n"
|
" -s -strict-json-pointer-syntax\n"
|
||||||
|
" -input-json-extra-comma\n"
|
||||||
"\n"
|
"\n"
|
||||||
"The input.json filename is optional. If absent, it reads from stdin.\n"
|
"The input.json filename is optional. If absent, it reads from stdin.\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -151,6 +152,9 @@ static const char* g_usage =
|
|||||||
"This JSON implementation also rejects integer values outside ±M, where\n"
|
"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"
|
"M is ((1<<53)-1), also known as JavaScript's Number.MAX_SAFE_INTEGER.\n"
|
||||||
"\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"
|
||||||
"\n"
|
"\n"
|
||||||
"The -s or -strict-json-pointer-syntax flag restricts the output lines\n"
|
"The -s or -strict-json-pointer-syntax flag restricts the output lines\n"
|
||||||
@@ -180,6 +184,7 @@ struct {
|
|||||||
int remaining_argc;
|
int remaining_argc;
|
||||||
char** remaining_argv;
|
char** remaining_argv;
|
||||||
|
|
||||||
|
bool input_json_extra_comma;
|
||||||
uint32_t max_output_depth;
|
uint32_t max_output_depth;
|
||||||
bool strict_json_pointer_syntax;
|
bool strict_json_pointer_syntax;
|
||||||
} g_flags = {0};
|
} g_flags = {0};
|
||||||
@@ -224,6 +229,10 @@ parse_flags(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
return g_usage;
|
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")) {
|
if (!strcmp(arg, "s") || !strcmp(arg, "strict-json-pointer-syntax")) {
|
||||||
g_flags.strict_json_pointer_syntax = true;
|
g_flags.strict_json_pointer_syntax = true;
|
||||||
continue;
|
continue;
|
||||||
@@ -282,12 +291,15 @@ class TokenStream {
|
|||||||
m_status =
|
m_status =
|
||||||
m_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
|
m_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0);
|
||||||
|
|
||||||
// Uncomment these lines to enable the WUFFS_JSON__QUIRK_ALLOW_BACKSLASH_X
|
if (m_status.is_ok()) {
|
||||||
// option, discussed in a separate comment.
|
// Uncomment this line 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);
|
||||||
// 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); }
|
Result peek() { return peek_or_next(false); }
|
||||||
|
|||||||
@@ -157,6 +157,8 @@ static const char* g_usage =
|
|||||||
" -s -strict-json-pointer-syntax\n"
|
" -s -strict-json-pointer-syntax\n"
|
||||||
" -t -tabs\n"
|
" -t -tabs\n"
|
||||||
" -fail-if-unsandboxed\n"
|
" -fail-if-unsandboxed\n"
|
||||||
|
" -input-json-extra-comma\n"
|
||||||
|
" -output-json-extra-comma\n"
|
||||||
"\n"
|
"\n"
|
||||||
"The input.json filename is optional. If absent, it reads from stdin.\n"
|
"The input.json filename is optional. If absent, it reads from stdin.\n"
|
||||||
"\n"
|
"\n"
|
||||||
@@ -174,6 +176,14 @@ static const char* g_usage =
|
|||||||
"on its own line. Configure this with the -c / -compact-output, -i=NUM /\n"
|
"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"
|
"-indent=NUM (for NUM ranging from 0 to 8) and -t / -tabs flags.\n"
|
||||||
"\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"
|
||||||
"\n"
|
"\n"
|
||||||
"The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n"
|
"The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n"
|
||||||
@@ -559,7 +569,9 @@ struct {
|
|||||||
bool compact_output;
|
bool compact_output;
|
||||||
bool fail_if_unsandboxed;
|
bool fail_if_unsandboxed;
|
||||||
size_t indent;
|
size_t indent;
|
||||||
|
bool input_json_extra_comma;
|
||||||
uint32_t max_output_depth;
|
uint32_t max_output_depth;
|
||||||
|
bool output_json_extra_comma;
|
||||||
char* query_c_string;
|
char* query_c_string;
|
||||||
bool strict_json_pointer_syntax;
|
bool strict_json_pointer_syntax;
|
||||||
bool tabs;
|
bool tabs;
|
||||||
@@ -623,6 +635,14 @@ parse_flags(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
return g_usage;
|
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)) {
|
if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) {
|
||||||
while (*arg++ != '=') {
|
while (*arg++ != '=') {
|
||||||
}
|
}
|
||||||
@@ -692,6 +712,10 @@ initialize_globals(int argc, char** argv) {
|
|||||||
TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0)
|
TRY(g_dec.initialize(sizeof__wuffs_json__decoder(), WUFFS_VERSION, 0)
|
||||||
.message());
|
.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,
|
// 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 |
|
// 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
|
// jsonptr" where it's "echo", not "echo -n") or hand-edited JSON files which
|
||||||
@@ -869,7 +893,11 @@ handle_token(wuffs_base__token t, bool start_of_token_chain) {
|
|||||||
if ((g_ctx != context::in_list_after_bracket) &&
|
if ((g_ctx != context::in_list_after_bracket) &&
|
||||||
(g_ctx != context::in_dict_after_brace) &&
|
(g_ctx != context::in_dict_after_brace) &&
|
||||||
!g_flags.compact_output) {
|
!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++) {
|
for (uint32_t i = 0; i < g_depth; i++) {
|
||||||
TRY(write_dst(
|
TRY(write_dst(
|
||||||
g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING,
|
g_flags.tabs ? INDENT_TAB_STRING : INDENT_SPACES_STRING,
|
||||||
|
|||||||
Reference in new issue
Block a user