xs_json.h 11 KB

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