2 * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
17 /* Encode an empty object/array, add an item, encode again */
20 result = json_dumps(json, 0);
21 if(!result || strcmp(result, "{}"))
22 fail("json_dumps failed");
25 json_object_set_new(json, "foo", json_integer(5));
26 result = json_dumps(json, 0);
27 if(!result || strcmp(result, "{\"foo\": 5}"))
28 fail("json_dumps failed");
34 result = json_dumps(json, 0);
35 if(!result || strcmp(result, "[]"))
36 fail("json_dumps failed");
39 json_array_append_new(json, json_integer(5));
40 result = json_dumps(json, 0);
41 if(!result || strcmp(result, "[5]"))
42 fail("json_dumps failed");
47 /* Construct a JSON object/array with a circular reference:
49 object: {"a": {"b": {"c": <circular reference to $.a>}}}
50 array: [[[<circular reference to the $[0] array>]]]
52 Encode it, remove the circular reference and encode again.
55 json_object_set_new(json, "a", json_object());
56 json_object_set_new(json_object_get(json, "a"), "b", json_object());
57 json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
58 json_object_get(json, "a"));
60 if(json_dumps(json, 0))
61 fail("json_dumps encoded a circular reference!");
63 json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
65 result = json_dumps(json, 0);
66 if(!result || strcmp(result, "{\"a\": {\"b\": {}}}"))
67 fail("json_dumps failed!");
73 json_array_append_new(json, json_array());
74 json_array_append_new(json_array_get(json, 0), json_array());
75 json_array_append(json_array_get(json_array_get(json, 0), 0),
76 json_array_get(json, 0));
78 if(json_dumps(json, 0))
79 fail("json_dumps encoded a circular reference!");
81 json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
83 result = json_dumps(json, 0);
84 if(!result || strcmp(result, "[[[]]]"))
85 fail("json_dumps failed!");