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.
16 #include "jansson_private.h"
17 #include "strbuffer.h"
20 #define MAX_INTEGER_STR_LENGTH 100
21 #define MAX_REAL_STR_LENGTH 100
23 typedef int (*dump_func)(const char *buffer, int size, void *data);
32 static int dump_to_strbuffer(const char *buffer, int size, void *data)
34 return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
37 static int dump_to_file(const char *buffer, int size, void *data)
39 FILE *dest = (FILE *)data;
40 if(fwrite(buffer, size, 1, dest) != 1)
45 /* 256 spaces (the maximum indentation size) */
46 static char whitespace[] = " ";
48 static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data)
50 if(JSON_INDENT(flags) > 0)
52 int i, ws_count = JSON_INDENT(flags);
54 if(dump("\n", 1, data))
57 for(i = 0; i < depth; i++)
59 if(dump(whitespace, ws_count, data))
63 else if(space && !(flags & JSON_COMPACT))
65 return dump(" ", 1, data);
70 static int dump_string(const char *str, int ascii, dump_func dump, void *data)
72 const char *pos, *end;
75 if(dump("\"", 1, data))
87 end = utf8_iterate(pos, &codepoint);
91 /* mandatory escape or control char */
92 if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
96 if(ascii && codepoint > 0x7F)
103 if(dump(str, pos - str, data))
110 /* handle \, ", and control codes */
114 case '\\': text = "\\\\"; break;
115 case '\"': text = "\\\""; break;
116 case '\b': text = "\\b"; break;
117 case '\f': text = "\\f"; break;
118 case '\n': text = "\\n"; break;
119 case '\r': text = "\\r"; break;
120 case '\t': text = "\\t"; break;
123 /* codepoint is in BMP */
124 if(codepoint < 0x10000)
126 sprintf(seq, "\\u%04x", codepoint);
130 /* not in BMP -> construct a UTF-16 surrogate pair */
135 codepoint -= 0x10000;
136 first = 0xD800 | ((codepoint & 0xffc00) >> 10);
137 last = 0xDC00 | (codepoint & 0x003ff);
139 sprintf(seq, "\\u%04x\\u%04x", first, last);
148 if(dump(text, length, data))
154 return dump("\"", 1, data);
157 static int object_key_compare_keys(const void *key1, const void *key2)
159 return strcmp((*(const object_key_t **)key1)->key,
160 (*(const object_key_t **)key2)->key);
163 static int object_key_compare_serials(const void *key1, const void *key2)
165 return (*(const object_key_t **)key1)->serial -
166 (*(const object_key_t **)key2)->serial;
169 static int do_dump(const json_t *json, unsigned long flags, int depth,
170 dump_func dump, void *data)
172 int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
174 switch(json_typeof(json)) {
176 return dump("null", 4, data);
179 return dump("true", 4, data);
182 return dump("false", 5, data);
186 char buffer[MAX_INTEGER_STR_LENGTH];
189 size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
190 if(size >= MAX_INTEGER_STR_LENGTH)
193 return dump(buffer, size, data);
198 char buffer[MAX_REAL_STR_LENGTH];
201 size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
202 json_real_value(json));
203 if(size >= MAX_REAL_STR_LENGTH)
206 /* Make sure there's a dot or 'e' in the output. Otherwise
207 a real is converted to an integer when decoding */
208 if(strchr(buffer, '.') == NULL &&
209 strchr(buffer, 'e') == NULL)
211 if(size + 2 >= MAX_REAL_STR_LENGTH) {
212 /* No space to append ".0" */
216 buffer[size + 1] = '0';
220 return dump(buffer, size, data);
224 return dump_string(json_string_value(json), ascii, dump, data);
232 /* detect circular references */
233 array = json_to_array(json);
238 n = json_array_size(json);
240 if(dump("[", 1, data))
243 return dump("]", 1, data);
244 if(dump_indent(flags, depth + 1, 0, dump, data))
247 for(i = 0; i < n; ++i) {
248 if(do_dump(json_array_get(json, i), flags, depth + 1,
254 if(dump(",", 1, data) ||
255 dump_indent(flags, depth + 1, 1, dump, data))
260 if(dump_indent(flags, depth, 0, dump, data))
266 return dump("]", 1, data);
271 json_object_t *object;
273 const char *separator;
274 int separator_length;
276 if(flags & JSON_COMPACT) {
278 separator_length = 1;
282 separator_length = 2;
285 /* detect circular references */
286 object = json_to_object(json);
291 iter = json_object_iter((json_t *)json);
293 if(dump("{", 1, data))
296 return dump("}", 1, data);
297 if(dump_indent(flags, depth + 1, 0, dump, data))
300 if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
302 const object_key_t **keys;
305 int (*cmp_func)(const void *, const void *);
307 size = json_object_size(json);
308 keys = malloc(size * sizeof(object_key_t *));
315 keys[i] = jsonp_object_iter_fullkey(iter);
316 iter = json_object_iter_next((json_t *)json, iter);
321 if(flags & JSON_SORT_KEYS)
322 cmp_func = object_key_compare_keys;
324 cmp_func = object_key_compare_serials;
326 qsort(keys, size, sizeof(object_key_t *), cmp_func);
328 for(i = 0; i < size; i++)
334 value = json_object_get(json, key);
337 dump_string(key, ascii, dump, data);
338 if(dump(separator, separator_length, data) ||
339 do_dump(value, flags, depth + 1, dump, data))
347 if(dump(",", 1, data) ||
348 dump_indent(flags, depth + 1, 1, dump, data))
356 if(dump_indent(flags, depth, 0, dump, data))
368 /* Don't sort keys */
372 void *next = json_object_iter_next((json_t *)json, iter);
374 dump_string(json_object_iter_key(iter), ascii, dump, data);
375 if(dump(separator, separator_length, data) ||
376 do_dump(json_object_iter_value(iter), flags, depth + 1,
382 if(dump(",", 1, data) ||
383 dump_indent(flags, depth + 1, 1, dump, data))
388 if(dump_indent(flags, depth, 0, dump, data))
397 return dump("}", 1, data);
407 char *json_dumps(const json_t *json, unsigned long flags)
412 if(!json_is_array(json) && !json_is_object(json))
415 if(strbuffer_init(&strbuff))
418 if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
419 strbuffer_close(&strbuff);
423 result = strdup(strbuffer_value(&strbuff));
424 strbuffer_close(&strbuff);
429 int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
431 if(!json_is_array(json) && !json_is_object(json))
434 return do_dump(json, flags, 0, dump_to_file, (void *)output);
437 int json_dump_file(const json_t *json, const char *path, unsigned long flags)
441 FILE *output = fopen(path, "w");
445 result = json_dumpf(json, output, flags);