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.
25 * Simple, valid json_pack cases
29 rv = json_unpack_ex(json_true(), &error, 0, "b", &i1);
31 fail("json_unpack boolean failed");
34 rv = json_unpack_ex(json_false(), &error, 0, "b", &i1);
36 fail("json_unpack boolean failed");
39 if(json_unpack_ex(json_null(), &error, 0, "n"))
40 fail("json_unpack null failed");
44 rv = json_unpack_ex(j, &error, 0, "i", &i1);
46 fail("json_unpack integer failed");
51 rv = json_unpack_ex(j, &error, 0, "f", &f);
53 fail("json_unpack real failed");
57 j = json_string("foo");
58 rv = json_unpack_ex(j, &error, 0, "s", &s);
59 if(rv || strcmp(s, "foo"))
60 fail("json_unpack string failed");
65 if(json_unpack_ex(j, &error, 0, "{}"))
66 fail("json_unpack empty object failed");
71 if(json_unpack_ex(j, &error, 0, "[]"))
72 fail("json_unpack empty list failed");
75 /* non-incref'd object */
77 rv = json_unpack_ex(j, &error, 0, "o", &j2);
78 if(j2 != j || j->refcount != 1)
79 fail("json_unpack object failed");
84 rv = json_unpack_ex(j, &error, 0, "O", &j2);
85 if(j2 != j || j->refcount != 2)
86 fail("json_unpack object failed");
91 j = json_pack("{s:i}", "foo", 42);
92 rv = json_unpack_ex(j, &error, 0, "{s:i}", "foo", &i1);
94 fail("json_unpack simple object failed");
98 j = json_pack("[iii]", 1, 2, 3);
99 rv = json_unpack_ex(j, &error, 0, "[i,i,i]", &i1, &i2, &i3);
100 if(rv || i1 != 1 || i2 != 2 || i3 != 3)
101 fail("json_unpack simple array failed");
108 /* mismatched open/close array/object */
110 if(!json_unpack_ex(j, &error, 0, "[}"))
111 fail("json_unpack failed to catch mismatched ']'");
115 if(!json_unpack_ex(j, &error, 0, "{]"))
116 fail("json_unpack failed to catch mismatched '}'");
119 /* missing close array */
121 if(!json_unpack_ex(j, &error, 0, "["))
122 fail("json_unpack failed to catch missing ']'");
125 /* missing close object */
127 if(!json_unpack_ex(j, &error, 0, "{"))
128 fail("json_unpack failed to catch missing '}'");
131 /* NULL format string */
133 if(!json_unpack_ex(j, &error, 0, NULL))
134 fail("json_unpack failed to catch null format string");
137 /* NULL string pointer */
138 j = json_string("foobie");
139 if(!json_unpack_ex(j, &error, 0, "s", NULL))
140 fail("json_unpack failed to catch null string pointer");