From 041267957c9048dc8e5fd28846d2fc32f0fcebd4 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 22 Feb 2021 12:23:57 +1100 Subject: [PATCH] Add example/json* -input-jwcc and -jwcc flags --- example/json-to-cbor/json-to-cbor.cc | 24 ++++++++++++++++++++++++ example/jsonfindptrs/jsonfindptrs.cc | 24 ++++++++++++++++++++++++ example/jsonptr/jsonptr.cc | 21 ++++++++++++++++++++- 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/example/json-to-cbor/json-to-cbor.cc b/example/json-to-cbor/json-to-cbor.cc index 3c533220..391190a3 100644 --- a/example/json-to-cbor/json-to-cbor.cc +++ b/example/json-to-cbor/json-to-cbor.cc @@ -79,6 +79,8 @@ static const char* g_usage = " -input-allow-comments\n" " -input-allow-extra-comma\n" " -input-allow-inf-nan-numbers\n" + " -input-jwcc\n" + " -jwcc\n" "\n" "The input.json filename is optional. If absent, it reads from stdin.\n" "\n" @@ -107,6 +109,16 @@ static const char* g_usage = "The -input-allow-inf-nan-numbers flag allows non-finite floating point\n" "numbers (infinities and not-a-numbers) within JSON input.\n" "\n" + "Combining some of those flags results in speaking JWCC (JSON With Commas\n" + "and Comments), not plain JSON. For convenience, the -input-jwcc or -jwcc\n" + "flags enables all of:\n" + " -input-allow-comments\n" + " -input-allow-extra-comma\n" + "\n" +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + "This program was configured at compile time to always use -jwcc.\n" + "\n" +#endif "----\n" "\n" "The JSON specification permits implementations to set their own maximum\n" @@ -130,6 +142,12 @@ struct { std::string // parse_flags(int argc, char** argv) { +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA); +#endif + int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. for (; c < argc; c++) { char* arg = argv[c]; @@ -163,6 +181,12 @@ parse_flags(int argc, char** argv) { g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS); continue; } + if (!strcmp(arg, "input-jwcc") || !strcmp(arg, "jwcc")) { + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA); + continue; + } return g_usage; } diff --git a/example/jsonfindptrs/jsonfindptrs.cc b/example/jsonfindptrs/jsonfindptrs.cc index 332883c4..28edc239 100644 --- a/example/jsonfindptrs/jsonfindptrs.cc +++ b/example/jsonfindptrs/jsonfindptrs.cc @@ -113,6 +113,8 @@ static const char* g_usage = " -input-allow-comments\n" " -input-allow-extra-comma\n" " -input-allow-inf-nan-numbers\n" + " -input-jwcc\n" + " -jwcc\n" " -only-parse-dont-output\n" " -strict-json-pointer-syntax\n" "\n" @@ -167,6 +169,16 @@ static const char* g_usage = "The -input-allow-inf-nan-numbers flag allows non-finite floating point\n" "numbers (infinities and not-a-numbers) within JSON input.\n" "\n" + "Combining some of those flags results in speaking JWCC (JSON With Commas\n" + "and Comments), not plain JSON. For convenience, the -input-jwcc or -jwcc\n" + "flags enables all of:\n" + " -input-allow-comments\n" + " -input-allow-extra-comma\n" + "\n" +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + "This program was configured at compile time to always use -jwcc.\n" + "\n" +#endif "----\n" "\n" "The -only-parse-dont-output flag means to write nothing to stdout. An\n" @@ -220,6 +232,12 @@ std::string // parse_flags(int argc, char** argv) { g_flags.max_output_depth = 0xFFFFFFFF; +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA); +#endif + int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. for (; c < argc; c++) { char* arg = argv[c]; @@ -269,6 +287,12 @@ parse_flags(int argc, char** argv) { g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_INF_NAN_NUMBERS); continue; } + if (!strcmp(arg, "input-jwcc") || !strcmp(arg, "jwcc")) { + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_BLOCK); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_COMMENT_LINE); + g_quirks.push_back(WUFFS_JSON__QUIRK_ALLOW_EXTRA_COMMA); + continue; + } if (!strncmp(arg, "q=", 2) || !strncmp(arg, "query=", 6)) { while (*arg++ != '=') { } diff --git a/example/jsonptr/jsonptr.cc b/example/jsonptr/jsonptr.cc index eb8d5652..6e3251ff 100644 --- a/example/jsonptr/jsonptr.cc +++ b/example/jsonptr/jsonptr.cc @@ -161,6 +161,7 @@ static const char* g_usage = " -input-allow-comments\n" " -input-allow-extra-comma\n" " -input-allow-inf-nan-numbers\n" + " -input-jwcc\n" " -jwcc\n" " -output-comments\n" " -output-extra-comma\n" @@ -209,12 +210,18 @@ static const char* g_usage = "and they can produce simpler line-based diffs. This flag is ignored when\n" "-compact-output is set.\n" "\n" - "The -jwcc flag (JSON With Commas and Comments) enables all of:\n" + "Combining some of those flags results in speaking JWCC (JSON With Commas\n" + "and Comments), not plain JSON. For convenience, the -input-jwcc or -jwcc\n" + "flags enables the first two or all four of:\n" " -input-allow-comments\n" " -input-allow-extra-comma\n" " -output-comments\n" " -output-extra-comma\n" "\n" +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + "This program was configured at compile time to always use -jwcc.\n" + "\n" +#endif "----\n" "\n" "The -q=STR or -query=STR flag gives an optional JSON Pointer query, to\n" @@ -775,6 +782,13 @@ parse_flags(int argc, char** argv) { g_flags.spaces = 4; g_flags.max_output_depth = 0xFFFFFFFF; +#if defined(WUFFS_EXAMPLE_SPEAK_JWCC_NOT_JSON) + g_flags.input_allow_comments = true; + g_flags.input_allow_extra_comma = true; + g_flags.output_comments = true; + g_flags.output_extra_comma = true; +#endif + int c = (argc > 0) ? 1 : 0; // Skip argv[0], the program name. for (; c < argc; c++) { char* arg = argv[c]; @@ -831,6 +845,11 @@ parse_flags(int argc, char** argv) { g_flags.input_allow_inf_nan_numbers = true; continue; } + if (!strcmp(arg, "input-jwcc")) { + g_flags.input_allow_comments = true; + g_flags.input_allow_extra_comma = true; + continue; + } if (!strcmp(arg, "jwcc")) { g_flags.input_allow_comments = true; g_flags.input_allow_extra_comma = true;