2 * Copyright (c) 2009-2012 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
27 static int dump_to_strbuffer(const char *buffer, size_t size, void *data)
29 return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
32 static int dump_to_file(const char *buffer, size_t size, void *data)
34 FILE *dest = (FILE *)data;
35 if(fwrite(buffer, size, 1, dest) != 1)
40 /* 32 spaces (the maximum indentation size) */
41 static char whitespace[] = " ";
43 static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
45 if(JSON_INDENT(flags) > 0)
47 int i, ws_count = JSON_INDENT(flags);
49 if(dump("\n", 1, data))
52 for(i = 0; i < depth; i++)
54 if(dump(whitespace, ws_count, data))
58 else if(space && !(flags & JSON_COMPACT))
60 return dump(" ", 1, data);
65 static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data)
67 const char *pos, *end;
70 if(dump("\"", 1, data))
82 end = utf8_iterate(pos, &codepoint);
86 /* mandatory escape or control char */
87 if(codepoint == '\\' || codepoint == '/' || codepoint == '"' || codepoint < 0x20)
91 if(ascii && codepoint > 0x7F)
98 if(dump(str, pos - str, data))
105 /* handle \, /, ", and control codes */
109 case '\\': text = "\\\\"; break;
110 case '/': text = "\\/"; break;
111 case '\"': text = "\\\""; break;
112 case '\b': text = "\\b"; break;
113 case '\f': text = "\\f"; break;
114 case '\n': text = "\\n"; break;
115 case '\r': text = "\\r"; break;
116 case '\t': text = "\\t"; break;
119 /* codepoint is in BMP */
120 if(codepoint < 0x10000)
122 sprintf(seq, "\\u%04x", codepoint);
126 /* not in BMP -> construct a UTF-16 surrogate pair */
131 codepoint -= 0x10000;
132 first = 0xD800 | ((codepoint & 0xffc00) >> 10);
133 last = 0xDC00 | (codepoint & 0x003ff);
135 sprintf(seq, "\\u%04x\\u%04x", first, last);
144 if(dump(text, length, data))
150 return dump("\"", 1, data);
153 static int object_key_compare_keys(const void *key1, const void *key2)
155 return strcmp(((const struct object_key *)key1)->key,
156 ((const struct object_key *)key2)->key);
159 static int object_key_compare_serials(const void *key1, const void *key2)
161 size_t a = ((const struct object_key *)key1)->serial;
162 size_t b = ((const struct object_key *)key2)->serial;
164 return a < b ? -1 : a == b ? 0 : 1;
167 static int do_dump(const json_t *json, size_t flags, int depth,
168 json_dump_callback_t dump, void *data)
170 int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
172 switch(json_typeof(json)) {
174 return dump("null", 4, data);
177 return dump("true", 4, data);
180 return dump("false", 5, data);
184 char buffer[MAX_INTEGER_STR_LENGTH];
187 size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
188 "%" JSON_INTEGER_FORMAT,
189 json_integer_value(json));
190 if(size >= MAX_INTEGER_STR_LENGTH)
193 return dump(buffer, size, data);
198 char buffer[MAX_REAL_STR_LENGTH];
200 double value = json_real_value(json);
202 size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value);
206 return dump(buffer, size, data);
210 return dump_string(json_string_value(json), ascii, dump, data);
218 /* detect circular references */
219 array = json_to_array(json);
224 n = json_array_size(json);
226 if(dump("[", 1, data))
230 return dump("]", 1, data);
232 if(dump_indent(flags, depth + 1, 0, dump, data))
235 for(i = 0; i < n; ++i) {
236 if(do_dump(json_array_get(json, i), flags, depth + 1,
242 if(dump(",", 1, data) ||
243 dump_indent(flags, depth + 1, 1, dump, data))
248 if(dump_indent(flags, depth, 0, dump, data))
254 return dump("]", 1, data);
263 json_object_t *object;
265 const char *separator;
266 int separator_length;
268 if(flags & JSON_COMPACT) {
270 separator_length = 1;
274 separator_length = 2;
277 /* detect circular references */
278 object = json_to_object(json);
283 iter = json_object_iter((json_t *)json);
285 if(dump("{", 1, data))
289 return dump("}", 1, data);
291 if(dump_indent(flags, depth + 1, 0, dump, data))
294 if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
296 struct object_key *keys;
298 int (*cmp_func)(const void *, const void *);
300 size = json_object_size(json);
301 keys = jsonp_malloc(size * sizeof(struct object_key));
308 keys[i].serial = hashtable_iter_serial(iter);
309 keys[i].key = json_object_iter_key(iter);
310 iter = json_object_iter_next((json_t *)json, iter);
315 if(flags & JSON_SORT_KEYS)
316 cmp_func = object_key_compare_keys;
318 cmp_func = object_key_compare_serials;
320 qsort(keys, size, sizeof(struct object_key), cmp_func);
322 for(i = 0; i < size; i++)
328 value = json_object_get(json, key);
331 dump_string(key, ascii, dump, data);
332 if(dump(separator, separator_length, data) ||
333 do_dump(value, flags, depth + 1, dump, data))
341 if(dump(",", 1, data) ||
342 dump_indent(flags, depth + 1, 1, dump, data))
350 if(dump_indent(flags, depth, 0, dump, data))
362 /* Don't sort keys */
366 void *next = json_object_iter_next((json_t *)json, iter);
368 dump_string(json_object_iter_key(iter), ascii, dump, data);
369 if(dump(separator, separator_length, data) ||
370 do_dump(json_object_iter_value(iter), flags, depth + 1,
376 if(dump(",", 1, data) ||
377 dump_indent(flags, depth + 1, 1, dump, data))
382 if(dump_indent(flags, depth, 0, dump, data))
391 return dump("}", 1, data);
404 char *json_dumps(const json_t *json, size_t flags)
409 if(strbuffer_init(&strbuff))
412 if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
415 result = jsonp_strdup(strbuffer_value(&strbuff));
417 strbuffer_close(&strbuff);
421 int json_dumpf(const json_t *json, FILE *output, size_t flags)
423 return json_dump_callback(json, dump_to_file, (void *)output, flags);
426 int json_dump_file(const json_t *json, const char *path, size_t flags)
430 FILE *output = fopen(path, "w");
434 result = json_dumpf(json, output, flags);
440 int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
442 if(!(flags & JSON_ENCODE_ANY)) {
443 if(!json_is_array(json) && !json_is_object(json))
447 return do_dump(json, flags, 0, callback, data);