]> mj.ucw.cz Git - libucw.git/blob - ucw-json/json-test.c
Released as 6.5.16.
[libucw.git] / ucw-json / json-test.c
1 /*
2  *      UCW JSON Library -- Tests
3  *
4  *      (c) 2015 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/fastbuf.h>
12 #include <ucw/fw-hex.h>
13 #include <ucw/opt.h>
14 #include <ucw/trans.h>
15 #include <ucw-json/json.h>
16
17 static int opt_read;
18 static int opt_write;
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;
24
25 static struct opt_section options = {
26   OPT_ITEMS {
27     OPT_HELP("Test program for UCW JSON library."),
28     OPT_HELP("Usage: json-test [options]"),
29     OPT_HELP(""),
30     OPT_HELP("Options:"),
31     OPT_HELP_OPTION,
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"),
39     OPT_END
40   }
41 };
42
43 static struct json_node *do_parse(struct json_context *js, struct fastbuf *fb)
44 {
45   struct json_node *n;
46   TRANS_TRY
47     {
48       n = json_parse(js, fb);
49     }
50   TRANS_CATCH(x)
51     {
52       fprintf(stderr, "ERROR: %s\n", x->msg);
53       exit(1);
54     }
55   TRANS_END;
56   return n;
57 }
58
59 static void test_stream(struct json_context *js)
60 {
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);
65
66   for (;;)
67     {
68       json_push(js);
69       struct json_node *n = json_next_value(js);
70       if (!n)
71         break;
72       json_write_value(js, n);
73       bputc(out, '\n');
74       json_pop(js);
75     }
76
77   bclose(out);
78   bclose(in);
79 }
80
81 int main(int argc UNUSED, char **argv)
82 {
83   opt_parse(&options, argv+1);
84
85   struct json_context *js = json_new();
86   struct json_node *n = NULL;
87
88   if (opt_escape)
89     js->format_options |= JSON_FORMAT_ESCAPE_NONASCII;
90   if (opt_indent)
91     js->format_options |= JSON_FORMAT_INDENT;
92
93   if (opt_stream)
94     {
95       test_stream(js);
96       json_delete(js);
97       return 0;
98     }
99
100   if (opt_read || opt_read_hex)
101     {
102       struct fastbuf *fb = bfdopen_shared(0, 65536);
103       if (opt_read_hex)
104         fb = fb_wrap_hex_in(fb);
105       n = do_parse(js, fb);
106       bclose(fb);
107     }
108
109   if (!n)
110     n = json_new_number(js, 42);
111
112   if (opt_write || opt_write_hex)
113     {
114       struct fastbuf *fb = bfdopen_shared(1, 65536);
115       if (opt_write_hex)
116         fb = fb_wrap_hex_out(fb);
117       json_write(js, fb, n);
118       bclose(fb);
119     }
120
121   json_delete(js);
122   return 0;
123 }