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;
23 static int opt_stream;
25 static struct opt_section options = {
27 OPT_HELP("Test program for UCW JSON library."),
28 OPT_HELP("Usage: json-test [options]"),
32 OPT_BOOL('r', "read", opt_read, 0, "\tRead JSON from standard input"),
33 OPT_BOOL('R', "read-hex", opt_read_hex, 0, "\tRead JSON, interpreting <XY> as hex escapes"),
34 OPT_BOOL('w', "write", opt_write, 0, "\tWrite JSON to standard output"),
35 OPT_BOOL('W', "write-hex", opt_write_hex, 0, "\tWrite JSON, print non-ASCII as hex escapes"),
36 OPT_BOOL('e', "escape", opt_escape, 0, "\tEscape non-ASCII characters in strings"),
37 OPT_BOOL('i', "indent", opt_indent, 0, "\tIndent output"),
38 OPT_BOOL('s', "stream", opt_stream, 0, "\tTest of streaming mode"),
43 static struct json_node *do_parse(struct json_context *js, struct fastbuf *fb)
48 n = json_parse(js, fb);
52 fprintf(stderr, "ERROR: %s\n", x->msg);
59 static void test_stream(struct json_context *js)
61 struct fastbuf *in = bfdopen_shared(0, 65536);
62 struct fastbuf *out = bfdopen_shared(1, 65536);
63 json_set_input(js, in);
64 json_set_output(js, out);
69 struct json_node *n = json_next_value(js);
72 json_write_value(js, n);
81 int main(int argc UNUSED, char **argv)
83 opt_parse(&options, argv+1);
85 struct json_context *js = json_new();
86 struct json_node *n = NULL;
89 js->format_options |= JSON_FORMAT_ESCAPE_NONASCII;
91 js->format_options |= JSON_FORMAT_INDENT;
100 if (opt_read || opt_read_hex)
102 struct fastbuf *fb = bfdopen_shared(0, 65536);
104 fb = fb_wrap_hex_in(fb);
105 n = do_parse(js, fb);
110 n = json_new_number(js, 42);
112 if (opt_write || opt_write_hex)
114 struct fastbuf *fb = bfdopen_shared(1, 65536);
116 fb = fb_wrap_hex_out(fb);
117 json_write(js, fb, n);