1 /* Structures for JSON parsing using only fixed-extent memory
3 * This file is Copyright (c) 2010 by the GPSD project
4 * BSD terms apply: see the file COPYING in the distribution root for details.
10 typedef enum {t_integer, t_uinteger, t_real,
11 t_string, t_boolean, t_character,
12 t_object, t_structobject, t_array,
15 #define nullbool -1 /* not true, not false */
23 json_type element_type;
26 const struct json_attr_t *subtype;
44 unsigned int *uinteger;
49 struct json_array_t array;
54 unsigned int uinteger;
61 const struct json_enum_t *map;
65 #define JSON_ATTR_MAX 31 /* max chars in JSON attribute name */
66 #define JSON_VAL_MAX 120 /* max chars in JSON value part */
71 int json_read_object(const char *, const struct json_attr_t *,
72 /*@null@*/const char **);
73 int json_read_array(const char *, const struct json_array_t *,
74 /*@null@*/const char **);
75 const /*@observer@*/char *json_error_string(int);
77 void json_enable_debug(int, FILE *);
82 #define JSON_ERR_OBSTART 1 /* non-WS when expecting object start */
83 #define JSON_ERR_ATTRSTART 2 /* non-WS when expecting attrib start */
84 #define JSON_ERR_BADATTR 3 /* unknown attribute name */
85 #define JSON_ERR_ATTRLEN 4 /* attribute name too long */
86 #define JSON_ERR_NOARRAY 5 /* saw [ when not expecting array */
87 #define JSON_ERR_NOBRAK 6 /* array element specified, but no [ */
88 #define JSON_ERR_STRLONG 7 /* string value too long */
89 #define JSON_ERR_TOKLONG 8 /* token value too long */
90 #define JSON_ERR_BADTRAIL 9 /* garbage while expecting , or } */
91 #define JSON_ERR_ARRAYSTART 10 /* didn't find expected array start */
92 #define JSON_ERR_OBJARR 11 /* error while parsing object array */
93 #define JSON_ERR_SUBTOOLONG 12 /* too many array elements */
94 #define JSON_ERR_BADSUBTRAIL 13 /* garbage while expecting array comma */
95 #define JSON_ERR_SUBTYPE 14 /* unsupported array element type */
96 #define JSON_ERR_BADSTRING 15 /* error while string parsing */
97 #define JSON_ERR_CHECKFAIL 16 /* check attribute not matched */
98 #define JSON_ERR_NOPARSTR 17 /* can't support strings in parallel arrays */
99 #define JSON_ERR_BADENUM 18 /* invalid enumerated value */
100 #define JSON_ERR_QNONSTRING 19 /* saw quoted value when expecting nonstring */
101 #define JSON_ERR_NONQSTRING 19 /* didn't see quoted value when expecting string */
102 #define JSON_ERR_MISC 20 /* other data conversion error */
105 * Use the following macros to declare template initializers for structobject
106 * arrays. Writing the equivalents out by hand is error-prone.
108 * STRUCTOBJECT takes a structure name s, and a fieldname f in s.
110 * STRUCTARRAY takes the name of a structure array, a pointer to a an
111 * initializer defining the subobject type, and the address of an integer to
112 * store the length in.
114 #define STRUCTOBJECT(s, f) .addr.offset = offsetof(s, f)
115 #define STRUCTARRAY(a, e, n) \
116 .addr.array.element_type = t_structobject, \
117 .addr.array.arr.objects.subtype = e, \
118 .addr.array.arr.objects.base = (char*)a, \
119 .addr.array.arr.objects.stride = sizeof(a[0]), \
120 .addr.array.count = n, \
121 .addr.array.maxlen = NITEMS(a)
123 /* json.h ends here */