2 * Copyright (c) 2009-2011 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
29 static int dump_to_strbuffer(const char *buffer, size_t size, void *data)
31 return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
34 static int dump_to_file(const char *buffer, size_t size, void *data)
36 FILE *dest = (FILE *)data;
37 if(fwrite(buffer, size, 1, dest) != 1)
42 /* 32 spaces (the maximum indentation size) */
43 static char whitespace[] = " ";
45 static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
47 if(JSON_INDENT(flags) > 0)
49 int i, ws_count = JSON_INDENT(flags);
51 if(dump("\n", 1, data))
54 for(i = 0; i < depth; i++)
56 if(dump(whitespace, ws_count, data))
60 else if(space && !(flags & JSON_COMPACT))
62 return dump(" ", 1, data);
67 static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data)
69 const char *pos, *end;
72 if(dump("\"", 1, data))
84 end = utf8_iterate(pos, &codepoint);
88 /* mandatory escape or control char */
89 if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
93 if(ascii && codepoint > 0x7F)
100 if(dump(str, pos - str, data))
107 /* handle \, ", and control codes */
111 case '\\': text = "\\\\"; break;
112 case '\"': text = "\\\""; break;
113 case '\b': text = "\\b"; break;
114 case '\f': text = "\\f"; break;
115 case '\n': text = "\\n"; break;
116 case '\r': text = "\\r"; break;
117 case '\t': text = "\\t"; break;
120 /* codepoint is in BMP */
121 if(codepoint < 0x10000)
123 sprintf(seq, "\\u%04x", codepoint);
127 /* not in BMP -> construct a UTF-16 surrogate pair */
132 codepoint -= 0x10000;
133 first = 0xD800 | ((codepoint & 0xffc00) >> 10);
134 last = 0xDC00 | (codepoint & 0x003ff);
136 sprintf(seq, "\\u%04x\\u%04x", first, last);
145 if(dump(text, length, data))
151 return dump("\"", 1, data);
154 static int object_key_compare_keys(const void *key1, const void *key2)
156 return strcmp((*(const object_key_t **)key1)->key,
157 (*(const object_key_t **)key2)->key);
160 static int object_key_compare_serials(const void *key1, const void *key2)
162 return (*(const object_key_t **)key1)->serial -
163 (*(const object_key_t **)key2)->serial;
166 static int do_dump(const json_t *json, size_t flags, int depth,
167 json_dump_callback_t dump, void *data)
169 int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
171 switch(json_typeof(json)) {
173 return dump("null", 4, data);
176 return dump("true", 4, data);
179 return dump("false", 5, data);
183 char buffer[MAX_INTEGER_STR_LENGTH];
186 size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
187 "%" JSON_INTEGER_FORMAT,
188 json_integer_value(json));
189 if(size >= MAX_INTEGER_STR_LENGTH)
192 return dump(buffer, size, data);
197 char buffer[MAX_REAL_STR_LENGTH];
199 double value = json_real_value(json);
201 size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value);
205 return dump(buffer, size, data);
209 return dump_string(json_string_value(json), ascii, dump, data);
217 /* detect circular references */
218 array = json_to_array(json);
223 n = json_array_size(json);
225 if(dump("[", 1, data))
229 return dump("]", 1, data);
231 if(dump_indent(flags, depth + 1, 0, dump, data))
234 for(i = 0; i < n; ++i) {
235 if(do_dump(json_array_get(json, i), flags, depth + 1,
241 if(dump(",", 1, data) ||
242 dump_indent(flags, depth + 1, 1, dump, data))
247 if(dump_indent(flags, depth, 0, dump, data))
253 return dump("]", 1, data);
262 json_object_t *object;
264 const char *separator;
265 int separator_length;
267 if(flags & JSON_COMPACT) {
269 separator_length = 1;
273 separator_length = 2;
276 /* detect circular references */
277 object = json_to_object(json);
282 iter = json_object_iter((json_t *)json);
284 if(dump("{", 1, data))
288 return dump("}", 1, data);
290 if(dump_indent(flags, depth + 1, 0, dump, data))
293 if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
295 const object_key_t **keys;
297 int (*cmp_func)(const void *, const void *);
299 size = json_object_size(json);
300 keys = jsonp_malloc(size * sizeof(object_key_t *));
307 keys[i] = jsonp_object_iter_fullkey(iter);
308 iter = json_object_iter_next((json_t *)json, iter);
313 if(flags & JSON_SORT_KEYS)
314 cmp_func = object_key_compare_keys;
316 cmp_func = object_key_compare_serials;
318 qsort(keys, size, sizeof(object_key_t *), cmp_func);
320 for(i = 0; i < size; i++)
326 value = json_object_get(json, key);
329 dump_string(key, ascii, dump, data);
330 if(dump(separator, separator_length, data) ||
331 do_dump(value, flags, depth + 1, dump, data))
339 if(dump(",", 1, data) ||
340 dump_indent(flags, depth + 1, 1, dump, data))
348 if(dump_indent(flags, depth, 0, dump, data))
360 /* Don't sort keys */
364 void *next = json_object_iter_next((json_t *)json, iter);
366 dump_string(json_object_iter_key(iter), ascii, dump, data);
367 if(dump(separator, separator_length, data) ||
368 do_dump(json_object_iter_value(iter), flags, depth + 1,
374 if(dump(",", 1, data) ||
375 dump_indent(flags, depth + 1, 1, dump, data))
380 if(dump_indent(flags, depth, 0, dump, data))
389 return dump("}", 1, data);
402 char *json_dumps(const json_t *json, size_t flags)
407 if(strbuffer_init(&strbuff))
410 if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
413 result = jsonp_strdup(strbuffer_value(&strbuff));
415 strbuffer_close(&strbuff);
419 int json_dumpf(const json_t *json, FILE *output, size_t flags)
421 return json_dump_callback(json, dump_to_file, (void *)output, flags);
424 int json_dump_file(const json_t *json, const char *path, size_t flags)
428 FILE *output = fopen(path, "w");
432 result = json_dumpf(json, output, flags);
438 int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
440 if(!(flags & JSON_ENCODE_ANY)) {
441 if(!json_is_array(json) && !json_is_object(json))
445 return do_dump(json, flags, 0, callback, data);