ujson.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* ujson: small arena JSON DOM parser for ring-3 commands.
  2. *
  3. * The caller owns all memory: a node pool and a string pool. Parsing never
  4. * allocates; it fails cleanly when either pool is exhausted. Strings (keys
  5. * and values) are unescaped into the string pool as NUL-terminated UTF-8
  6. * (\uXXXX escapes and surrogate pairs are decoded). Numbers keep their raw
  7. * text; ujson_int() converts the integer part.
  8. *
  9. * Pure freestanding C with no includes, so the host test (ujson_test.c) links
  10. * the same file. */
  11. #ifndef UJSON_H
  12. #define UJSON_H
  13. #define UJSON_NULL 0
  14. #define UJSON_FALSE 1
  15. #define UJSON_TRUE 2
  16. #define UJSON_NUMBER 3
  17. #define UJSON_STRING 4
  18. #define UJSON_ARRAY 5
  19. #define UJSON_OBJECT 6
  20. #define UJSON_MAX_DEPTH 48
  21. struct ujson_node {
  22. unsigned char type;
  23. const char *key; /* member name inside an object, else 0 */
  24. const char *str; /* STRING: decoded text; NUMBER: raw text */
  25. unsigned int len; /* STRING/NUMBER: bytes; ARRAY/OBJECT: count */
  26. int child; /* first child index, -1 none */
  27. int next; /* next sibling index, -1 none */
  28. };
  29. struct ujson {
  30. struct ujson_node *nodes;
  31. unsigned int cap, n;
  32. char *sbuf;
  33. unsigned int scap, sn;
  34. const char *error; /* static message after a failed parse */
  35. unsigned int error_pos;
  36. };
  37. /* A string pool of len + 2 * nodes bytes is always large enough. */
  38. void ujson_init(struct ujson *j, struct ujson_node *nodes, unsigned int cap,
  39. char *sbuf, unsigned int scap);
  40. /* Returns the root node index (0) or -1 on error (j->error says why). */
  41. int ujson_parse(struct ujson *j, const char *src, unsigned int len);
  42. int ujson_type(const struct ujson *j, int idx);
  43. /* Object member by name; -1 when idx is not an object or has no member. */
  44. int ujson_get(const struct ujson *j, int idx, const char *key);
  45. /* Array/object iteration: first child and next sibling, -1 at the end. */
  46. int ujson_first(const struct ujson *j, int idx);
  47. int ujson_next(const struct ujson *j, int idx);
  48. unsigned int ujson_count(const struct ujson *j, int idx);
  49. /* "" when idx is not a string (or -1). */
  50. const char *ujson_str(const struct ujson *j, int idx);
  51. /* Convenience: ujson_str(ujson_get(obj, key)). */
  52. const char *ujson_get_str(const struct ujson *j, int obj, const char *key);
  53. /* Integer part of a number (or numeric string); def otherwise. */
  54. long ujson_int(const struct ujson *j, int idx, long def);
  55. /* 1 for true, a non-zero number or a non-empty string; 0 otherwise. */
  56. int ujson_truthy(const struct ujson *j, int idx);
  57. /* Write s as a JSON string literal (with the quotes). Returns the length
  58. * written, or -1 when it does not fit in cap (including the NUL). */
  59. int ujson_escape(char *out, unsigned int cap, const char *s);
  60. #endif