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.
12 static void encode_twice()
14 /* 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");
48 static void circular_references()
50 /* Construct a JSON object/array with a circular reference:
52 object: {"a": {"b": {"c": <circular reference to $.a>}}}
53 array: [[[<circular reference to the $[0] array>]]]
55 Encode it, remove the circular reference and encode again.
62 json_object_set_new(json, "a", json_object());
63 json_object_set_new(json_object_get(json, "a"), "b", json_object());
64 json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
65 json_object_get(json, "a"));
67 if(json_dumps(json, 0))
68 fail("json_dumps encoded a circular reference!");
70 json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
72 result = json_dumps(json, 0);
73 if(!result || strcmp(result, "{\"a\": {\"b\": {}}}"))
74 fail("json_dumps failed!");
80 json_array_append_new(json, json_array());
81 json_array_append_new(json_array_get(json, 0), json_array());
82 json_array_append(json_array_get(json_array_get(json, 0), 0),
83 json_array_get(json, 0));
85 if(json_dumps(json, 0))
86 fail("json_dumps encoded a circular reference!");
88 json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
90 result = json_dumps(json, 0);
91 if(!result || strcmp(result, "[[[]]]"))
92 fail("json_dumps failed!");
98 static void encode_other_than_array_or_object()
100 /* Encoding anything other than array or object should only
101 * succeed if the JSON_ENCODE_ANY flag is used */
107 json = json_string("foo");
108 if(json_dumps(json, 0) != NULL)
109 fail("json_dumps encoded a string!");
110 if(json_dumpf(json, fp, 0) == 0)
111 fail("json_dumpf encoded a string!");
113 result = json_dumps(json, JSON_ENCODE_ANY);
114 if(!result || strcmp(result, "\"foo\"") != 0)
115 fail("json_dumps failed to encode a string with JSON_ENCODE_ANY");
120 json = json_integer(42);
121 if(json_dumps(json, 0) != NULL)
122 fail("json_dumps encoded an integer!");
123 if(json_dumpf(json, fp, 0) == 0)
124 fail("json_dumpf encoded an integer!");
126 result = json_dumps(json, JSON_ENCODE_ANY);
127 if(!result || strcmp(result, "42") != 0)
128 fail("json_dumps failed to encode an integer with JSON_ENCODE_ANY");
139 circular_references();
140 encode_other_than_array_or_object();