]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Make json_parse a bit more useful by adding -u (validate UTF8) and -P (specify arbitr... master
authorEric Hawicz <erh+git@nimenees.com>
Wed, 9 Jul 2025 01:02:52 +0000 (21:02 -0400)
committerEric Hawicz <erh+git@nimenees.com>
Wed, 9 Jul 2025 01:04:00 +0000 (21:04 -0400)
apps/json_parse.c

index 4eb629b154a63576c15e1f2a7aa5b142f6121cd8..c6c7728330e90946e912767acbc559d5a84ca121 100644 (file)
@@ -38,6 +38,8 @@
 static int formatted_output = JSON_C_TO_STRING_SPACED;
 static int show_output = 1;
 static int strict_mode = 0;
+static int validate_utf8 = 0;
+static int tokener_flags = 0;
 static int color = 0;
 static const char *fname = NULL;
 
@@ -76,11 +78,18 @@ static int parseit(int fd, int (*callback)(struct json_object *))
        }
        if (strict_mode)
        {
-               json_tokener_set_flags(tok, JSON_TOKENER_STRICT
-       #ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
-                        | JSON_TOKENER_ALLOW_TRAILING_CHARS
+               tokener_flags |= JSON_TOKENER_STRICT;
+#ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
+               tokener_flags |= JSON_TOKENER_ALLOW_TRAILING_CHARS;
        #endif
-               );
+       }
+       if (validate_utf8)
+       {
+               tokener_flags |= JSON_TOKENER_VALIDATE_UTF8;
+       }
+       if (tokener_flags)
+       {
+               json_tokener_set_flags(tok, tokener_flags);
        }
 
        // XXX push this into some kind of json_tokener_parse_fd API?
@@ -160,13 +169,15 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
                fp = stderr;
        if (errmsg != NULL)
                fprintf(fp, "ERROR: %s\n\n", errmsg);
-       fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s]\n", argv0);
+       fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [filename]\n", argv0);
        fprintf(fp, "  -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
        fprintf(fp, "  -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
        fprintf(fp, "  -n - No output\n");
-       fprintf(fp, "  -c - color\n");
-       fprintf(fp, "  -s - Parse in strict mode, flags:\n");
+       fprintf(fp, "  -c - Set JSON_C_TO_STRING_COLOR to colorize the output\n");
+       fprintf(fp, "  -P - Initialize tokener flags to the given value\n");
+       fprintf(fp, "  -s - Parse in strict mode, add flags:\n");
        fprintf(fp, "       JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
+       fprintf(fp, "  -u - Add the JSON_TOKENER_VALIDATE_UTF8 flag when parsing\n");
        fprintf(fp, " Diagnostic information will be emitted to stderr\n");
 
        fprintf(fp, "\nWARNING WARNING WARNING\n");
@@ -178,26 +189,28 @@ int main(int argc, char **argv)
 {
        int opt;
 
-       while ((opt = getopt(argc, argv, "fF:hnsc")) != -1)
+       while ((opt = getopt(argc, argv, "cfF:hnP:su")) != -1)
        {
                switch (opt)
                {
+               case 'c': color = JSON_C_TO_STRING_COLOR; break;
                case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
                case 'F': formatted_output = atoi(optarg); break;
                case 'n': show_output = 0; break;
+               case 'P': tokener_flags = atoi(optarg); break;
                case 's': strict_mode = 1; break;
-               case 'c': color = JSON_C_TO_STRING_COLOR; break;
+               case 'u': validate_utf8 = 1; break;
                case 'h': usage(argv[0], 0, NULL);
                default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
                }
        }
-       if (optind >= argc)
+       int fd = STDIN_FILENO;
+       fname = "stdin";
+       if (argc > optind && strcmp(argv[optind], "-") != 0)
        {
-               usage(argv[0], EXIT_FAILURE, "Expected argument after options");
+               fname = argv[optind];
+               fd = open(fname, O_RDONLY, 0);
        }
-       fname = argv[optind];
-
-       int fd = open(argv[optind], O_RDONLY, 0);
        showmem();
        if (parseit(fd, showobj) != 0)
                exit(EXIT_FAILURE);