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, json_dump_callback_t dump, void *data, size_t flags)
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 < 0x20)
91 if((flags & JSON_ESCAPE_SLASH) && codepoint == '/')
95 if((flags & JSON_ENSURE_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;
120 case '/': text = "\\/"; 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 struct object_key *)key1)->key,
160 ((const struct object_key *)key2)->key);
163 static int object_key_compare_serials(const void *key1, const void *key2)
165 size_t a = ((const struct object_key *)key1)->serial;
166 size_t b = ((const struct object_key *)key2)->serial;
168 return a < b ? -1 : a == b ? 0 : 1;
171 static int do_dump(const json_t *json, size_t flags, int depth,
172 json_dump_callback_t dump, void *data)
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,
190 "%" JSON_INTEGER_FORMAT,
191 json_integer_value(json));
192 if(size < 0 || size >= MAX_INTEGER_STR_LENGTH)
195 return dump(buffer, size, data);
200 char buffer[MAX_REAL_STR_LENGTH];
202 double value = json_real_value(json);
204 size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value);
208 return dump(buffer, size, data);
212 return dump_string(json_string_value(json), dump, data, flags);
220 /* detect circular references */
221 array = json_to_array(json);
226 n = json_array_size(json);
228 if(dump("[", 1, data))
232 return dump("]", 1, data);
234 if(dump_indent(flags, depth + 1, 0, dump, data))
237 for(i = 0; i < n; ++i) {
238 if(do_dump(json_array_get(json, i), flags, depth + 1,
244 if(dump(",", 1, data) ||
245 dump_indent(flags, depth + 1, 1, dump, data))
250 if(dump_indent(flags, depth, 0, dump, data))
256 return dump("]", 1, data);
265 json_object_t *object;
267 const char *separator;
268 int separator_length;
270 if(flags & JSON_COMPACT) {
272 separator_length = 1;
276 separator_length = 2;
279 /* detect circular references */
280 object = json_to_object(json);
285 iter = json_object_iter((json_t *)json);
287 if(dump("{", 1, data))
291 return dump("}", 1, data);
293 if(dump_indent(flags, depth + 1, 0, dump, data))
296 if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
298 struct object_key *keys;
300 int (*cmp_func)(const void *, const void *);
302 size = json_object_size(json);
303 keys = jsonp_malloc(size * sizeof(struct object_key));
310 keys[i].serial = hashtable_iter_serial(iter);
311 keys[i].key = json_object_iter_key(iter);
312 iter = json_object_iter_next((json_t *)json, iter);
317 if(flags & JSON_SORT_KEYS)
318 cmp_func = object_key_compare_keys;
320 cmp_func = object_key_compare_serials;
322 qsort(keys, size, sizeof(struct object_key), cmp_func);
324 for(i = 0; i < size; i++)
330 value = json_object_get(json, key);
333 dump_string(key, dump, data, flags);
334 if(dump(separator, separator_length, data) ||
335 do_dump(value, flags, depth + 1, dump, data))
343 if(dump(",", 1, data) ||
344 dump_indent(flags, depth + 1, 1, dump, data))
352 if(dump_indent(flags, depth, 0, dump, data))
364 /* Don't sort keys */
368 void *next = json_object_iter_next((json_t *)json, iter);
370 dump_string(json_object_iter_key(iter), dump, data, flags);
371 if(dump(separator, separator_length, data) ||
372 do_dump(json_object_iter_value(iter), flags, depth + 1,
378 if(dump(",", 1, data) ||
379 dump_indent(flags, depth + 1, 1, dump, data))
384 if(dump_indent(flags, depth, 0, dump, data))
393 return dump("}", 1, data);
406 char *json_dumps(const json_t *json, size_t flags)
411 if(strbuffer_init(&strbuff))
414 if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
417 result = jsonp_strdup(strbuffer_value(&strbuff));
419 strbuffer_close(&strbuff);
423 int json_dumpf(const json_t *json, FILE *output, size_t flags)
425 return json_dump_callback(json, dump_to_file, (void *)output, flags);
428 int json_dump_file(const json_t *json, const char *path, size_t flags)
432 FILE *output = fopen(path, "w");
436 result = json_dumpf(json, output, flags);
442 int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
444 if(!(flags & JSON_ENCODE_ANY)) {
445 if(!json_is_array(json) && !json_is_object(json))
449 return do_dump(json, flags, 0, callback, data);