]> git.ozlabs.org Git - ccan/blob - ccan/json/test/run-stringify.c
json: new module for parsing and generating JSON
[ccan] / ccan / json / test / run-stringify.c
1 #include "common.h"
2
3 static char buf1[256], buf2[256];
4
5 /* Used for pass and fail messages */
6 static char *quote_string(const char *str, char buf[256])
7 {
8         char *out = buf;
9         
10         *out++ = '"';
11         for (; *str != 0; str++) {
12                 if (out - buf > 256 - 5) {
13                         /* String is too long.  End it with `...' */
14                         out = buf + 256 - 5;
15                         *out++ = '.';
16                         *out++ = '.';
17                         *out++ = '.';
18                         break;
19                 }
20                 switch (*str) {
21                         case '\t':
22                                 *out++ = '\\';
23                                 *out++ = 't';
24                                 break;
25                         case '\n':
26                                 *out++ = '\\';
27                                 *out++ = 'n';
28                                 break;
29                         case '"':
30                                 *out++ = '\\';
31                                 *out++ = '"';
32                                 break;
33                         case '\\':
34                                 *out++ = '\\';
35                                 *out++ = '\\';
36                                 break;
37                         default:
38                                 *out++ = *str;
39                                 break;
40                 }
41         }
42         *out++ = '"';
43         
44         *out = 0;
45         return buf;
46 }
47
48 static void test_stringify(const char *input, const char *expected)
49 {
50         JsonNode *node = NULL;
51         char *enc = NULL;
52         char *strn = NULL;
53         char *str = NULL;
54         
55         node = json_decode(input);
56         if (node == NULL) {
57                 fail("Failed to decode %s", input);
58                 goto end;
59         }
60         
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);
64                 goto end;
65         }
66         
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");
70                 goto end;
71         }
72         
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));
77                 goto end;
78         }
79         
80         pass("stringify %s", input);
81         
82 end:
83         json_delete(node);
84         free(enc);
85         free(strn);
86         free(str);
87 }
88
89 int main(void)
90 {
91         (void) chomp;
92         
93         plan_tests(9);
94         
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]");
100         
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}");
106         
107         return exit_status();
108 }