Implement JSON_SORT_KEYS encoding flag
[jansson.git] / test / suites / api / test_dump.c
1 /*
2  * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
3  *
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.
6  */
7
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 static json_t *create_object()
13 {
14     json_t *object;
15
16     object = json_object();
17     if(!object)
18         fail("unable to create an object");
19
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");
23
24     return object;
25 }
26
27 static json_t *create_array()
28 {
29     json_t *array;
30
31     array = json_array();
32     if(!array)
33         fail("unable to create an array");
34
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");
38
39     return array;
40 }
41
42
43 #define NORMAL_OBJECT "{\"a\": 1, \"b\": 2}"
44 #define NORMAL_ARRAY  "[1, 2]"
45
46 static void test_normal()
47 {
48     json_t *object;
49     json_t *array;
50     char *result;
51
52     object = create_object();
53     array = create_array();
54
55     result = json_dumps(object, 0);
56     if(strcmp(result, NORMAL_OBJECT) != 0)
57         fail("unexpected encoded object");
58     free(result);
59
60     result = json_dumps(array, 0);
61     if(strcmp(result, NORMAL_ARRAY) != 0)
62         fail("unexpected encoded array");
63     free(result);
64
65     json_decref(object);
66     json_decref(array);
67 }
68
69
70 #define INDENTED_OBJECT                         \
71     "{\n"                                       \
72     "    \"a\": 1,\n"                           \
73     "    \"b\": 2\n"                            \
74     "}"
75 #define INDENTED_ARRAY                          \
76     "[\n"                                       \
77     "    1,\n"                                  \
78     "    2\n"                                   \
79     "]"
80
81 static void test_indent()
82 {
83     json_t *object;
84     json_t *array;
85     char *result;
86
87     object = create_object();
88     array = create_array();
89
90     result = json_dumps(object, JSON_INDENT(4));
91     if(strcmp(result, INDENTED_OBJECT) != 0)
92         fail("unexpected encoded object");
93     free(result);
94
95     result = json_dumps(array, JSON_INDENT(4));
96     if(strcmp(result, INDENTED_ARRAY) != 0)
97         fail("unexpected encoded array");
98     free(result);
99
100     json_decref(object);
101     json_decref(array);
102 }
103
104
105 #define COMPACT_OBJECT "{\"a\":1,\"b\":2}"
106 #define COMPACT_ARRAY  "[1,2]"
107
108 static void test_compact()
109 {
110     json_t *object;
111     json_t *array;
112     char *result;
113
114     object = create_object();
115     array = create_array();
116
117     result = json_dumps(object, JSON_COMPACT);
118     if(strcmp(result, COMPACT_OBJECT) != 0)
119         fail("unexpected encoded object");
120     free(result);
121
122     result = json_dumps(array, JSON_COMPACT);
123     if(strcmp(result, COMPACT_ARRAY) != 0)
124         fail("unexpected encoded array");
125     free(result);
126
127     json_decref(object);
128     json_decref(array);
129 }
130
131
132 #define INDENTED_COMPACT_OBJECT                 \
133     "{\n"                                       \
134     "    \"a\":1,\n"                            \
135     "    \"b\":2\n"                             \
136     "}"
137 #define INDENTED_COMPACT_ARRAY                  \
138     "[\n"                                       \
139     "    1,\n"                                  \
140     "    2\n"                                   \
141     "]"
142
143 static void test_compact_indent()
144 {
145     json_t *object;
146     json_t *array;
147     char *result;
148
149     object = create_object();
150     array = create_array();
151
152     result = json_dumps(object, JSON_INDENT(4) | JSON_COMPACT);
153     if(strcmp(result, INDENTED_COMPACT_OBJECT) != 0)
154         fail("unexpected encoded object");
155     free(result);
156
157     result = json_dumps(array, JSON_INDENT(4) | JSON_COMPACT);
158     if(strcmp(result, INDENTED_COMPACT_ARRAY) != 0)
159         fail("unexpected encoded array");
160     free(result);
161
162     json_decref(object);
163     json_decref(array);
164 }
165
166
167 static const char *test_ensure_ascii_data[][2] = {
168     /*
169     { "input", "output" }
170     */
171
172     /* ascii */
173     { "foo", "foo" },
174
175     /* BMP */
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" },
180
181     /* non-BMP */
182     { "clef g: \xf0\x9d\x84\x9e", "clef g: \\ud834\\udd1e" },
183 };
184
185 static void test_ensure_ascii()
186 {
187     int i;
188     int num_tests = sizeof(test_ensure_ascii_data) / sizeof(const char *) / 2;
189
190     for(i = 0; i < num_tests; i++) {
191         json_t *array, *string;
192         const char *input, *output;
193         char *result, *stripped;
194
195         input = test_ensure_ascii_data[i][0];
196         output = test_ensure_ascii_data[i][1];
197
198         array = json_array();
199         string = json_string(input);
200         if(!array || !string)
201             fail("unable to create json values");
202
203         json_array_append(array, string);
204         result = json_dumps(array, JSON_ENSURE_ASCII);
205
206         /* strip leading [" and trailing "] */
207         stripped = &result[2];
208         stripped[strlen(stripped) - 2] = '\0';
209
210         if(strcmp(stripped, output) != 0) {
211             free(result);
212             fail("the result of json_dumps is invalid");
213         }
214         free(result);
215     }
216 }
217
218 int main(void)
219 {
220     test_normal();
221     test_indent();
222     test_compact();
223     test_compact_indent();
224     test_ensure_ascii();
225
226     return 0;
227 }