dump.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright (c) 2009, 2010 Petri Lehtinen <petri@digip.org>
  3. *
  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.
  6. */
  7. #define _GNU_SOURCE
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #include <jansson.h>
  13. #include "jansson_private.h"
  14. #include "strbuffer.h"
  15. #include "utf.h"
  16. #define MAX_INTEGER_STR_LENGTH 100
  17. #define MAX_REAL_STR_LENGTH 100
  18. typedef int (*dump_func)(const char *buffer, int size, void *data);
  19. struct string
  20. {
  21. char *buffer;
  22. int length;
  23. int size;
  24. };
  25. static int dump_to_strbuffer(const char *buffer, int size, void *data)
  26. {
  27. return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
  28. }
  29. static int dump_to_file(const char *buffer, int size, void *data)
  30. {
  31. FILE *dest = (FILE *)data;
  32. if(fwrite(buffer, size, 1, dest) != 1)
  33. return -1;
  34. return 0;
  35. }
  36. /* 256 spaces (the maximum indentation size) */
  37. static char whitespace[] = " ";
  38. static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data)
  39. {
  40. if(JSON_INDENT(flags) > 0)
  41. {
  42. int i, ws_count = JSON_INDENT(flags);
  43. if(dump("\n", 1, data))
  44. return -1;
  45. for(i = 0; i < depth; i++)
  46. {
  47. if(dump(whitespace, ws_count, data))
  48. return -1;
  49. }
  50. }
  51. else if(space && !(flags & JSON_COMPACT))
  52. {
  53. return dump(" ", 1, data);
  54. }
  55. return 0;
  56. }
  57. static int dump_string(const char *str, int ascii, dump_func dump, void *data)
  58. {
  59. const char *pos, *end;
  60. int32_t codepoint;
  61. if(dump("\"", 1, data))
  62. return -1;
  63. end = pos = str;
  64. while(1)
  65. {
  66. const char *text;
  67. char seq[13];
  68. int length;
  69. while(*end)
  70. {
  71. end = utf8_iterate(pos, &codepoint);
  72. if(!end)
  73. return -1;
  74. /* mandatory escape or control char */
  75. if(codepoint == '\\' || codepoint == '"' || codepoint < 0x20)
  76. break;
  77. /* non-ASCII */
  78. if(ascii && codepoint > 0x7F)
  79. break;
  80. pos = end;
  81. }
  82. if(pos != str) {
  83. if(dump(str, pos - str, data))
  84. return -1;
  85. }
  86. if(end == pos)
  87. break;
  88. /* handle \, ", and control codes */
  89. length = 2;
  90. switch(codepoint)
  91. {
  92. case '\\': text = "\\\\"; break;
  93. case '\"': text = "\\\""; break;
  94. case '\b': text = "\\b"; break;
  95. case '\f': text = "\\f"; break;
  96. case '\n': text = "\\n"; break;
  97. case '\r': text = "\\r"; break;
  98. case '\t': text = "\\t"; break;
  99. default:
  100. {
  101. /* codepoint is in BMP */
  102. if(codepoint < 0x10000)
  103. {
  104. sprintf(seq, "\\u%04x", codepoint);
  105. length = 6;
  106. }
  107. /* not in BMP -> construct a UTF-16 surrogate pair */
  108. else
  109. {
  110. int32_t first, last;
  111. codepoint -= 0x10000;
  112. first = 0xD800 | ((codepoint & 0xffc00) >> 10);
  113. last = 0xDC00 | (codepoint & 0x003ff);
  114. sprintf(seq, "\\u%04x\\u%04x", first, last);
  115. length = 12;
  116. }
  117. text = seq;
  118. break;
  119. }
  120. }
  121. if(dump(text, length, data))
  122. return -1;
  123. str = pos = end;
  124. }
  125. return dump("\"", 1, data);
  126. }
  127. static int object_key_compare_keys(const void *key1, const void *key2)
  128. {
  129. return strcmp((*(const object_key_t **)key1)->key,
  130. (*(const object_key_t **)key2)->key);
  131. }
  132. static int object_key_compare_serials(const void *key1, const void *key2)
  133. {
  134. return (*(const object_key_t **)key1)->serial -
  135. (*(const object_key_t **)key2)->serial;
  136. }
  137. static int do_dump(const json_t *json, unsigned long flags, int depth,
  138. dump_func dump, void *data)
  139. {
  140. int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
  141. switch(json_typeof(json)) {
  142. case JSON_NULL:
  143. return dump("null", 4, data);
  144. case JSON_TRUE:
  145. return dump("true", 4, data);
  146. case JSON_FALSE:
  147. return dump("false", 5, data);
  148. case JSON_INTEGER:
  149. {
  150. char buffer[MAX_INTEGER_STR_LENGTH];
  151. int size;
  152. size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
  153. if(size >= MAX_INTEGER_STR_LENGTH)
  154. return -1;
  155. return dump(buffer, size, data);
  156. }
  157. case JSON_REAL:
  158. {
  159. char buffer[MAX_REAL_STR_LENGTH];
  160. int size;
  161. size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
  162. json_real_value(json));
  163. if(size >= MAX_REAL_STR_LENGTH)
  164. return -1;
  165. /* Make sure there's a dot or 'e' in the output. Otherwise
  166. a real is converted to an integer when decoding */
  167. if(strchr(buffer, '.') == NULL &&
  168. strchr(buffer, 'e') == NULL)
  169. {
  170. if(size + 2 >= MAX_REAL_STR_LENGTH) {
  171. /* No space to append ".0" */
  172. return -1;
  173. }
  174. buffer[size] = '.';
  175. buffer[size + 1] = '0';
  176. size += 2;
  177. }
  178. return dump(buffer, size, data);
  179. }
  180. case JSON_STRING:
  181. return dump_string(json_string_value(json), ascii, dump, data);
  182. case JSON_ARRAY:
  183. {
  184. int i;
  185. int n;
  186. json_array_t *array;
  187. /* detect circular references */
  188. array = json_to_array(json);
  189. if(array->visited)
  190. goto array_error;
  191. array->visited = 1;
  192. n = json_array_size(json);
  193. if(dump("[", 1, data))
  194. goto array_error;
  195. if(n == 0) {
  196. array->visited = 0;
  197. return dump("]", 1, data);
  198. }
  199. if(dump_indent(flags, depth + 1, 0, dump, data))
  200. goto array_error;
  201. for(i = 0; i < n; ++i) {
  202. if(do_dump(json_array_get(json, i), flags, depth + 1,
  203. dump, data))
  204. goto array_error;
  205. if(i < n - 1)
  206. {
  207. if(dump(",", 1, data) ||
  208. dump_indent(flags, depth + 1, 1, dump, data))
  209. goto array_error;
  210. }
  211. else
  212. {
  213. if(dump_indent(flags, depth, 0, dump, data))
  214. goto array_error;
  215. }
  216. }
  217. array->visited = 0;
  218. return dump("]", 1, data);
  219. array_error:
  220. array->visited = 0;
  221. return -1;
  222. }
  223. case JSON_OBJECT:
  224. {
  225. json_object_t *object;
  226. void *iter;
  227. const char *separator;
  228. int separator_length;
  229. if(flags & JSON_COMPACT) {
  230. separator = ":";
  231. separator_length = 1;
  232. }
  233. else {
  234. separator = ": ";
  235. separator_length = 2;
  236. }
  237. /* detect circular references */
  238. object = json_to_object(json);
  239. if(object->visited)
  240. goto object_error;
  241. object->visited = 1;
  242. iter = json_object_iter((json_t *)json);
  243. if(dump("{", 1, data))
  244. goto object_error;
  245. if(!iter) {
  246. object->visited = 0;
  247. return dump("}", 1, data);
  248. }
  249. if(dump_indent(flags, depth + 1, 0, dump, data))
  250. goto object_error;
  251. if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
  252. {
  253. const object_key_t **keys;
  254. unsigned int size;
  255. unsigned int i;
  256. int (*cmp_func)(const void *, const void *);
  257. size = json_object_size(json);
  258. keys = malloc(size * sizeof(object_key_t *));
  259. if(!keys)
  260. goto object_error;
  261. i = 0;
  262. while(iter)
  263. {
  264. keys[i] = jsonp_object_iter_fullkey(iter);
  265. iter = json_object_iter_next((json_t *)json, iter);
  266. i++;
  267. }
  268. assert(i == size);
  269. if(flags & JSON_SORT_KEYS)
  270. cmp_func = object_key_compare_keys;
  271. else
  272. cmp_func = object_key_compare_serials;
  273. qsort(keys, size, sizeof(object_key_t *), cmp_func);
  274. for(i = 0; i < size; i++)
  275. {
  276. const char *key;
  277. json_t *value;
  278. key = keys[i]->key;
  279. value = json_object_get(json, key);
  280. assert(value);
  281. dump_string(key, ascii, dump, data);
  282. if(dump(separator, separator_length, data) ||
  283. do_dump(value, flags, depth + 1, dump, data))
  284. {
  285. free(keys);
  286. goto object_error;
  287. }
  288. if(i < size - 1)
  289. {
  290. if(dump(",", 1, data) ||
  291. dump_indent(flags, depth + 1, 1, dump, data))
  292. {
  293. free(keys);
  294. goto object_error;
  295. }
  296. }
  297. else
  298. {
  299. if(dump_indent(flags, depth, 0, dump, data))
  300. {
  301. free(keys);
  302. goto object_error;
  303. }
  304. }
  305. }
  306. free(keys);
  307. }
  308. else
  309. {
  310. /* Don't sort keys */
  311. while(iter)
  312. {
  313. void *next = json_object_iter_next((json_t *)json, iter);
  314. dump_string(json_object_iter_key(iter), ascii, dump, data);
  315. if(dump(separator, separator_length, data) ||
  316. do_dump(json_object_iter_value(iter), flags, depth + 1,
  317. dump, data))
  318. goto object_error;
  319. if(next)
  320. {
  321. if(dump(",", 1, data) ||
  322. dump_indent(flags, depth + 1, 1, dump, data))
  323. goto object_error;
  324. }
  325. else
  326. {
  327. if(dump_indent(flags, depth, 0, dump, data))
  328. goto object_error;
  329. }
  330. iter = next;
  331. }
  332. }
  333. object->visited = 0;
  334. return dump("}", 1, data);
  335. object_error:
  336. object->visited = 0;
  337. return -1;
  338. }
  339. default:
  340. /* not reached */
  341. return -1;
  342. }
  343. }
  344. char *json_dumps(const json_t *json, unsigned long flags)
  345. {
  346. strbuffer_t strbuff;
  347. char *result;
  348. if(!json_is_array(json) && !json_is_object(json))
  349. return NULL;
  350. if(strbuffer_init(&strbuff))
  351. return NULL;
  352. if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
  353. strbuffer_close(&strbuff);
  354. return NULL;
  355. }
  356. result = strdup(strbuffer_value(&strbuff));
  357. strbuffer_close(&strbuff);
  358. return result;
  359. }
  360. int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
  361. {
  362. if(!json_is_array(json) && !json_is_object(json))
  363. return -1;
  364. return do_dump(json, flags, 0, dump_to_file, (void *)output);
  365. }
  366. int json_dump_file(const json_t *json, const char *path, unsigned long flags)
  367. {
  368. int result;
  369. FILE *output = fopen(path, "w");
  370. if(!output)
  371. return -1;
  372. result = json_dumpf(json, output, flags);
  373. fclose(output);
  374. return result;
  375. }