json.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Structures for JSON parsing using only fixed-extent memory
  2. *
  3. * This file is Copyright 2010 by the GPSD project
  4. * SPDX-License-Identifier: BSD-2-clause
  5. */
  6. #include <ctype.h>
  7. #include <stdbool.h>
  8. #include <sys/time.h> // for struct timespec
  9. /* the json_type is the type of the C variable the JSON
  10. * value gets placed in. It is NOT the JSON type as used
  11. * in the JSON standard. But it does partly specify how
  12. * the JSON value is decoded.
  13. *
  14. * For example a t_character must be in quotes, but a t_byte
  15. * is a bare number. */
  16. typedef enum {t_array,
  17. t_boolean,
  18. t_byte,
  19. t_character,
  20. t_check,
  21. t_ignore,
  22. t_integer,
  23. t_longint,
  24. t_object,
  25. t_real,
  26. t_short,
  27. t_string,
  28. t_structobject,
  29. t_time,
  30. t_timespec,
  31. t_ubyte,
  32. t_uinteger,
  33. t_ulongint,
  34. t_ushort}
  35. json_type;
  36. struct json_enum_t {
  37. char *name;
  38. int value;
  39. };
  40. struct json_array_t {
  41. json_type element_type;
  42. union {
  43. struct {
  44. const struct json_attr_t *subtype;
  45. char *base;
  46. size_t stride;
  47. } objects;
  48. struct {
  49. char **ptrs;
  50. char *store;
  51. int storelen;
  52. } strings;
  53. struct {
  54. int *store;
  55. } bytes;
  56. struct {
  57. unsigned int *store;
  58. } ubytes;
  59. struct {
  60. int *store;
  61. } integers;
  62. struct {
  63. unsigned int *store;
  64. } uintegers;
  65. struct {
  66. long *store;
  67. } longint;
  68. struct {
  69. unsigned long *store;
  70. } ulongint;
  71. struct {
  72. short *store;
  73. } shorts;
  74. struct {
  75. unsigned short *store;
  76. } ushorts;
  77. struct {
  78. double *store;
  79. } reals;
  80. struct {
  81. bool *store;
  82. } booleans;
  83. struct {
  84. struct timespec *store;
  85. } timespecs;
  86. } arr;
  87. int *count, maxlen;
  88. };
  89. struct json_attr_t {
  90. char *attribute;
  91. json_type type;
  92. union {
  93. bool *boolean;
  94. char *byte;
  95. char *character;
  96. char *string;
  97. double *real;
  98. int *integer;
  99. long *longint;
  100. short *shortint;
  101. size_t offset;
  102. struct json_array_t array;
  103. unsigned char *ubyte;
  104. unsigned int *uinteger;
  105. unsigned long *ulongint;
  106. unsigned short *ushortint;
  107. struct timespec *ts;
  108. } addr;
  109. union {
  110. bool boolean;
  111. char byte;
  112. char character;
  113. char *check;
  114. double real;
  115. int integer;
  116. long longint;
  117. short shortint;
  118. unsigned char ubyte;
  119. unsigned int uinteger;
  120. unsigned long ulongint;
  121. unsigned short ushortint;
  122. struct timespec ts;
  123. } dflt;
  124. size_t len;
  125. const struct json_enum_t *map;
  126. bool nodefault;
  127. };
  128. #define JSON_ATTR_MAX 31 /* max chars in JSON attribute name */
  129. #define JSON_VAL_MAX 512 /* max chars in JSON value part */
  130. #ifdef __cplusplus
  131. extern "C" {
  132. #endif
  133. int json_read_object(const char *, const struct json_attr_t *,
  134. const char **);
  135. int json_read_array(const char *, const struct json_array_t *,
  136. const char **);
  137. const char *json_error_string(int);
  138. void json_enable_debug(int, FILE *);
  139. char *json_quote(const char *, char *, size_t, size_t);
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #define JSON_ERR_OBSTART 1 // non-WS when expecting object start
  144. #define JSON_ERR_ATTRSTART 2 // non-WS when expecting attrib start
  145. #define JSON_ERR_BADATTR 3 // unknown attribute name
  146. #define JSON_ERR_ATTRLEN 4 // attribute name too long
  147. #define JSON_ERR_NOARRAY 5 // saw [ when not expecting array
  148. #define JSON_ERR_NOBRAK 6 // array element specified, but no [
  149. #define JSON_ERR_STRLONG 7 // string value too long
  150. #define JSON_ERR_TOKLONG 8 // token value too long
  151. #define JSON_ERR_BADTRAIL 9 // garbage while expecting comma or } or ]
  152. #define JSON_ERR_ARRAYSTART 10 // didn't find expected array start
  153. #define JSON_ERR_OBJARR 11 // error while parsing object array
  154. #define JSON_ERR_SUBTOOLONG 12 // too many array elements
  155. #define JSON_ERR_BADSUBTRAIL 13 // garbage while expecting array comma
  156. #define JSON_ERR_SUBTYPE 14 // unsupported array element type
  157. #define JSON_ERR_BADSTRING 15 // error while string parsing
  158. #define JSON_ERR_CHECKFAIL 16 // check attribute not matched
  159. #define JSON_ERR_NOPARSTR 17 // can't support strings in parallel arrays
  160. #define JSON_ERR_BADENUM 18 // invalid enumerated value
  161. #define JSON_ERR_QNONSTRING 19 // saw quoted value when expecting nonstring
  162. #define JSON_ERR_NONQSTRING 20 // didn't see quoted value when expecting string
  163. #define JSON_ERR_MISC 21 // other data conversion error
  164. #define JSON_ERR_BADNUM 22 // error while parsing a numerical argument
  165. #define JSON_ERR_NULLPTR 23 // unexpected null value or attribute pointer
  166. #define JSON_ERR_NOCURLY 24 // object element specified, but no {
  167. #define JSON_ERR_EMPTY 25 // input was empty or white-space only
  168. /*
  169. * Use the following macros to declare template initializers for structobject
  170. * arrays. Writing the equivalents out by hand is error-prone.
  171. *
  172. * STRUCTOBJECT takes a structure name s, and a fieldname f in s.
  173. *
  174. * STRUCTARRAY takes the name of a structure array, a pointer to a an
  175. * initializer defining the subobject type, and the address of an integer to
  176. * store the length in.
  177. */
  178. #define STRUCTOBJECT(s, f) .addr.offset = offsetof(s, f)
  179. #define STRUCTARRAY(a, e, n) \
  180. .addr.array.element_type = t_structobject, \
  181. .addr.array.arr.objects.subtype = e, \
  182. .addr.array.arr.objects.base = (char*)a, \
  183. .addr.array.arr.objects.stride = sizeof(a[0]), \
  184. .addr.array.count = n, \
  185. .addr.array.maxlen = NITEMS(a)
  186. /* json.h ends here */
  187. // vim: set expandtab shiftwidth=4