1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief JSON test
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <yaz/test.h>
14 #include <yaz/json.h>
15 #include <string.h>
16 #include <yaz/log.h>
17 
expect(json_parser_t p,const char * input,const char * output)18 static int expect(json_parser_t p, const char *input,
19                   const char *output)
20 {
21     int ret = 0;
22     struct json_node *n;
23 
24     n = json_parser_parse(p, input);
25     if (n == 0 && output == 0)
26         ret = 1;
27     else if (n && output)
28     {
29         WRBUF result = wrbuf_alloc();
30 
31         json_write_wrbuf(n, result);
32         if (strcmp(wrbuf_cstr(result), output) == 0)
33             ret = 1;
34         else
35         {
36             yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
37                     output, wrbuf_cstr(result));
38         }
39         wrbuf_destroy(result);
40     }
41     else if (!n)
42     {
43         yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
44                 output, json_parser_get_errmsg(p));
45     }
46     json_remove_node(n);
47     return ret;
48 }
49 
tst1(void)50 static void tst1(void)
51 {
52     json_parser_t p = json_parser_create();
53 
54     YAZ_CHECK(p);
55     if (!p)
56         return;
57 
58     YAZ_CHECK(expect(p, "", 0));
59 
60     YAZ_CHECK(expect(p, "1234", "1234"));
61 
62     YAZ_CHECK(expect(p, "\"\\a\"", 0));
63 
64     YAZ_CHECK(expect(p, "\"\\u0061\"", "\"a\""));
65 
66     YAZ_CHECK(expect(p, "\"\\u61\"", 0));
67 
68     YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));
69 
70     YAZ_CHECK(expect(p, "[ -12e2 ]", "[-1200]"));
71 
72     YAZ_CHECK(expect(p, "[ 12.34e2 ]", "[1234]"));
73 
74     YAZ_CHECK(expect(p, "[ 12.34e+2 ]", "[1234]"));
75 
76     YAZ_CHECK(expect(p, "[ 12.34E+2 ]", "[1234]"));
77 
78     YAZ_CHECK(expect(p, "[ .12 ]", 0));
79 
80     YAZ_CHECK(expect(p, "[ 01 ]", 0));
81 
82     YAZ_CHECK(expect(p, "[ -01 ]", 0));
83 
84     YAZ_CHECK(expect(p, "[ +7 ]", 0));
85 
86     YAZ_CHECK(expect(p, "[ 7. ]", 0));
87 
88     YAZ_CHECK(expect(p, "[ fals ]", 0));
89 
90     YAZ_CHECK(expect(p, "{\"k\":tru}", 0));
91 
92     YAZ_CHECK(expect(p, "{\"k\":null", 0));
93 
94     YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));
95 
96     YAZ_CHECK(expect(p, "{\"k\":-", 0));
97 
98     YAZ_CHECK(expect(p, "{\"k\":+", 0));
99 
100     YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));
101 
102     YAZ_CHECK(expect(p, "{\"k\":\"a", 0));
103 
104     YAZ_CHECK(expect(p, "{\"k\":\"", 0));
105 
106     YAZ_CHECK(expect(p, "{", 0));
107 
108     YAZ_CHECK(expect(p, "{}", "{}"));
109 
110     YAZ_CHECK(expect(p, "{}  extra", 0));
111 
112     YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
113 
114     YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));
115 
116     YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));
117 
118     YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));
119 
120     YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));
121 
122     YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));
123 
124     YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));
125 
126     YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));
127 
128     YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));
129 
130     YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));
131 
132     YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));
133 
134     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));
135 
136     YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
137                      "{\"a\":1,\"b\":2,\"c\":3}"));
138 
139     YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));
140 
141     YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));
142 
143     YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));
144 
145     YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));
146 
147     YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\\t\"}"));
148     YAZ_CHECK(expect(p, "{\"k\":\"\t\"}", 0));
149 
150     YAZ_CHECK(expect(p, "{\"k\":\"\\n\"}", "{\"k\":\"\\n\"}"));
151     YAZ_CHECK(expect(p, "{\"k\":\"\n\"}", 0));
152 
153     YAZ_CHECK(expect(p, "{\"k\":\"\\r\"}", "{\"k\":\"\\r\"}"));
154     YAZ_CHECK(expect(p, "{\"k\":\"\r\"}", 0));
155 
156     YAZ_CHECK(expect(p, "{\"k\":\"\\f\"}", "{\"k\":\"\\f\"}"));
157     YAZ_CHECK(expect(p, "{\"k\":\"\f\"}", 0));
158 
159     YAZ_CHECK(expect(p, "{\"k\":\"\\b\"}", "{\"k\":\"\\b\"}"));
160     YAZ_CHECK(expect(p, "{\"k\":\"\b\"}", 0));
161 
162     YAZ_CHECK(expect(p,
163                      "{\"k\":\"\\u0001\\u0002\"}",
164                      "{\"k\":\"\\u0001\\u0002\"}"));
165 
166     json_parser_destroy(p);
167 }
168 
tst2(void)169 static void tst2(void)
170 {
171     struct json_node *n, *n1;
172 
173     n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
174     YAZ_CHECK(n);
175     if (!n)
176         return;
177 
178     YAZ_CHECK_EQ(json_count_children(n), 3);
179 
180     n1 = json_get_object(n, "a");
181     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
182     YAZ_CHECK_EQ(json_count_children(n1), 0);
183 
184     n1 = json_get_object(n, "b");
185     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
186     YAZ_CHECK_EQ(json_count_children(n1), 0);
187 
188     n1 = json_get_object(n, "b");
189     YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
190     YAZ_CHECK_EQ(json_count_children(n1), 0);
191 
192     n1 = json_get_object(n, "c");
193     YAZ_CHECK(n1 && n1->type == json_node_array);
194     YAZ_CHECK_EQ(json_count_children(n1), 3);
195 
196     n1 = json_get_elem(json_get_object(n, "c"), 0);
197     YAZ_CHECK(n1 && n1->type == json_node_true);
198 
199     n1 = json_get_elem(json_get_object(n, "c"), 1);
200     YAZ_CHECK(n1 && n1->type == json_node_false);
201 
202     n1 = json_get_elem(json_get_object(n, "c"), 2);
203     YAZ_CHECK(n1 && n1->type == json_node_null);
204 
205     n1 = json_get_elem(json_get_object(n, "c"), 3);
206     YAZ_CHECK(n1 == 0);
207 
208     json_remove_node(n);
209 }
210 
append_check(const char * a,const char * b,const char * exp)211 static int append_check(const char *a, const char *b, const char *exp)
212 {
213     WRBUF w = wrbuf_alloc();
214     struct json_node *n_a, *n_b;
215     int ret = 0;
216 
217     n_a = json_parse(a, 0);
218     n_b = json_parse(b, 0);
219     json_append_array(json_get_object(n_a, "a"),
220                       json_detach_object(n_b, "b"));
221 
222     json_write_wrbuf(n_a, w);
223 
224     if (!strcmp(wrbuf_cstr(w), exp))
225         ret = 1;
226     wrbuf_destroy(w);
227     json_remove_node(n_a);
228     json_remove_node(n_b);
229     return ret;
230 }
231 
tst3(void)232 static void tst3(void)
233 {
234     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
235                            "{\"a\":[1,2,3,5,6,7]}"));
236 
237     YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
238                            "{\"a\":[5,6,7]}"));
239 
240     YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
241                            "{\"a\":[1,2,3]}"));
242 }
243 
main(int argc,char ** argv)244 int main (int argc, char **argv)
245 {
246     YAZ_CHECK_INIT(argc, argv);
247     tst1();
248     tst2();
249     tst3();
250     YAZ_CHECK_TERM;
251 }
252 
253 /*
254  * Local variables:
255  * c-basic-offset: 4
256  * c-file-style: "Stroustrup"
257  * indent-tabs-mode: nil
258  * End:
259  * vim: shiftwidth=4 tabstop=8 expandtab
260  */
261 
262 
263