3 static char buf1[256], buf2[256];
5 /* Used for pass and fail messages */
6 static char *quote_string(const char *str, char buf[256])
11 for (; *str != 0; str++) {
12 if (out - buf > 256 - 5) {
13 /* String is too long. End it with `...' */
48 static void test_stringify(const char *input, const char *expected)
50 JsonNode *node = NULL;
55 node = json_decode(input);
57 fail("Failed to decode %s", input);
61 enc = json_encode(node);
62 if (strcmp(enc, input) != 0) {
63 fail("%s re-encodes to %s. Either encode/decode is broken, or the input string needs to be normalized", input, enc);
67 strn = json_stringify(node, NULL);
68 if (strcmp(strn, enc) != 0) {
69 fail("json_stringify with NULL space produced a different string than json_encode");
73 str = json_stringify(node, "\t");
74 if (strcmp(str, expected) != 0) {
75 fail("Expected %s, but json_stringify produced %s",
76 quote_string(expected, buf1), quote_string(str, buf2));
80 pass("stringify %s", input);
95 test_stringify("[]", "[]");
96 test_stringify("[1]", "[\n\t1\n]");
97 test_stringify("[1,2,3]", "[\n\t1,\n\t2,\n\t3\n]");
98 test_stringify("[[]]", "[\n\t[]\n]");
99 test_stringify("[[1,2],[3,4]]", "[\n\t[\n\t\t1,\n\t\t2\n\t],\n\t[\n\t\t3,\n\t\t4\n\t]\n]");
101 test_stringify("{}", "{}");
102 test_stringify("{\"one\":1}", "{\n\t\"one\": 1\n}");
103 test_stringify("{\"one\":1,\"t*\":[2,3,10]}", "{\n\t\"one\": 1,\n\t\"t*\": [\n\t\t2,\n\t\t3,\n\t\t10\n\t]\n}");
104 test_stringify("{\"a\":{\"1\":1,\"2\":2},\"b\":{\"3\":[null,false,true,\"\\f\"]}}",
105 "{\n\t\"a\": {\n\t\t\"1\": 1,\n\t\t\"2\": 2\n\t},\n\t\"b\": {\n\t\t\"3\": [\n\t\t\tnull,\n\t\t\tfalse,\n\t\t\ttrue,\n\t\t\t\"\\f\"\n\t\t]\n\t}\n}");
107 return exit_status();