2 * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
3 * Copyright (c) 2010 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(), &error, "b", &i1);
32 fail("json_unpack boolean failed");
35 rv = json_unpack(json_false(), &error, "b", &i1);
37 fail("json_unpack boolean failed");
40 rv = json_unpack(json_null(), &error, "n");
42 fail("json_unpack null failed");
46 rv = json_unpack(j, &error, "i", &i1);
48 fail("json_unpack integer failed");
53 rv = json_unpack(j, &error, "f", &f);
55 fail("json_unpack real failed");
59 j = json_string("foo");
60 rv = json_unpack(j, &error, "s", &s);
61 if(rv || strcmp(s, "foo"))
62 fail("json_unpack string failed");
67 rv = json_unpack(j, &error, "{}");
69 fail("json_unpack empty object failed");
74 rv = json_unpack(j, &error, "[]");
76 fail("json_unpack empty list failed");
79 /* non-incref'd object */
81 rv = json_unpack(j, &error, "o", &j2);
82 if(j2 != j || j->refcount != (ssize_t)1)
83 fail("json_unpack object failed");
88 rv = json_unpack(j, &error, "O", &j2);
89 if(j2 != j || j->refcount != (ssize_t)2)
90 fail("json_unpack object failed");
95 j = json_pack(&error, "{s:i}", "foo", 1);
96 rv = json_unpack(j, &error, "{s:i}", "foo", &i1);
98 fail("json_unpack simple object failed");
102 j = json_pack(&error, "[iii]", 1, 2, 3);
103 rv = json_unpack(j, &error, "[i,i,i]", &i1, &i2, &i3);
104 if(rv || i1 != 1 || i2 != 2 || i3 != 3)
105 fail("json_unpack simple array failed");
112 /* mismatched open/close array/object */
113 j = json_pack(&error, "[]");
114 rv = json_unpack(j, &error, "[}");
116 fail("json_unpack failed to catch mismatched ']'");
119 j = json_pack(&error, "{}");
120 rv = json_unpack(j, &error, "{]");
122 fail("json_unpack failed to catch mismatched '}'");
125 /* missing close array */
126 j = json_pack(&error, "[]");
127 rv = json_unpack(j, &error, "[");
129 fail("json_unpack failed to catch missing ']'");
132 /* missing close object */
133 j = json_pack(&error, "{}");
134 rv = json_unpack(j, &error, "{");
136 fail("json_unpack failed to catch missing '}'");
139 /* NULL format string */
140 j = json_pack(&error, "[]");
141 rv =json_unpack(j, &error, NULL);
143 fail("json_unpack failed to catch null format string");
146 /* NULL string pointer */
147 j = json_string("foobie");
148 rv =json_unpack(j, &error, "s", NULL);
150 fail("json_unpack failed to catch null string pointer");
155 //fprintf(stderr, "%i/%i: %s %s\n", error.line, error.column, error.source, error.text);
158 /* vim: ts=4:expandtab:sw=4