forked from vincenthz/libjson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonlint.c
560 lines (496 loc) · 13.7 KB
/
jsonlint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Copyright (C) 2009-2011 Vincent Hanquez <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 or version 3.0 only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <locale.h>
#include <getopt.h>
#include <errno.h>
#include "json.h"
char *indent_string = NULL;
char *string_of_errors[] =
{
[JSON_ERROR_NO_MEMORY] = "out of memory",
[JSON_ERROR_BAD_CHAR] = "bad character",
[JSON_ERROR_POP_EMPTY] = "stack empty",
[JSON_ERROR_POP_UNEXPECTED_MODE] = "pop unexpected mode",
[JSON_ERROR_NESTING_LIMIT] = "nesting limit",
[JSON_ERROR_DATA_LIMIT] = "data limit",
[JSON_ERROR_COMMENT_NOT_ALLOWED] = "comment not allowed by config",
[JSON_ERROR_UNEXPECTED_CHAR] = "unexpected char",
[JSON_ERROR_UNICODE_MISSING_LOW_SURROGATE] = "missing unicode low surrogate",
[JSON_ERROR_UNICODE_UNEXPECTED_LOW_SURROGATE] = "unexpected unicode low surrogate",
[JSON_ERROR_COMMA_OUT_OF_STRUCTURE] = "error comma out of structure",
[JSON_ERROR_CALLBACK] = "error in a callback",
[JSON_ERROR_UTF8] = "utf8 validation error"
};
static int printchannel(void *userdata, const char *data, uint32_t length)
{
FILE *channel = userdata;
int ret;
/* should check return value */
ret = fwrite(data, length, 1, channel);
return 0;
}
static int prettyprint(void *userdata, int type, const char *data, uint32_t length)
{
json_printer *printer = userdata;
return json_print_pretty(printer, type, data, length);
}
FILE *open_filename(const char *filename, const char *opt, int is_input)
{
FILE *input;
if (strcmp(filename, "-") == 0)
input = (is_input) ? stdin : stdout;
else {
input = fopen(filename, opt);
if (!input) {
fprintf(stderr, "error: cannot open %s: %s", filename, strerror(errno));
return NULL;
}
}
return input;
}
void close_filename(const char *filename, FILE *file)
{
if (strcmp(filename, "-") != 0)
fclose(file);
}
int process_file(json_parser *parser, FILE *input, int *retlines, int *retcols)
{
char buffer[4096];
int ret = 0;
int32_t read;
int lines, col, i;
lines = 1;
col = 0;
while (1) {
uint32_t processed;
read = fread(buffer, 1, 4096, input);
if (read <= 0)
break;
ret = json_parser_string(parser, buffer, read, &processed);
for (i = 0; i < processed; i++) {
if (buffer[i] == '\n') { col = 0; lines++; } else col++;
}
if (ret)
break;
}
if (retlines) *retlines = lines;
if (retcols) *retcols = col;
return ret;
}
static int do_verify(json_config *config, const char *filename)
{
FILE *input;
json_parser parser;
int ret;
input = open_filename(filename, "r", 1);
if (!input)
return 2;
/* initialize the parser structure. we don't need a callback in verify */
ret = json_parser_init(&parser, config, NULL, NULL);
if (ret) {
fprintf(stderr, "error: initializing parser failed (code=%d): %s\n", ret, string_of_errors[ret]);
return ret;
}
ret = process_file(&parser, input, NULL, NULL);
if (ret)
return 1;
ret = json_parser_is_done(&parser);
if (!ret)
return 1;
close_filename(filename, input);
return 0;
}
static int do_parse(json_config *config, const char *filename)
{
FILE *input;
json_parser parser;
int ret;
int col, lines;
input = open_filename(filename, "r", 1);
if (!input)
return 2;
/* initialize the parser structure. we don't need a callback in verify */
ret = json_parser_init(&parser, config, NULL, NULL);
if (ret) {
fprintf(stderr, "error: initializing parser failed (code=%d): %s\n", ret, string_of_errors[ret]);
return ret;
}
ret = process_file(&parser, input, &lines, &col);
if (ret) {
fprintf(stderr, "line %d, col %d: [code=%d] %s\n",
lines, col, ret, string_of_errors[ret]);
return 1;
}
ret = json_parser_is_done(&parser);
if (!ret) {
fprintf(stderr, "syntax error\n");
return 1;
}
close_filename(filename, input);
return 0;
}
static int do_format(json_config *config, const char *filename, const char *outputfile)
{
FILE *input, *output;
json_parser parser;
json_printer printer;
int ret;
int col, lines;
input = open_filename(filename, "r", 1);
if (!input)
return 2;
output = open_filename(outputfile, "a+", 0);
if (!output)
return 2;
/* initialize printer and parser structures */
ret = json_print_init(&printer, printchannel, stdout);
if (ret) {
fprintf(stderr, "error: initializing printer failed: [code=%d] %s\n", ret, string_of_errors[ret]);
return ret;
}
if (indent_string)
printer.indentstr = indent_string;
ret = json_parser_init(&parser, config, &prettyprint, &printer);
if (ret) {
fprintf(stderr, "error: initializing parser failed: [code=%d] %s\n", ret, string_of_errors[ret]);
return ret;
}
ret = process_file(&parser, input, &lines, &col);
if (ret) {
fprintf(stderr, "line %d, col %d: [code=%d] %s\n",
lines, col, ret, string_of_errors[ret]);
return 1;
}
ret = json_parser_is_done(&parser);
if (!ret) {
fprintf(stderr, "syntax error\n");
return 1;
}
/* cleanup */
json_parser_free(&parser);
json_print_free(&printer);
fwrite("\n", 1, 1, stdout);
close_filename(filename, input);
return 0;
}
struct json_val_elem {
char *key;
uint32_t key_length;
struct json_val *val;
};
typedef struct json_val {
int type;
int length;
union {
char *data;
struct json_val **array;
struct json_val_elem **object;
} u;
} json_val_t;
static void *tree_create_structure(int nesting, int is_object)
{
json_val_t *v = malloc(sizeof(json_val_t));
if (v) {
/* instead of defining a new enum type, we abuse the
* meaning of the json enum type for array and object */
if (is_object) {
v->type = JSON_OBJECT_BEGIN;
v->u.object = NULL;
} else {
v->type = JSON_ARRAY_BEGIN;
v->u.array = NULL;
}
v->length = 0;
}
return v;
}
static char *memalloc_copy_length(const char *src, uint32_t n)
{
char *dest;
dest = calloc(n + 1, sizeof(char));
if (dest)
memcpy(dest, src, n);
return dest;
}
static void *tree_create_data(int type, const char *data, uint32_t length)
{
json_val_t *v;
v = malloc(sizeof(json_val_t));
if (v) {
v->type = type;
v->length = length;
v->u.data = memalloc_copy_length(data, length);
if (!v->u.data) {
free(v);
return NULL;
}
}
return v;
}
static int tree_append(void *structure, char *key, uint32_t key_length, void *obj)
{
json_val_t *parent = structure;
if (key) {
struct json_val_elem *objelem;
if (parent->length == 0) {
parent->u.object = calloc(1 + 1, sizeof(json_val_t *)); /* +1 for null */
if (!parent->u.object)
return 1;
} else {
uint32_t newsize = parent->length + 1 + 1; /* +1 for null */
void *newptr;
newptr = realloc(parent->u.object, newsize * sizeof(json_val_t *));
if (!newptr)
return -1;
parent->u.object = newptr;
}
objelem = malloc(sizeof(struct json_val_elem));
if (!objelem)
return -1;
objelem->key = memalloc_copy_length(key, key_length);
objelem->key_length = key_length;
objelem->val = obj;
parent->u.object[parent->length++] = objelem;
parent->u.object[parent->length] = NULL;
} else {
if (parent->length == 0) {
parent->u.array = calloc(1 + 1, sizeof(json_val_t *)); /* +1 for null */
if (!parent->u.array)
return 1;
} else {
uint32_t newsize = parent->length + 1 + 1; /* +1 for null */
void *newptr;
newptr = realloc(parent->u.object, newsize * sizeof(json_val_t *));
if (!newptr)
return -1;
parent->u.array = newptr;
}
parent->u.array[parent->length++] = obj;
parent->u.array[parent->length] = NULL;
}
return 0;
}
static int do_tree(json_config *config, const char *filename, json_val_t **root_structure)
{
FILE *input;
json_parser parser;
json_parser_dom dom;
int ret;
int col, lines;
input = open_filename(filename, "r", 1);
if (!input)
return 2;
ret = json_parser_dom_init(&dom, tree_create_structure, tree_create_data, tree_append);
if (ret) {
fprintf(stderr, "error: initializing helper failed: [code=%d] %s\n", ret, string_of_errors[ret]);
return ret;
}
ret = json_parser_init(&parser, config, json_parser_dom_callback, &dom);
if (ret) {
fprintf(stderr, "error: initializing parser failed: [code=%d] %s\n", ret, string_of_errors[ret]);
return ret;
}
ret = process_file(&parser, input, &lines, &col);
if (ret) {
fprintf(stderr, "line %d, col %d: [code=%d] %s\n",
lines, col, ret, string_of_errors[ret]);
return 1;
}
ret = json_parser_is_done(&parser);
if (!ret) {
fprintf(stderr, "syntax error\n");
return 1;
}
if (root_structure)
*root_structure = dom.root_structure;
/* cleanup */
json_parser_free(&parser);
close_filename(filename, input);
return 0;
}
static int print_tree_iter(json_val_t *element, FILE *output)
{
int i;
if (!element) {
fprintf(stderr, "error: no element in print tree\n");
return -1;
}
switch (element->type) {
case JSON_OBJECT_BEGIN:
fprintf(output, "object begin (%d element)\n", element->length);
for (i = 0; i < element->length; i++) {
fprintf(output, "key: %s\n", element->u.object[i]->key);
print_tree_iter(element->u.object[i]->val, output);
}
fprintf(output, "object end\n");
break;
case JSON_ARRAY_BEGIN:
fprintf(output, "array begin\n");
for (i = 0; i < element->length; i++) {
print_tree_iter(element->u.array[i], output);
}
fprintf(output, "array end\n");
break;
case JSON_FALSE:
case JSON_TRUE:
case JSON_NULL:
fprintf(output, "constant\n");
break;
case JSON_INT:
fprintf(output, "integer: %s\n", element->u.data);
break;
case JSON_STRING:
fprintf(output, "string: %s\n", element->u.data);
break;
case JSON_FLOAT:
fprintf(output, "float: %s\n", element->u.data);
break;
default:
break;
}
return 0;
}
static int print_tree(json_val_t *root_structure, char *outputfile)
{
FILE *output;
output = open_filename(outputfile, "a+", 0);
if (!output)
return 2;
print_tree_iter(root_structure, output);
close_filename(outputfile, output);
return 0;
}
int usage(const char *argv0)
{
printf("usage: %s [options] JSON-FILE(s)...\n", argv0);
printf("\t--no-comments : disallow C and YAML comments in json file (default to both on)\n");
printf("\t--no-yaml-comments : disallow YAML comment (default to on)\n");
printf("\t--no-c-comments : disallow C comment (default to on)\n");
printf("\t--format : pretty print the json file to stdout (unless -o specified)\n");
printf("\t--verify : quietly verified if the json file is valid. exit 0 if valid, 1 if not\n");
printf("\t--benchmark : quietly iterate multiples times over valid json files\n");
printf("\t--max-nesting : limit the number of nesting in structure (default to no limit)\n");
printf("\t--max-data : limit the number of characters of data (string/int/float) (default to no limit)\n");
printf("\t--indent-string : set the string to use for indenting one level (default to 1 tab)\n");
printf("\t--tree : build a tree (DOM)\n");
printf("\t-o : output to a specific file instead of stdout\n");
exit(0);
}
int main(int argc, char **argv)
{
int format = 0, verify = 0, use_tree = 0, benchmarks = 0;
int ret = 0, i;
json_config config;
char *output = "-";
memset(&config, 0, sizeof(json_config));
config.max_nesting = 0;
config.max_data = 0;
config.allow_c_comments = 1;
config.allow_yaml_comments = 1;
while (1) {
int option_index;
struct option long_options[] = {
{ "no-comments", 0, 0, 0 },
{ "no-yaml-comments", 0, 0, 0 },
{ "no-c-comments", 0, 0, 0 },
{ "format", 0, 0, 0 },
{ "verify", 0, 0, 0 },
{ "benchmark", 1, 0, 0 },
{ "help", 0, 0, 0 },
{ "max-nesting", 1, 0, 0 },
{ "max-data", 1, 0, 0 },
{ "indent-string", 1, 0, 0 },
{ "tree", 0, 0, 0 },
{ 0 },
};
int c = getopt_long(argc, argv, "o:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0: {
const char *name = long_options[option_index].name;
if (strcmp(name, "help") == 0)
usage(argv[0]);
else if (strcmp(name, "no-c-comments") == 0)
config.allow_c_comments = 0;
else if (strcmp(name, "no-yaml-comments") == 0)
config.allow_yaml_comments = 0;
else if (strcmp(name, "no-comments") == 0)
config.allow_c_comments = config.allow_yaml_comments = 0;
else if (strcmp(name, "format") == 0)
format = 1;
else if (strcmp(name, "verify") == 0)
verify = 1;
else if (strcmp(name, "max-nesting") == 0)
config.max_nesting = atoi(optarg);
else if (strcmp(name, "benchmark") == 0)
benchmarks = atoi(optarg);
else if (strcmp(name, "max-data") == 0)
config.max_data = atoi(optarg);
else if (strcmp(name, "indent-string") == 0)
indent_string = strdup(optarg);
else if (strcmp(name, "tree") == 0)
use_tree = 1;
break;
}
case 'o':
output = strdup(optarg);
break;
default:
break;
}
}
if (config.max_nesting < 0)
config.max_nesting = 0;
if (!output)
output = "-";
if (optind >= argc)
usage(argv[0]);
if (benchmarks > 0) {
for (i = 0; i < benchmarks; i++) {
if (use_tree) {
json_val_t *root_structure;
ret = do_tree(&config, argv[optind], &root_structure);
} else {
ret = do_verify(&config, argv[optind]);
}
if (ret)
exit(ret);
}
exit(0);
}
for (i = optind; i < argc; i++) {
if (use_tree) {
json_val_t *root_structure;
ret = do_tree(&config, argv[i], &root_structure);
if (ret)
exit(ret);
if (!verify)
print_tree(root_structure, output);
} else {
if (format)
ret = do_format(&config, argv[i], output);
else if (verify)
ret = do_verify(&config, argv[i]);
else
ret = do_parse(&config, argv[i]);
}
if (ret)
exit(ret);
}
return ret;
}