Skip to content

Commit bf92456

Browse files
committed
Make json_parse a bit more useful by adding -u (validate UTF8) and -P (specify arbitrary tokener parse flags), and read from stdin if no filename is provided.
1 parent 7cee523 commit bf92456

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

apps/json_parse.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
static int formatted_output = JSON_C_TO_STRING_SPACED;
3939
static int show_output = 1;
4040
static int strict_mode = 0;
41+
static int validate_utf8 = 0;
42+
static int tokener_flags = 0;
4143
static int color = 0;
4244
static const char *fname = NULL;
4345

@@ -76,11 +78,18 @@ static int parseit(int fd, int (*callback)(struct json_object *))
7678
}
7779
if (strict_mode)
7880
{
79-
json_tokener_set_flags(tok, JSON_TOKENER_STRICT
80-
#ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
81-
| JSON_TOKENER_ALLOW_TRAILING_CHARS
81+
tokener_flags |= JSON_TOKENER_STRICT;
82+
#ifdef JSON_TOKENER_ALLOW_TRAILING_CHARS
83+
tokener_flags |= JSON_TOKENER_ALLOW_TRAILING_CHARS;
8284
#endif
83-
);
85+
}
86+
if (validate_utf8)
87+
{
88+
tokener_flags |= JSON_TOKENER_VALIDATE_UTF8;
89+
}
90+
if (tokener_flags)
91+
{
92+
json_tokener_set_flags(tok, tokener_flags);
8493
}
8594

8695
// 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)
160169
fp = stderr;
161170
if (errmsg != NULL)
162171
fprintf(fp, "ERROR: %s\n\n", errmsg);
163-
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s]\n", argv0);
172+
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [filename]\n", argv0);
164173
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
165174
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
166175
fprintf(fp, " -n - No output\n");
167-
fprintf(fp, " -c - color\n");
168-
fprintf(fp, " -s - Parse in strict mode, flags:\n");
176+
fprintf(fp, " -c - Set JSON_C_TO_STRING_COLOR to colorize the output\n");
177+
fprintf(fp, " -P - Initialize tokener flags to the given value\n");
178+
fprintf(fp, " -s - Parse in strict mode, add flags:\n");
169179
fprintf(fp, " JSON_TOKENER_STRICT|JSON_TOKENER_ALLOW_TRAILING_CHARS\n");
180+
fprintf(fp, " -u - Add the JSON_TOKENER_VALIDATE_UTF8 flag when parsing\n");
170181
fprintf(fp, " Diagnostic information will be emitted to stderr\n");
171182

172183
fprintf(fp, "\nWARNING WARNING WARNING\n");
@@ -178,26 +189,28 @@ int main(int argc, char **argv)
178189
{
179190
int opt;
180191

181-
while ((opt = getopt(argc, argv, "fF:hnsc")) != -1)
192+
while ((opt = getopt(argc, argv, "cfF:hnP:su")) != -1)
182193
{
183194
switch (opt)
184195
{
196+
case 'c': color = JSON_C_TO_STRING_COLOR; break;
185197
case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
186198
case 'F': formatted_output = atoi(optarg); break;
187199
case 'n': show_output = 0; break;
200+
case 'P': tokener_flags = atoi(optarg); break;
188201
case 's': strict_mode = 1; break;
189-
case 'c': color = JSON_C_TO_STRING_COLOR; break;
202+
case 'u': validate_utf8 = 1; break;
190203
case 'h': usage(argv[0], 0, NULL);
191204
default: /* '?' */ usage(argv[0], EXIT_FAILURE, "Unknown arguments");
192205
}
193206
}
194-
if (optind >= argc)
207+
int fd = STDIN_FILENO;
208+
fname = "stdin";
209+
if (argc > optind && strcmp(argv[optind], "-") != 0)
195210
{
196-
usage(argv[0], EXIT_FAILURE, "Expected argument after options");
211+
fname = argv[optind];
212+
fd = open(fname, O_RDONLY, 0);
197213
}
198-
fname = argv[optind];
199-
200-
int fd = open(argv[optind], O_RDONLY, 0);
201214
showmem();
202215
if (parseit(fd, showobj) != 0)
203216
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)