2 * Copyright 2007-2009, Lloyd Hilaiel.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. Neither the name of Lloyd Hilaiel nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <yajl/yajl_parse.h>
40 usage(const char * progname
)
42 fprintf(stderr
, "%s: validate json from stdin\n"
43 "usage: json_verify [options]\n"
45 " -c allow comments\n"
46 " -u allow invalid utf8 inside strings\n",
52 main(int argc
, char ** argv
)
57 static unsigned char fileData
[65536];
59 int retval
= 0, done
= 0;
60 yajl_parser_config cfg
= { 0, 1 };
63 if (argc
> 1 && argc
< 4) {
66 for (i
=1; i
< argc
;i
++) {
67 if (!strcmp("-q", argv
[i
])) {
69 } else if (!strcmp("-c", argv
[i
])) {
70 cfg
.allowComments
= 1;
71 } else if (!strcmp("-u", argv
[i
])) {
74 fprintf(stderr
, "unrecognized option: '%s'\n\n", argv
[i
]);
78 } else if (argc
!= 1) {
82 /* allocate a parser */
83 hand
= yajl_alloc(NULL
, &cfg
, NULL
, NULL
);
86 rd
= fread((void *) fileData
, 1, sizeof(fileData
) - 1, stdin
);
93 fprintf(stderr
, "error encountered on file read\n");
103 /* parse any remaining buffered data */
104 stat
= yajl_parse_complete(hand
);
106 /* read file data, pass to parser */
107 stat
= yajl_parse(hand
, fileData
, rd
);
109 if (stat
!= yajl_status_ok
&&
110 stat
!= yajl_status_insufficient_data
)
113 unsigned char * str
= yajl_get_error(hand
, 1, fileData
, rd
);
114 fprintf(stderr
, (const char *) str
);
115 yajl_free_error(hand
, str
);
125 printf("JSON is %s\n", retval
? "invalid" : "valid");