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\":\"\\t\"}"));
114 YAZ_CHECK(expect(p, "{\"k\":\"\t\"}", "{\"k\":\"\\t\"}"));
116 YAZ_CHECK(expect(p, "{\"k\":\"\\n\"}", "{\"k\":\"\\n\"}"));
117 YAZ_CHECK(expect(p, "{\"k\":\"\n\"}", "{\"k\":\"\\n\"}"));
119 YAZ_CHECK(expect(p, "{\"k\":\"\\r\"}", "{\"k\":\"\\r\"}"));
120 YAZ_CHECK(expect(p, "{\"k\":\"\r\"}", "{\"k\":\"\\r\"}"));
122 YAZ_CHECK(expect(p, "{\"k\":\"\\f\"}", "{\"k\":\"\\f\"}"));
123 YAZ_CHECK(expect(p, "{\"k\":\"\f\"}", "{\"k\":\"\\f\"}"));
125 YAZ_CHECK(expect(p, "{\"k\":\"\\b\"}", "{\"k\":\"\\b\"}"));
126 YAZ_CHECK(expect(p, "{\"k\":\"\b\"}", "{\"k\":\"\\b\"}"));
129 "{\"k\":\"\\u0001\\u0002\"}",
130 "{\"k\":\"\\u0001\\u0002\"}"));
132 json_parser_destroy(p);
135 static void tst2(void)
137 struct json_node *n, *n1;
139 n = json_parse("{\"a\":1,\"b\":2,\"c\":[true,false,null]}", 0);
144 YAZ_CHECK_EQ(json_count_children(n), 3);
146 n1 = json_get_object(n, "a");
147 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 1.0);
148 YAZ_CHECK_EQ(json_count_children(n1), 0);
150 n1 = json_get_object(n, "b");
151 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
152 YAZ_CHECK_EQ(json_count_children(n1), 0);
154 n1 = json_get_object(n, "b");
155 YAZ_CHECK(n1 && n1->type == json_node_number && n1->u.number == 2.0);
156 YAZ_CHECK_EQ(json_count_children(n1), 0);
158 n1 = json_get_object(n, "c");
159 YAZ_CHECK(n1 && n1->type == json_node_array);
160 YAZ_CHECK_EQ(json_count_children(n1), 3);
162 n1 = json_get_elem(json_get_object(n, "c"), 0);
163 YAZ_CHECK(n1 && n1->type == json_node_true);
165 n1 = json_get_elem(json_get_object(n, "c"), 1);
166 YAZ_CHECK(n1 && n1->type == json_node_false);
168 n1 = json_get_elem(json_get_object(n, "c"), 2);
169 YAZ_CHECK(n1 && n1->type == json_node_null);
171 n1 = json_get_elem(json_get_object(n, "c"), 3);
177 static int append_check(const char *a, const char *b, const char *exp)
179 WRBUF w = wrbuf_alloc();
180 struct json_node *n_a, *n_b;
183 n_a = json_parse(a, 0);
184 n_b = json_parse(b, 0);
185 json_append_array(json_get_object(n_a, "a"),
186 json_detach_object(n_b, "b"));
188 json_write_wrbuf(n_a, w);
190 if (!strcmp(wrbuf_cstr(w), exp))
193 json_remove_node(n_a);
194 json_remove_node(n_b);
198 static void tst3(void)
200 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[5,6,7]}",
201 "{\"a\":[1,2,3,5,6,7]}"));
203 YAZ_CHECK(append_check("{\"a\":[]}", "{\"b\":[5,6,7]}",
206 YAZ_CHECK(append_check("{\"a\":[1,2,3]}", "{\"b\":[]}",
210 int main (int argc, char **argv)
212 YAZ_CHECK_INIT(argc, argv);
222 * c-file-style: "Stroustrup"
223 * indent-tabs-mode: nil
225 * vim: shiftwidth=4 tabstop=8 expandtab