2 * Copyright (c) 2009, 2010 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.
15 #include "jansson_private.h"
16 #include "strbuffer.h"
19 #define MAX_INTEGER_STR_LENGTH 100
20 #define MAX_REAL_STR_LENGTH 100
22 typedef int (*dump_func)(const char *buffer, int size, void *data);
31 static int dump_to_strbuffer(const char *buffer, int size, void *data)
33 return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
36 static int dump_to_file(const char *buffer, int size, void *data)
38 FILE *dest = (FILE *)data;
39 if(fwrite(buffer, size, 1, dest) != 1)
44 /* 256 spaces (the maximum indentation size) */
45 static char whitespace[] = " ";
47 static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data)
49 if(JSON_INDENT(flags) > 0)
51 int i, ws_count = JSON_INDENT(flags);
53 if(dump("\n", 1, data))
56 for(i = 0; i < depth; i++)
58 if(dump(whitespace, ws_count, data))
62 else if(space && !(flags & JSON_COMPACT))
64 return dump(" ", 1, data);
69 static int dump_string(const char *str, int ascii, dump_func dump, void *data)
71 const char *pos, *end;
74 if(dump("\"", 1, data))
86 end = utf8_iterate(pos, &codepoint);
90 /* mandatory escape or control char */
91 if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
95 if(ascii && codepoint > 0x7F)
102 if(dump(str, pos - str, data))
109 /* handle \, ", and control codes */
113 case '\\': text = "\\\\"; break;
114 case '\"': text = "\\\""; break;
115 case '\b': text = "\\b"; break;
116 case '\f': text = "\\f"; break;
117 case '\n': text = "\\n"; break;
118 case '\r': text = "\\r"; break;
119 case '\t': text = "\\t"; break;
122 /* codepoint is in BMP */
123 if(codepoint < 0x10000)
125 sprintf(seq, "\\u%04x", codepoint);
129 /* not in BMP -> construct a UTF-16 surrogate pair */
134 codepoint -= 0x10000;
135 first = 0xD800 | ((codepoint & 0xffc00) >> 10);
136 last = 0xDC00 | (codepoint & 0x003ff);
138 sprintf(seq, "\\u%04x\\u%04x", first, last);
147 if(dump(text, length, data))
153 return dump("\"", 1, data);
156 static int object_key_compare_keys(const void *key1, const void *key2)
158 return strcmp((*(const object_key_t **)key1)->key,
159 (*(const object_key_t **)key2)->key);
162 static int object_key_compare_serials(const void *key1, const void *key2)
164 return (*(const object_key_t **)key1)->serial -
165 (*(const object_key_t **)key2)->serial;
168 static int do_dump(const json_t *json, size_t flags, int depth,
169 dump_func dump, void *data)
171 int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
173 switch(json_typeof(json)) {
175 return dump("null", 4, data);
178 return dump("true", 4, data);
181 return dump("false", 5, data);
185 char buffer[MAX_INTEGER_STR_LENGTH];
188 size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%li", json_integer_value(json));
189 if(size >= MAX_INTEGER_STR_LENGTH)
192 return dump(buffer, size, data);
197 char buffer[MAX_REAL_STR_LENGTH];
200 size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
201 json_real_value(json));
202 if(size >= MAX_REAL_STR_LENGTH)
205 /* Make sure there's a dot or 'e' in the output. Otherwise
206 a real is converted to an integer when decoding */
207 if(strchr(buffer, '.') == NULL &&
208 strchr(buffer, 'e') == NULL)
210 if(size + 2 >= MAX_REAL_STR_LENGTH) {
211 /* No space to append ".0" */
215 buffer[size + 1] = '0';
219 return dump(buffer, size, data);
223 return dump_string(json_string_value(json), ascii, dump, data);
231 /* detect circular references */
232 array = json_to_array(json);
237 n = json_array_size(json);
239 if(dump("[", 1, data))
243 return dump("]", 1, data);
245 if(dump_indent(flags, depth + 1, 0, dump, data))
248 for(i = 0; i < n; ++i) {
249 if(do_dump(json_array_get(json, i), flags, depth + 1,
255 if(dump(",", 1, data) ||
256 dump_indent(flags, depth + 1, 1, dump, data))
261 if(dump_indent(flags, depth, 0, dump, data))
267 return dump("]", 1, data);
276 json_object_t *object;
278 const char *separator;
279 int separator_length;
281 if(flags & JSON_COMPACT) {
283 separator_length = 1;
287 separator_length = 2;
290 /* detect circular references */
291 object = json_to_object(json);
296 iter = json_object_iter((json_t *)json);
298 if(dump("{", 1, data))
302 return dump("}", 1, data);
304 if(dump_indent(flags, depth + 1, 0, dump, data))
307 if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
309 const object_key_t **keys;
311 int (*cmp_func)(const void *, const void *);
313 size = json_object_size(json);
314 keys = malloc(size * sizeof(object_key_t *));
321 keys[i] = jsonp_object_iter_fullkey(iter);
322 iter = json_object_iter_next((json_t *)json, iter);
327 if(flags & JSON_SORT_KEYS)
328 cmp_func = object_key_compare_keys;
330 cmp_func = object_key_compare_serials;
332 qsort(keys, size, sizeof(object_key_t *), cmp_func);
334 for(i = 0; i < size; i++)
340 value = json_object_get(json, key);
343 dump_string(key, ascii, dump, data);
344 if(dump(separator, separator_length, data) ||
345 do_dump(value, flags, depth + 1, dump, data))
353 if(dump(",", 1, data) ||
354 dump_indent(flags, depth + 1, 1, dump, data))
362 if(dump_indent(flags, depth, 0, dump, data))
374 /* Don't sort keys */
378 void *next = json_object_iter_next((json_t *)json, iter);
380 dump_string(json_object_iter_key(iter), ascii, dump, data);
381 if(dump(separator, separator_length, data) ||
382 do_dump(json_object_iter_value(iter), flags, depth + 1,
388 if(dump(",", 1, data) ||
389 dump_indent(flags, depth + 1, 1, dump, data))
394 if(dump_indent(flags, depth, 0, dump, data))
403 return dump("}", 1, data);
417 char *json_dumps(const json_t *json, size_t flags)
422 if(!json_is_array(json) && !json_is_object(json))
425 if(strbuffer_init(&strbuff))
428 if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
429 strbuffer_close(&strbuff);
433 result = strdup(strbuffer_value(&strbuff));
434 strbuffer_close(&strbuff);
439 int json_dumpf(const json_t *json, FILE *output, size_t flags)
441 if(!json_is_array(json) && !json_is_object(json))
444 return do_dump(json, flags, 0, dump_to_file, (void *)output);
447 int json_dump_file(const json_t *json, const char *path, size_t flags)
451 FILE *output = fopen(path, "w");
455 result = json_dumpf(json, output, flags);