xs_json.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  2. #ifndef _XS_JSON_H
  3. #define _XS_JSON_H
  4. int xs_json_dump(const xs_val *data, int indent, FILE *f);
  5. xs_str *xs_json_dumps(const xs_val *data, int indent);
  6. xs_val *xs_json_load(FILE *f);
  7. xs_val *xs_json_loads(const xs_str *json);
  8. xstype xs_json_load_type(FILE *f);
  9. int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c);
  10. int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c);
  11. xs_list *xs_json_load_array(FILE *f);
  12. xs_dict *xs_json_load_object(FILE *f);
  13. #ifdef XS_IMPLEMENTATION
  14. /** IMPLEMENTATION **/
  15. /** JSON dumps **/
  16. static void _xs_json_dump_str(const char *data, FILE *f)
  17. /* dumps a string in JSON format */
  18. {
  19. unsigned char c;
  20. fputs("\"", f);
  21. while ((c = *data)) {
  22. if (c == '\n')
  23. fputs("\\n", f);
  24. else
  25. if (c == '\r')
  26. fputs("\\r", f);
  27. else
  28. if (c == '\t')
  29. fputs("\\t", f);
  30. else
  31. if (c == '\\')
  32. fputs("\\\\", f);
  33. else
  34. if (c == '"')
  35. fputs("\\\"", f);
  36. else
  37. if (c < 32)
  38. fprintf(f, "\\u%04x", (unsigned int) c);
  39. else
  40. fputc(c, f);
  41. data++;
  42. }
  43. fputs("\"", f);
  44. }
  45. static void _xs_json_indent(int level, int indent, FILE *f)
  46. /* adds indentation */
  47. {
  48. if (indent) {
  49. int n;
  50. fputc('\n', f);
  51. for (n = 0; n < level * indent; n++)
  52. fputc(' ', f);
  53. }
  54. }
  55. static void _xs_json_dump(const xs_val *data, int level, int indent, FILE *f)
  56. /* dumps partial data as JSON */
  57. {
  58. int c = 0;
  59. const xs_val *v;
  60. switch (xs_type(data)) {
  61. case XSTYPE_NULL:
  62. fputs("null", f);
  63. break;
  64. case XSTYPE_TRUE:
  65. fputs("true", f);
  66. break;
  67. case XSTYPE_FALSE:
  68. fputs("false", f);
  69. break;
  70. case XSTYPE_NUMBER:
  71. fputs(xs_number_str(data), f);
  72. break;
  73. case XSTYPE_LIST:
  74. fputc('[', f);
  75. xs_list_foreach(data, v) {
  76. if (c != 0)
  77. fputc(',', f);
  78. _xs_json_indent(level + 1, indent, f);
  79. _xs_json_dump(v, level + 1, indent, f);
  80. c++;
  81. }
  82. _xs_json_indent(level, indent, f);
  83. fputc(']', f);
  84. break;
  85. case XSTYPE_DICT:
  86. fputc('{', f);
  87. const xs_str *k;
  88. xs_dict_foreach(data, k, v) {
  89. if (c != 0)
  90. fputc(',', f);
  91. _xs_json_indent(level + 1, indent, f);
  92. _xs_json_dump_str(k, f);
  93. fputc(':', f);
  94. if (indent)
  95. fputc(' ', f);
  96. _xs_json_dump(v, level + 1, indent, f);
  97. c++;
  98. }
  99. _xs_json_indent(level, indent, f);
  100. fputc('}', f);
  101. break;
  102. case XSTYPE_STRING:
  103. _xs_json_dump_str(data, f);
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. xs_str *xs_json_dumps(const xs_val *data, int indent)
  110. /* dumps data as a JSON string */
  111. {
  112. xs_str *s = NULL;
  113. size_t sz;
  114. FILE *f;
  115. if ((f = open_memstream(&s, &sz)) != NULL) {
  116. int r = xs_json_dump(data, indent, f);
  117. fclose(f);
  118. if (!r)
  119. s = xs_free(s);
  120. }
  121. return s;
  122. }
  123. int xs_json_dump(const xs_val *data, int indent, FILE *f)
  124. /* dumps data into a file as JSON */
  125. {
  126. xstype t = xs_type(data);
  127. if (t == XSTYPE_LIST || t == XSTYPE_DICT) {
  128. _xs_json_dump(data, 0, indent, f);
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. /** JSON loads **/
  134. typedef enum {
  135. JS_ERROR = -1,
  136. JS_INCOMPLETE,
  137. JS_OCURLY,
  138. JS_OBRACK,
  139. JS_CCURLY,
  140. JS_CBRACK,
  141. JS_COMMA,
  142. JS_COLON,
  143. JS_VALUE,
  144. JS_STRING,
  145. JS_NUMBER,
  146. JS_TRUE,
  147. JS_FALSE,
  148. JS_NULL,
  149. JS_ARRAY,
  150. JS_OBJECT
  151. } js_type;
  152. static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
  153. {
  154. int c;
  155. xs_val *v = NULL;
  156. int offset;
  157. *t = JS_ERROR;
  158. /* skip blanks */
  159. while ((c = fgetc(f)) == ' ' || c == '\t' || c == '\n' || c == '\r');
  160. if (c == '{')
  161. *t = JS_OCURLY;
  162. else
  163. if (c == '}')
  164. *t = JS_CCURLY;
  165. else
  166. if (c == '[')
  167. *t = JS_OBRACK;
  168. else
  169. if (c == ']')
  170. *t = JS_CBRACK;
  171. else
  172. if (c == ',')
  173. *t = JS_COMMA;
  174. else
  175. if (c == ':')
  176. *t = JS_COLON;
  177. else
  178. if (c == '"') {
  179. *t = JS_STRING;
  180. v = xs_str_new(NULL);
  181. offset = 0;
  182. while ((c = fgetc(f)) != '"' && c != EOF && *t != JS_ERROR) {
  183. if (c == '\\') {
  184. unsigned int cp = fgetc(f);
  185. switch (cp) {
  186. case 'n': cp = '\n'; break;
  187. case 'r': cp = '\r'; break;
  188. case 't': cp = '\t'; break;
  189. case 'u': /* Unicode codepoint as an hex char */
  190. if (fscanf(f, "%04x", &cp) != 1) {
  191. *t = JS_ERROR;
  192. break;
  193. }
  194. if (xs_is_surrogate(cp)) {
  195. /* \u must follow */
  196. if (fgetc(f) != '\\' || fgetc(f) != 'u') {
  197. *t = JS_ERROR;
  198. break;
  199. }
  200. unsigned int p2;
  201. if (fscanf(f, "%04x", &p2) != 1) {
  202. *t = JS_ERROR;
  203. break;
  204. }
  205. cp = xs_surrogate_dec(cp, p2);
  206. }
  207. /* replace dangerous control codes with their visual representations */
  208. if (cp < ' ' && !strchr("\r\n\t", cp))
  209. cp += 0x2400;
  210. break;
  211. }
  212. v = xs_utf8_insert(v, cp, &offset);
  213. }
  214. else {
  215. char cc = c;
  216. v = xs_insert_m(v, offset, &cc, 1);
  217. offset++;
  218. }
  219. }
  220. if (c == EOF)
  221. *t = JS_ERROR;
  222. }
  223. else
  224. if (c == '-' || (c >= '0' && c <= '9') || c == '.') {
  225. double d;
  226. ungetc(c, f);
  227. if (fscanf(f, "%lf", &d) == 1) {
  228. *t = JS_NUMBER;
  229. v = xs_number_new(d);
  230. }
  231. }
  232. else
  233. if (c == 't') {
  234. if (fgetc(f) == 'r' && fgetc(f) == 'u' && fgetc(f) == 'e') {
  235. *t = JS_TRUE;
  236. v = xs_val_new(XSTYPE_TRUE);
  237. }
  238. }
  239. else
  240. if (c == 'f') {
  241. if (fgetc(f) == 'a' && fgetc(f) == 'l' &&
  242. fgetc(f) == 's' && fgetc(f) == 'e') {
  243. *t = JS_FALSE;
  244. v = xs_val_new(XSTYPE_FALSE);
  245. }
  246. }
  247. else
  248. if (c == 'n') {
  249. if (fgetc(f) == 'u' && fgetc(f) == 'l' && fgetc(f) == 'l') {
  250. *t = JS_NULL;
  251. v = xs_val_new(XSTYPE_NULL);
  252. }
  253. }
  254. if (*t == JS_ERROR)
  255. v = xs_free(v);
  256. return v;
  257. }
  258. int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
  259. /* loads the next scalar value from the JSON stream */
  260. /* if the value ahead is compound, value is NULL and pt is set */
  261. {
  262. js_type t;
  263. *value = _xs_json_load_lexer(f, &t);
  264. if (t == JS_ERROR)
  265. return -1;
  266. if (t == JS_CBRACK)
  267. return 0;
  268. if (*c > 0) {
  269. if (t == JS_COMMA)
  270. *value = _xs_json_load_lexer(f, &t);
  271. else
  272. return -1;
  273. }
  274. if (*value == NULL) {
  275. /* possible compound type ahead */
  276. if (t == JS_OBRACK)
  277. *pt = XSTYPE_LIST;
  278. else
  279. if (t == JS_OCURLY)
  280. *pt = XSTYPE_DICT;
  281. else
  282. return -1;
  283. }
  284. *c = *c + 1;
  285. return 1;
  286. }
  287. xs_list *xs_json_load_array(FILE *f)
  288. /* loads a full JSON array (after the initial OBRACK) */
  289. {
  290. xstype t;
  291. xs_list *l = xs_list_new();
  292. int c = 0;
  293. for (;;) {
  294. xs *v = NULL;
  295. int r = xs_json_load_array_iter(f, &v, &t, &c);
  296. if (r == -1)
  297. l = xs_free(l);
  298. if (r == 1) {
  299. /* partial load? */
  300. if (v == NULL) {
  301. if (t == XSTYPE_LIST)
  302. v = xs_json_load_array(f);
  303. else
  304. if (t == XSTYPE_DICT)
  305. v = xs_json_load_object(f);
  306. }
  307. /* still null? fail */
  308. if (v == NULL) {
  309. l = xs_free(l);
  310. break;
  311. }
  312. l = xs_list_append(l, v);
  313. }
  314. else
  315. break;
  316. }
  317. return l;
  318. }
  319. int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c)
  320. /* loads the next key and scalar value from the JSON stream */
  321. /* if the value ahead is compound, value is NULL and pt is set */
  322. {
  323. js_type t;
  324. *key = _xs_json_load_lexer(f, &t);
  325. if (t == JS_ERROR)
  326. return -1;
  327. if (t == JS_CCURLY)
  328. return 0;
  329. if (*c > 0) {
  330. if (t == JS_COMMA)
  331. *key = _xs_json_load_lexer(f, &t);
  332. else
  333. return -1;
  334. }
  335. if (t != JS_STRING)
  336. return -1;
  337. xs_free(_xs_json_load_lexer(f, &t));
  338. if (t != JS_COLON)
  339. return -1;
  340. *value = _xs_json_load_lexer(f, &t);
  341. if (*value == NULL) {
  342. /* possible complex type ahead */
  343. if (t == JS_OBRACK)
  344. *pt = XSTYPE_LIST;
  345. else
  346. if (t == JS_OCURLY)
  347. *pt = XSTYPE_DICT;
  348. else
  349. return -1;
  350. }
  351. *c = *c + 1;
  352. return 1;
  353. }
  354. xs_dict *xs_json_load_object(FILE *f)
  355. /* loads a full JSON object (after the initial OCURLY) */
  356. {
  357. xstype t;
  358. xs_dict *d = xs_dict_new();
  359. int c = 0;
  360. for (;;) {
  361. xs *k = NULL;
  362. xs *v = NULL;
  363. int r = xs_json_load_object_iter(f, &k, &v, &t, &c);
  364. if (r == -1)
  365. d = xs_free(d);
  366. if (r == 1) {
  367. /* partial load? */
  368. if (v == NULL) {
  369. if (t == XSTYPE_LIST)
  370. v = xs_json_load_array(f);
  371. else
  372. if (t == XSTYPE_DICT)
  373. v = xs_json_load_object(f);
  374. }
  375. /* still null? fail */
  376. if (v == NULL) {
  377. d = xs_free(d);
  378. break;
  379. }
  380. d = xs_dict_append(d, k, v);
  381. }
  382. else
  383. break;
  384. }
  385. return d;
  386. }
  387. xs_val *xs_json_loads(const xs_str *json)
  388. /* loads a string in JSON format and converts to a multiple data */
  389. {
  390. FILE *f;
  391. xs_val *v = NULL;
  392. if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) {
  393. v = xs_json_load(f);
  394. fclose(f);
  395. }
  396. return v;
  397. }
  398. xstype xs_json_load_type(FILE *f)
  399. /* identifies the type of a JSON stream */
  400. {
  401. xstype t = XSTYPE_NULL;
  402. js_type jt;
  403. xs_free(_xs_json_load_lexer(f, &jt));
  404. if (jt == JS_OBRACK)
  405. t = XSTYPE_LIST;
  406. else
  407. if (jt == JS_OCURLY)
  408. t = XSTYPE_DICT;
  409. return t;
  410. }
  411. xs_val *xs_json_load(FILE *f)
  412. /* loads a JSON file */
  413. {
  414. xs_val *v = NULL;
  415. xstype t = xs_json_load_type(f);
  416. if (t == XSTYPE_LIST)
  417. v = xs_json_load_array(f);
  418. else
  419. if (t == XSTYPE_DICT)
  420. v = xs_json_load_object(f);
  421. return v;
  422. }
  423. #endif /* XS_IMPLEMENTATION */
  424. #endif /* _XS_JSON_H */