xs_json.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
  2. #ifndef _XS_JSON_H
  3. #define _XS_JSON_H
  4. xs_str *xs_json_dumps_pp(const xs_val *data, int indent);
  5. int xs_json_dump_pp(const xs_val *data, int indent, FILE *f);
  6. #define xs_json_dumps(data) xs_json_dumps_pp(data, 0)
  7. xs_val *xs_json_loads(const xs_str *json);
  8. xs_val *xs_json_load(FILE *f);
  9. #ifdef XS_IMPLEMENTATION
  10. /** IMPLEMENTATION **/
  11. /** JSON dumps **/
  12. static xs_str *_xs_json_dumps_str(xs_str *s, const char *data)
  13. /* dumps a string in JSON format */
  14. {
  15. unsigned char c;
  16. s = xs_str_cat(s, "\"");
  17. while ((c = *data)) {
  18. if (c == '\n')
  19. s = xs_str_cat(s, "\\n");
  20. else
  21. if (c == '\r')
  22. s = xs_str_cat(s, "\\r");
  23. else
  24. if (c == '\t')
  25. s = xs_str_cat(s, "\\t");
  26. else
  27. if (c == '\\')
  28. s = xs_str_cat(s, "\\\\");
  29. else
  30. if (c == '"')
  31. s = xs_str_cat(s, "\\\"");
  32. else
  33. if (c < 32) {
  34. char tmp[10];
  35. snprintf(tmp, sizeof(tmp), "\\u%04x", (unsigned int) c);
  36. s = xs_str_cat(s, tmp);
  37. }
  38. else
  39. s = xs_append_m(s, data, 1);
  40. data++;
  41. }
  42. s = xs_str_cat(s, "\"");
  43. return s;
  44. }
  45. static xs_str *_xs_json_indent(xs_str *s, int level, int indent)
  46. /* adds indentation */
  47. {
  48. if (indent) {
  49. int n;
  50. s = xs_str_cat(s, "\n");
  51. for (n = 0; n < level * indent; n++)
  52. s = xs_str_cat(s, " ");
  53. }
  54. return s;
  55. }
  56. static xs_str *_xs_json_dumps(xs_str *s, const xs_val *s_data, int level, int indent)
  57. /* dumps partial data as JSON */
  58. {
  59. int c = 0;
  60. xs_val *v;
  61. xs_val *data = (xs_val *)s_data;
  62. switch (xs_type(data)) {
  63. case XSTYPE_NULL:
  64. s = xs_str_cat(s, "null");
  65. break;
  66. case XSTYPE_TRUE:
  67. s = xs_str_cat(s, "true");
  68. break;
  69. case XSTYPE_FALSE:
  70. s = xs_str_cat(s, "false");
  71. break;
  72. case XSTYPE_NUMBER:
  73. s = xs_str_cat(s, xs_number_str(data));
  74. break;
  75. case XSTYPE_LIST:
  76. s = xs_str_cat(s, "[");
  77. while (xs_list_iter(&data, &v)) {
  78. if (c != 0)
  79. s = xs_str_cat(s, ",");
  80. s = _xs_json_indent(s, level + 1, indent);
  81. s = _xs_json_dumps(s, v, level + 1, indent);
  82. c++;
  83. }
  84. s = _xs_json_indent(s, level, indent);
  85. s = xs_str_cat(s, "]");
  86. break;
  87. case XSTYPE_DICT:
  88. s = xs_str_cat(s, "{");
  89. xs_str *k;
  90. while (xs_dict_iter(&data, &k, &v)) {
  91. if (c != 0)
  92. s = xs_str_cat(s, ",");
  93. s = _xs_json_indent(s, level + 1, indent);
  94. s = _xs_json_dumps_str(s, k);
  95. s = xs_str_cat(s, ":");
  96. if (indent)
  97. s = xs_str_cat(s, " ");
  98. s = _xs_json_dumps(s, v, level + 1, indent);
  99. c++;
  100. }
  101. s = _xs_json_indent(s, level, indent);
  102. s = xs_str_cat(s, "}");
  103. break;
  104. case XSTYPE_STRING:
  105. s = _xs_json_dumps_str(s, data);
  106. break;
  107. default:
  108. break;
  109. }
  110. return s;
  111. }
  112. xs_str *xs_json_dumps_pp(const xs_val *data, int indent)
  113. /* dumps a piece of data as JSON */
  114. {
  115. xstype t = xs_type(data);
  116. xs_str *s = NULL;
  117. if (t == XSTYPE_LIST || t == XSTYPE_DICT) {
  118. s = xs_str_new(NULL);
  119. s = _xs_json_dumps(s, data, 0, indent);
  120. }
  121. return s;
  122. }
  123. int xs_json_dump_pp(const xs_val *data, int indent, FILE *f)
  124. /* dumps data into a file as JSON */
  125. {
  126. xs *j = xs_json_dumps_pp(data, indent);
  127. if (j == NULL)
  128. return 0;
  129. fwrite(j, strlen(j), 1, f);
  130. return 1;
  131. }
  132. /** JSON loads **/
  133. /* this code comes mostly from the Minimum Profit Text Editor (MPDM) */
  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_INTEGER,
  146. JS_REAL,
  147. JS_TRUE,
  148. JS_FALSE,
  149. JS_NULL,
  150. JS_ARRAY,
  151. JS_OBJECT
  152. } js_type;
  153. static xs_val *_xs_json_loads_lexer(const char **json, js_type *t)
  154. {
  155. char c;
  156. const char *s = *json;
  157. xs_val *v = NULL;
  158. /* skip blanks */
  159. while (*s == L' ' || *s == L'\t' || *s == L'\n' || *s == L'\r')
  160. s++;
  161. c = *s++;
  162. if (c == '{')
  163. *t = JS_OCURLY;
  164. else
  165. if (c == '}')
  166. *t = JS_CCURLY;
  167. else
  168. if (c == '[')
  169. *t = JS_OBRACK;
  170. else
  171. if (c == ']')
  172. *t = JS_CBRACK;
  173. else
  174. if (c == ',')
  175. *t = JS_COMMA;
  176. else
  177. if (c == ':')
  178. *t = JS_COLON;
  179. else
  180. if (c == '"') {
  181. *t = JS_STRING;
  182. v = xs_str_new(NULL);
  183. while ((c = *s) != '"' && c != '\0') {
  184. char tmp[5];
  185. int cp, i;
  186. if (c == '\\') {
  187. s++;
  188. c = *s;
  189. switch (c) {
  190. case 'n': c = '\n'; break;
  191. case 'r': c = '\r'; break;
  192. case 't': c = '\t'; break;
  193. case 'u': /* Unicode codepoint as an hex char */
  194. s++;
  195. strncpy(tmp, s, 4);
  196. tmp[4] = '\0';
  197. if (strlen(tmp) != 4) {
  198. *t = JS_ERROR;
  199. break;
  200. }
  201. s += 3; /* skip as it was one byte */
  202. sscanf(tmp, "%04x", &i);
  203. if (i >= 0xd800 && i <= 0xdfff) {
  204. /* it's a surrogate pair */
  205. cp = (i & 0x3ff) << 10;
  206. /* skip to the next value (last char + \ + u) */
  207. s++;
  208. if (memcmp(s, "\\u", 2) != 0) {
  209. *t = JS_ERROR;
  210. break;
  211. }
  212. s += 2;
  213. strncpy(tmp, s, 4);
  214. tmp[4] = '\0';
  215. if (strlen(tmp) != 4) {
  216. *t = JS_ERROR;
  217. break;
  218. }
  219. s += 3; /* skip as it was one byte */
  220. sscanf(tmp, "%04x", &i);
  221. cp |= (i & 0x3ff);
  222. cp += 0x10000;
  223. }
  224. else
  225. cp = i;
  226. /* replace dangerous control codes with their visual representations */
  227. if (cp >= '\0' && cp < ' ' && !strchr("\r\n\t", cp))
  228. cp += 0x2400;
  229. v = xs_utf8_enc(v, cp);
  230. c = '\0';
  231. break;
  232. }
  233. }
  234. if (c)
  235. v = xs_append_m(v, &c, 1);
  236. s++;
  237. }
  238. if (c != '\0')
  239. s++;
  240. }
  241. else
  242. if (c == '-' || (c >= '0' && c <= '9') || c == '.') {
  243. xs *vn = NULL;
  244. *t = JS_INTEGER;
  245. vn = xs_str_new(NULL);
  246. vn = xs_append_m(vn, &c, 1);
  247. while (((c = *s) >= '0' && c <= '9') || c == '.') {
  248. if (c == '.')
  249. *t = JS_REAL;
  250. vn = xs_append_m(vn, &c, 1);
  251. s++;
  252. }
  253. /* convert to XSTYPE_NUMBER */
  254. v = xs_number_new(atof(vn));
  255. }
  256. else
  257. if (c == 't' && strncmp(s, "rue", 3) == 0) {
  258. s += 3;
  259. *t = JS_TRUE;
  260. v = xs_val_new(XSTYPE_TRUE);
  261. }
  262. else
  263. if (c == 'f' && strncmp(s, "alse", 4) == 0) {
  264. s += 4;
  265. *t = JS_FALSE;
  266. v = xs_val_new(XSTYPE_FALSE);
  267. }
  268. else
  269. if (c == 'n' && strncmp(s, "ull", 3) == 0) {
  270. s += 3;
  271. *t = JS_NULL;
  272. v = xs_val_new(XSTYPE_NULL);
  273. }
  274. else
  275. *t = JS_ERROR;
  276. *json = s;
  277. return v;
  278. }
  279. static xs_list *_xs_json_loads_array(const char **json, js_type *t);
  280. static xs_dict *_xs_json_loads_object(const char **json, js_type *t);
  281. static xs_val *_xs_json_loads_value(const char **json, js_type *t, xs_val *v)
  282. /* parses a JSON value */
  283. {
  284. if (*t == JS_OBRACK)
  285. v = _xs_json_loads_array(json, t);
  286. else
  287. if (*t == JS_OCURLY)
  288. v = _xs_json_loads_object(json, t);
  289. if (*t >= JS_VALUE)
  290. *t = JS_VALUE;
  291. else
  292. *t = JS_ERROR;
  293. return v;
  294. }
  295. static xs_list *_xs_json_loads_array(const char **json, js_type *t)
  296. /* parses a JSON array */
  297. {
  298. const char *s = *json;
  299. xs *v;
  300. xs_list *l;
  301. js_type tt;
  302. l = xs_list_new();
  303. *t = JS_INCOMPLETE;
  304. v = _xs_json_loads_lexer(&s, &tt);
  305. if (tt == JS_CBRACK)
  306. *t = JS_ARRAY;
  307. else {
  308. v = _xs_json_loads_value(&s, &tt, v);
  309. if (tt == JS_VALUE) {
  310. l = xs_list_append(l, v);
  311. while (*t == JS_INCOMPLETE) {
  312. xs_free(_xs_json_loads_lexer(&s, &tt));
  313. if (tt == JS_CBRACK)
  314. *t = JS_ARRAY;
  315. else
  316. if (tt == JS_COMMA) {
  317. xs *v2;
  318. v2 = _xs_json_loads_lexer(&s, &tt);
  319. v2 = _xs_json_loads_value(&s, &tt, v2);
  320. if (tt == JS_VALUE)
  321. l = xs_list_append(l, v2);
  322. else
  323. *t = JS_ERROR;
  324. }
  325. else
  326. *t = JS_ERROR;
  327. }
  328. }
  329. else
  330. *t = JS_ERROR;
  331. }
  332. if (*t == JS_ERROR)
  333. l = xs_free(l);
  334. *json = s;
  335. return l;
  336. }
  337. static xs_dict *_xs_json_loads_object(const char **json, js_type *t)
  338. /* parses a JSON object */
  339. {
  340. const char *s = *json;
  341. xs *k1;
  342. xs_dict *d;
  343. js_type tt;
  344. d = xs_dict_new();
  345. *t = JS_INCOMPLETE;
  346. k1 = _xs_json_loads_lexer(&s, &tt);
  347. if (tt == JS_CCURLY)
  348. *t = JS_OBJECT;
  349. else
  350. if (tt == JS_STRING) {
  351. xs_free(_xs_json_loads_lexer(&s, &tt));
  352. if (tt == JS_COLON) {
  353. xs *v1;
  354. v1 = _xs_json_loads_lexer(&s, &tt);
  355. v1 = _xs_json_loads_value(&s, &tt, v1);
  356. if (tt == JS_VALUE) {
  357. d = xs_dict_append(d, k1, v1);
  358. while (*t == JS_INCOMPLETE) {
  359. xs_free(_xs_json_loads_lexer(&s, &tt));
  360. if (tt == JS_CCURLY)
  361. *t = JS_OBJECT;
  362. else
  363. if (tt == JS_COMMA) {
  364. xs *k = _xs_json_loads_lexer(&s, &tt);
  365. if (tt == JS_STRING) {
  366. xs_free(_xs_json_loads_lexer(&s, &tt));
  367. if (tt == JS_COLON) {
  368. xs *v;
  369. v = _xs_json_loads_lexer(&s, &tt);
  370. v = _xs_json_loads_value(&s, &tt, v);
  371. if (tt == JS_VALUE)
  372. d = xs_dict_append(d, k, v);
  373. else
  374. *t = JS_ERROR;
  375. }
  376. else
  377. *t = JS_ERROR;
  378. }
  379. else
  380. *t = JS_ERROR;
  381. }
  382. else
  383. *t = JS_ERROR;
  384. }
  385. }
  386. else
  387. *t = JS_ERROR;
  388. }
  389. else
  390. *t = JS_ERROR;
  391. }
  392. else
  393. *t = JS_ERROR;
  394. if (*t == JS_ERROR)
  395. d = xs_free(d);
  396. *json = s;
  397. return d;
  398. }
  399. xs_val *xs_json_loads(const xs_str *json)
  400. /* loads a string in JSON format and converts to a multiple data */
  401. {
  402. xs_val *v = NULL;
  403. js_type t;
  404. xs_free(_xs_json_loads_lexer(&json, &t));
  405. if (t == JS_OBRACK)
  406. v = _xs_json_loads_array(&json, &t);
  407. else
  408. if (t == JS_OCURLY)
  409. v = _xs_json_loads_object(&json, &t);
  410. else
  411. t = JS_ERROR;
  412. return v;
  413. }
  414. xs_val *xs_json_load(FILE *f)
  415. /* loads a JSON file */
  416. {
  417. xs *o = xs_readall(f);
  418. return o ? xs_json_loads(o) : NULL;
  419. }
  420. #endif /* XS_IMPLEMENTATION */
  421. #endif /* _XS_JSON_H */