2 * UCW JSON Library -- Tests
4 * (c) 2015 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/fastbuf.h>
12 #include <ucw/fw-hex.h>
14 #include <ucw/trans.h>
15 #include <ucw-json/json.h>
19 static int opt_escape;
20 static int opt_indent;
21 static int opt_read_hex;
22 static int opt_write_hex;
24 static struct opt_section options = {
26 OPT_HELP("Test program for UCW JSON library."),
27 OPT_HELP("Usage: json-test [options]"),
31 OPT_BOOL('r', "read", opt_read, 0, "\tRead JSON from standard input"),
32 OPT_BOOL('R', "read-hex", opt_read_hex, 0, "\tRead JSON, interpreting <XY> as hex escapes"),
33 OPT_BOOL('w', "write", opt_write, 0, "\tWrite JSON to standard output"),
34 OPT_BOOL('W', "write-hex", opt_write_hex, 0, "\tWrite JSON, print non-ASCII as hex escapes"),
35 OPT_BOOL('e', "escape", opt_escape, 0, "\tEscape non-ASCII characters in strings"),
36 OPT_BOOL('i', "indent", opt_indent, 0, "\tIndent output"),
41 static struct json_node *do_parse(struct json_context *js, struct fastbuf *fb)
46 n = json_parse(js, fb);
50 fprintf(stderr, "ERROR: %s\n", x->msg);
57 int main(int argc UNUSED, char **argv)
59 opt_parse(&options, argv+1);
61 struct json_context *js = json_new();
62 struct json_node *n = NULL;
65 js->format_options |= JSON_FORMAT_ESCAPE_NONASCII;
67 js->format_options |= JSON_FORMAT_INDENT;
69 if (opt_read || opt_read_hex)
71 struct fastbuf *fb = bfdopen_shared(0, 65536);
73 fb = fb_wrap_hex_in(fb);
79 n = json_new_number(js, 42);
81 if (opt_write || opt_write_hex)
83 struct fastbuf *fb = bfdopen_shared(1, 65536);
85 fb = fb_wrap_hex_out(fb);
86 json_write(js, fb, n);