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");
171 test_compact_indent();