2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2010-2011 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
5 * Jansson is free software; you can redistribute it and/or modify
6 * it under the terms of the MIT license. See LICENSE for details.
26 * Simple, valid json_pack cases
30 rv = json_unpack(json_true(), "b", &i1);
32 fail("json_unpack boolean failed");
35 rv = json_unpack(json_false(), "b", &i1);
37 fail("json_unpack boolean failed");
40 if(json_unpack(json_null(), "n"))
41 fail("json_unpack null failed");
45 rv = json_unpack(j, "i", &i1);
47 fail("json_unpack integer failed");
51 j = json_integer(5555555);
52 rv = json_unpack(j, "I", &I1);
53 if(rv || I1 != 5555555)
54 fail("json_unpack json_int_t failed");
59 rv = json_unpack(j, "f", &f);
61 fail("json_unpack real failed");
65 j = json_integer(12345);
66 rv = json_unpack(j, "F", &f);
67 if(rv || f != 12345.0)
68 fail("json_unpack (real or) integer failed");
72 rv = json_unpack(j, "F", &f);
74 fail("json_unpack real (or integer) failed");
78 j = json_string("foo");
79 rv = json_unpack(j, "s", &s);
80 if(rv || strcmp(s, "foo"))
81 fail("json_unpack string failed");
86 if(json_unpack(j, "{}"))
87 fail("json_unpack empty object failed");
92 if(json_unpack(j, "[]"))
93 fail("json_unpack empty list failed");
96 /* non-incref'd object */
98 rv = json_unpack(j, "o", &j2);
99 if(j2 != j || j->refcount != 1)
100 fail("json_unpack object failed");
103 /* incref'd object */
105 rv = json_unpack(j, "O", &j2);
106 if(j2 != j || j->refcount != 2)
107 fail("json_unpack object failed");
112 j = json_pack("{s:i}", "foo", 42);
113 rv = json_unpack(j, "{s:i}", "foo", &i1);
115 fail("json_unpack simple object failed");
119 j = json_pack("[iii]", 1, 2, 3);
120 rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
121 if(rv || i1 != 1 || i2 != 2 || i3 != 3)
122 fail("json_unpack simple array failed");
129 j = json_integer(42);
130 if(!json_unpack_ex(j, &error, 0, "z"))
131 fail("json_unpack succeeded with invalid format character");
132 check_error("Unexpected format character 'z'", "<format>", 1, 1, 1);
134 if(!json_unpack_ex(NULL, &error, 0, "[i]"))
135 fail("json_unpack succeeded with NULL root");
136 check_error("NULL root value", "<root>", -1, -1, 0);
139 /* mismatched open/close array/object */
141 if(!json_unpack_ex(j, &error, 0, "[}"))
142 fail("json_unpack failed to catch mismatched ']'");
143 check_error("Unexpected format character '}'", "<format>", 1, 2, 2);
147 if(!json_unpack_ex(j, &error, 0, "{]"))
148 fail("json_unpack failed to catch mismatched '}'");
149 check_error("Expected format 's', got ']'", "<format>", 1, 2, 2);
152 /* missing close array */
154 if(!json_unpack_ex(j, &error, 0, "["))
155 fail("json_unpack failed to catch missing ']'");
156 check_error("Unexpected end of format string", "<format>", 1, 2, 2);
159 /* missing close object */
161 if(!json_unpack_ex(j, &error, 0, "{"))
162 fail("json_unpack failed to catch missing '}'");
163 check_error("Unexpected end of format string", "<format>", 1, 2, 2);
166 /* garbage after format string */
167 j = json_pack("[i]", 42);
168 if(!json_unpack_ex(j, &error, 0, "[i]a", &i1))
169 fail("json_unpack failed to catch garbage after format string");
170 check_error("Garbage after format string", "<format>", 1, 4, 4);
173 j = json_integer(12345);
174 if(!json_unpack_ex(j, &error, 0, "ia", &i1))
175 fail("json_unpack failed to catch garbage after format string");
176 check_error("Garbage after format string", "<format>", 1, 2, 2);
179 /* NULL format string */
181 if(!json_unpack_ex(j, &error, 0, NULL))
182 fail("json_unpack failed to catch null format string");
183 check_error("NULL or empty format string", "<format>", -1, -1, 0);
186 /* NULL string pointer */
187 j = json_string("foobie");
188 if(!json_unpack_ex(j, &error, 0, "s", NULL))
189 fail("json_unpack failed to catch null string pointer");
190 check_error("NULL string argument", "<args>", 1, 1, 1);
194 j = json_integer(42);
195 j2 = json_string("foo");
196 if(!json_unpack_ex(j, &error, 0, "s"))
197 fail("json_unpack failed to catch invalid type");
198 check_error("Expected string, got integer", "<validation>", 1, 1, 1);
200 if(!json_unpack_ex(j, &error, 0, "n"))
201 fail("json_unpack failed to catch invalid type");
202 check_error("Expected null, got integer", "<validation>", 1, 1, 1);
204 if(!json_unpack_ex(j, &error, 0, "b"))
205 fail("json_unpack failed to catch invalid type");
206 check_error("Expected true or false, got integer", "<validation>", 1, 1, 1);
208 if(!json_unpack_ex(j2, &error, 0, "i"))
209 fail("json_unpack failed to catch invalid type");
210 check_error("Expected integer, got string", "<validation>", 1, 1, 1);
212 if(!json_unpack_ex(j2, &error, 0, "I"))
213 fail("json_unpack failed to catch invalid type");
214 check_error("Expected integer, got string", "<validation>", 1, 1, 1);
216 if(!json_unpack_ex(j, &error, 0, "f"))
217 fail("json_unpack failed to catch invalid type");
218 check_error("Expected real, got integer", "<validation>", 1, 1, 1);
220 if(!json_unpack_ex(j2, &error, 0, "F"))
221 fail("json_unpack failed to catch invalid type");
222 check_error("Expected real or integer, got string", "<validation>", 1, 1, 1);
224 if(!json_unpack_ex(j, &error, 0, "[i]"))
225 fail("json_unpack failed to catch invalid type");
226 check_error("Expected array, got integer", "<validation>", 1, 1, 1);
228 if(!json_unpack_ex(j, &error, 0, "{si}", "foo"))
229 fail("json_unpack failed to catch invalid type");
230 check_error("Expected object, got integer", "<validation>", 1, 1, 1);
235 /* Array index out of range */
236 j = json_pack("[i]", 1);
237 if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2))
238 fail("json_unpack failed to catch index out of array bounds");
239 check_error("Array index 1 out of range", "<validation>", 1, 3, 3);
242 /* NULL object key */
243 j = json_pack("{si}", "foo", 42);
244 if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1))
245 fail("json_unpack failed to catch null string pointer");
246 check_error("NULL object key", "<args>", 1, 2, 2);
249 /* Object key not found */
250 j = json_pack("{si}", "foo", 42);
251 if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1))
252 fail("json_unpack failed to catch null string pointer");
253 check_error("Object item not found: baz", "<validation>", 1, 3, 3);
260 j = json_pack("[iii]", 1, 2, 3);
261 rv = json_unpack(j, "[iii!]", &i1, &i2, &i3);
262 if(rv || i1 != 1 || i2 != 2 || i3 != 3)
263 fail("json_unpack array with strict validation failed");
266 j = json_pack("[iii]", 1, 2, 3);
267 if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2))
268 fail("json_unpack array with strict validation failed");
269 check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);
272 /* Like above, but with JSON_STRICT instead of '!' format */
273 j = json_pack("[iii]", 1, 2, 3);
274 if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2))
275 fail("json_unpack array with strict validation failed");
276 check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4);
279 j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
280 rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1);
281 if(rv || strcmp(s, "bar") != 0 || i1 != 42)
282 fail("json_unpack object with strict validation failed");
285 /* Unpack the same item twice */
286 j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
287 if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
288 fail("json_unpack object with strict validation failed");
289 check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
292 j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
293 if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY,
294 "[i{sisn}[ii]]", "foo", "bar"))
295 fail("json_unpack complex value with strict validation failed");
298 /* ! and * must be last */
299 j = json_pack("[ii]", 1, 2);
300 if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2))
301 fail("json_unpack failed to catch ! in the middle of an array");
302 check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4);
304 if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2))
305 fail("json_unpack failed to catch * in the middle of an array");
306 check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4);
309 j = json_pack("{sssi}", "foo", "bar", "baz", 42);
310 if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1))
311 fail("json_unpack failed to catch ! in the middle of an object");
312 check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5);
314 if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1))
315 fail("json_unpack failed to catch ! in the middle of an object");
316 check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5);
319 /* Error in nested object */
320 j = json_pack("{s{snsn}}", "foo", "bar", "baz");
321 if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
322 fail("json_unpack nested object with strict validation failed");
323 check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
326 /* Error in nested array */
327 j = json_pack("[[ii]]", 1, 2);
328 if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1))
329 fail("json_unpack nested array with strict validation failed");
330 check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5);