2 * Copyright (c) 2009 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 json_t *create_object()
16 object = json_object();
18 fail("unable to create an object");
20 if(json_object_set_new(object, "a", json_integer(1)) ||
21 json_object_set_new(object, "b", json_integer(2)))
22 fail("unable to set object values");
27 static json_t *create_array()
33 fail("unable to create an array");
35 if(json_array_append_new(array, json_integer(1)) ||
36 json_array_append_new(array, json_integer(2)))
37 fail("unable to append array values");
43 #define NORMAL_OBJECT "{\"a\": 1, \"b\": 2}"
44 #define NORMAL_ARRAY "[1, 2]"
46 static void test_normal()
52 object = create_object();
53 array = create_array();
55 result = json_dumps(object, 0);
56 if(strcmp(result, NORMAL_OBJECT) != 0)
57 fail("unexpected encoded object");
60 result = json_dumps(array, 0);
61 if(strcmp(result, NORMAL_ARRAY) != 0)
62 fail("unexpected encoded array");
70 #define INDENTED_OBJECT \
75 #define INDENTED_ARRAY \
81 static void test_indent()
87 object = create_object();
88 array = create_array();
90 result = json_dumps(object, JSON_INDENT(4));
91 if(strcmp(result, INDENTED_OBJECT) != 0)
92 fail("unexpected encoded object");
95 result = json_dumps(array, JSON_INDENT(4));
96 if(strcmp(result, INDENTED_ARRAY) != 0)
97 fail("unexpected encoded array");
105 #define COMPACT_OBJECT "{\"a\":1,\"b\":2}"
106 #define COMPACT_ARRAY "[1,2]"
108 static void test_compact()
114 object = create_object();
115 array = create_array();
117 result = json_dumps(object, JSON_COMPACT);
118 if(strcmp(result, COMPACT_OBJECT) != 0)
119 fail("unexpected encoded object");
122 result = json_dumps(array, JSON_COMPACT);
123 if(strcmp(result, COMPACT_ARRAY) != 0)
124 fail("unexpected encoded array");
132 #define INDENTED_COMPACT_OBJECT \
137 #define INDENTED_COMPACT_ARRAY \
143 static void test_compact_indent()
149 object = create_object();
150 array = create_array();
152 result = json_dumps(object, JSON_INDENT(4) | JSON_COMPACT);
153 if(strcmp(result, INDENTED_COMPACT_OBJECT) != 0)
154 fail("unexpected encoded object");
157 result = json_dumps(array, JSON_INDENT(4) | JSON_COMPACT);
158 if(strcmp(result, INDENTED_COMPACT_ARRAY) != 0)
159 fail("unexpected encoded array");
167 static const char *test_ensure_ascii_data[][2] = {
169 { "input", "output" }
176 { "\xc3\xa4 \xc3\xb6 \xc3\xa5", "\\u00e4 \\u00f6 \\u00e5" },
177 { "foo \xc3\xa4\xc3\xa5", "foo \\u00e4\\u00e5" },
178 { "\xc3\xa4\xc3\xa5 foo", "\\u00e4\\u00e5 foo" },
179 { "\xc3\xa4 foo \xc3\xa5", "\\u00e4 foo \\u00e5" },
182 { "clef g: \xf0\x9d\x84\x9e", "clef g: \\ud834\\udd1e" },
185 static void test_ensure_ascii()
188 int num_tests = sizeof(test_ensure_ascii_data) / sizeof(const char *) / 2;
190 for(i = 0; i < num_tests; i++) {
191 json_t *array, *string;
192 const char *input, *output;
193 char *result, *stripped;
195 input = test_ensure_ascii_data[i][0];
196 output = test_ensure_ascii_data[i][1];
198 array = json_array();
199 string = json_string(input);
200 if(!array || !string)
201 fail("unable to create json values");
203 json_array_append(array, string);
204 result = json_dumps(array, JSON_ENSURE_ASCII);
206 /* strip leading [" and trailing "] */
207 stripped = &result[2];
208 stripped[strlen(stripped) - 2] = '\0';
210 if(strcmp(stripped, output) != 0) {
212 fail("the result of json_dumps is invalid");
223 test_compact_indent();