10 static int expect(json_parser_t p, const char *input,
16 n = json_parser_parse(p, input);
17 if (n == 0 && output == 0)
21 WRBUF result = wrbuf_alloc();
23 json_write_wrbuf(n, result);
24 if (strcmp(wrbuf_cstr(result), output) == 0)
28 yaz_log(YLOG_WARN, "expected '%s' but got '%s'",
29 output, wrbuf_cstr(result));
31 wrbuf_destroy(result);
35 yaz_log(YLOG_WARN, "expected '%s' but got error '%s'",
36 output, json_parser_get_errmsg(p));
42 static void tst1(void)
44 json_parser_t p = json_parser_create();
50 YAZ_CHECK(expect(p, "", 0));
52 YAZ_CHECK(expect(p, "1234", "1234"));
54 YAZ_CHECK(expect(p, "[ 1234 ]", "[1234]"));
56 YAZ_CHECK(expect(p, "{\"k\":tru}", 0));
58 YAZ_CHECK(expect(p, "{\"k\":null", 0));
60 YAZ_CHECK(expect(p, "{\"k\":nullx}", 0));
62 YAZ_CHECK(expect(p, "{\"k\":-", 0));
64 YAZ_CHECK(expect(p, "{\"k\":+", 0));
66 YAZ_CHECK(expect(p, "{\"k\":\"a}", 0));
68 YAZ_CHECK(expect(p, "{\"k\":\"a", 0));
70 YAZ_CHECK(expect(p, "{\"k\":\"", 0));
72 YAZ_CHECK(expect(p, "{", 0));
74 YAZ_CHECK(expect(p, "{}", "{}"));
76 YAZ_CHECK(expect(p, "{} extra", 0));
78 YAZ_CHECK(expect(p, "{\"a\":[1,2,3}", 0));
80 YAZ_CHECK(expect(p, "{\"a\":[1,2,", 0));
82 YAZ_CHECK(expect(p, "{\"k\":\"wa\"}", "{\"k\":\"wa\"}"));
84 YAZ_CHECK(expect(p, "{\"k\":null}", "{\"k\":null}"));
86 YAZ_CHECK(expect(p, "{\"k\":false}", "{\"k\":false}"));
88 YAZ_CHECK(expect(p, "{\"k\":true}", "{\"k\":true}"));
90 YAZ_CHECK(expect(p, "{\"k\":12}", "{\"k\":12}"));
92 YAZ_CHECK(expect(p, "{\"k\":-12}", "{\"k\":-12}"));
94 YAZ_CHECK(expect(p, "{\"k\":1.2e6}", "{\"k\":1.2e+06}"));
96 YAZ_CHECK(expect(p, "{\"k\":1e3}", "{\"k\":1000}"));
98 YAZ_CHECK(expect(p, "{\"k\":\"\"}", "{\"k\":\"\"}"));
100 YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2}", "{\"a\":1,\"b\":2}"));
102 YAZ_CHECK(expect(p, "{\"a\":1,\"b\":2,\"c\":3}",
103 "{\"a\":1,\"b\":2,\"c\":3}"));
105 YAZ_CHECK(expect(p, "{\"a\":[]}", "{\"a\":[]}"));
107 YAZ_CHECK(expect(p, "{\"a\":[1]}", "{\"a\":[1]}"));
109 YAZ_CHECK(expect(p, "{\"a\":[1,2]}", "{\"a\":[1,2]}"));
111 YAZ_CHECK(expect(p, "{\"a\":[1,2,3]}", "{\"a\":[1,2,3]}"));
113 YAZ_CHECK(expect(p, "{\"k\":\"\\t\"}", "{\"k\":\"\x09\"}"));
115 YAZ_CHECK(expect(p, "{\"k\":\"\\u0009\"}", "{\"k\":\"\x09\"}"));
117 json_parser_destroy(p);
120 static void tst2(void)
122 struct json_node *n, *n1;
124 n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
129 YAZ_CHECK_EQ(json_count_children(n), 3);
131 n1 = json_get_object(n, "a");
132 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
133 YAZ_CHECK_EQ(json_count_children(n1), 0);
135 n1 = json_get_object(n, "b");
136 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
137 YAZ_CHECK_EQ(json_count_children(n1), 0);
139 n1 = json_get_object(n, "b");
140 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
141 YAZ_CHECK_EQ(json_count_children(n1), 0);
143 n1 = json_get_object(n, "c");
144 YAZ_CHECK(n1 && n1->type == json_node_array);
145 YAZ_CHECK_EQ(json_count_children(n1), 3);
147 n1 = json_get_elem(json_get_object(n, "c"), 0);
148 YAZ_CHECK(n1 && n1->type == json_node_true);
150 n1 = json_get_elem(json_get_object(n, "c"), 1);
151 YAZ_CHECK(n1 && n1->type == json_node_false);
153 n1 = json_get_elem(json_get_object(n, "c"), 2);
154 YAZ_CHECK(n1 && n1->type == json_node_null);
156 n1 = json_get_elem(json_get_object(n, "c"), 3);
162 static int append_check(const char *a, const char *b, const char *exp)
164 WRBUF w = wrbuf_alloc();
165 struct json_node *n_a, *n_b;
168 n_a = json_parse(a, 0);
169 n_b = json_parse(b, 0);
170 json_append_array(json_get_object(n_a, "a"),
171 json_detach_object(n_b, "b"));
173 json_write_wrbuf(n_a, w);
175 if (!strcmp(wrbuf_cstr(w), exp))
178 json_remove_node(n_a);
179 json_remove_node(n_b);
183 static void tst3(void)
185 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
186 "{\"a\":[1,2,3,5,6,7]}"));
188 YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
191 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
195 int main (int argc, char **argv)
197 YAZ_CHECK_INIT(argc, argv);
207 * c-file-style: "Stroustrup"
208 * indent-tabs-mode: nil
210 * vim: shiftwidth=4 tabstop=8 expandtab