1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
18 static int expect(json_parser_t p, const char *input,
24 n = json_parser_parse(p, input);
25 if (n == 0 && output == 0)
29 WRBUF result = wrbuf_alloc();
31 json_write_wrbuf(n, result);
32 if (strcmp(wrbuf_cstr(result), output) == 0)
36 yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
37 output, wrbuf_cstr(result));
39 wrbuf_destroy(result);
43 yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
44 output, json_parser_get_errmsg(p));
50 static void tst1(void)
52 json_parser_t p = json_parser_create();
58 YAZ_CHECK(expect(p, "", 0));
60 YAZ_CHECK(expect(p, "1234", "1234"));
62 YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));
64 YAZ_CHECK(expect(p, "{\"k\":tru}", 0));
66 YAZ_CHECK(expect(p, "{\"k\":null", 0));
68 YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));
70 YAZ_CHECK(expect(p, "{\"k\":-", 0));
72 YAZ_CHECK(expect(p, "{\"k\":+", 0));
74 YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));
76 YAZ_CHECK(expect(p, "{\"k\":\"a", 0));
78 YAZ_CHECK(expect(p, "{\"k\":\"", 0));
80 YAZ_CHECK(expect(p, "{", 0));
82 YAZ_CHECK(expect(p, "{}", "{}"));
84 YAZ_CHECK(expect(p, "{} extra", 0));
86 YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
88 YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));
90 YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));
92 YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));
94 YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));
96 YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));
98 YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));
100 YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));
102 YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));
104 YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));
106 YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));
108 YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));
110 YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
111 "{\"a\":1,\"b\":2,\"c\":3}"));
113 YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));
115 YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));
117 YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));
119 YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));
121 YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\\t\"}"));
122 YAZ_CHECK(expect(p, "{\"k\":\"\t\"}", "{\"k\":\"\\t\"}"));
124 YAZ_CHECK(expect(p, "{\"k\":\"\\n\"}", "{\"k\":\"\\n\"}"));
125 YAZ_CHECK(expect(p, "{\"k\":\"\n\"}", "{\"k\":\"\\n\"}"));
127 YAZ_CHECK(expect(p, "{\"k\":\"\\r\"}", "{\"k\":\"\\r\"}"));
128 YAZ_CHECK(expect(p, "{\"k\":\"\r\"}", "{\"k\":\"\\r\"}"));
130 YAZ_CHECK(expect(p, "{\"k\":\"\\f\"}", "{\"k\":\"\\f\"}"));
131 YAZ_CHECK(expect(p, "{\"k\":\"\f\"}", "{\"k\":\"\\f\"}"));
133 YAZ_CHECK(expect(p, "{\"k\":\"\\b\"}", "{\"k\":\"\\b\"}"));
134 YAZ_CHECK(expect(p, "{\"k\":\"\b\"}", "{\"k\":\"\\b\"}"));
137 "{\"k\":\"\\u0001\\u0002\"}",
138 "{\"k\":\"\\u0001\\u0002\"}"));
140 json_parser_destroy(p);
143 static void tst2(void)
145 struct json_node *n, *n1;
147 n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
152 YAZ_CHECK_EQ(json_count_children(n), 3);
154 n1 = json_get_object(n, "a");
155 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
156 YAZ_CHECK_EQ(json_count_children(n1), 0);
158 n1 = json_get_object(n, "b");
159 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
160 YAZ_CHECK_EQ(json_count_children(n1), 0);
162 n1 = json_get_object(n, "b");
163 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
164 YAZ_CHECK_EQ(json_count_children(n1), 0);
166 n1 = json_get_object(n, "c");
167 YAZ_CHECK(n1 && n1->type == json_node_array);
168 YAZ_CHECK_EQ(json_count_children(n1), 3);
170 n1 = json_get_elem(json_get_object(n, "c"), 0);
171 YAZ_CHECK(n1 && n1->type == json_node_true);
173 n1 = json_get_elem(json_get_object(n, "c"), 1);
174 YAZ_CHECK(n1 && n1->type == json_node_false);
176 n1 = json_get_elem(json_get_object(n, "c"), 2);
177 YAZ_CHECK(n1 && n1->type == json_node_null);
179 n1 = json_get_elem(json_get_object(n, "c"), 3);
185 static int append_check(const char *a, const char *b, const char *exp)
187 WRBUF w = wrbuf_alloc();
188 struct json_node *n_a, *n_b;
191 n_a = json_parse(a, 0);
192 n_b = json_parse(b, 0);
193 json_append_array(json_get_object(n_a, "a"),
194 json_detach_object(n_b, "b"));
196 json_write_wrbuf(n_a, w);
198 if (!strcmp(wrbuf_cstr(w), exp))
201 json_remove_node(n_a);
202 json_remove_node(n_b);
206 static void tst3(void)
208 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
209 "{\"a\":[1,2,3,5,6,7]}"));
211 YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
214 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
218 int main (int argc, char **argv)
220 YAZ_CHECK_INIT(argc, argv);
230 * c-file-style: "Stroustrup"
231 * indent-tabs-mode: nil
233 * vim: shiftwidth=4 tabstop=8 expandtab