5#ifndef SPA_UTILS_JSON_H
6#define SPA_UTILS_JSON_H
37#define SPA_JSON_ERROR_FLAG 0x100
42#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), 0, 0, 0 })
48#define SPA_JSON_ENTER(iter) ((struct spa_json) { (iter)->cur, (iter)->end, (iter), (iter)->state & 0xff0, 0 })
55#define SPA_JSON_SAVE(iter) ((struct spa_json) { (iter)->cur, (iter)->end, NULL, (iter)->state, 0 })
61 int utf8_remain = 0, err = 0;
63 __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT,
65 __PREV_ARRAY_FLAG = 0x20,
70 __ERROR_INVALID_ARRAY_SEPARATOR,
71 __ERROR_EXPECTED_OBJECT_KEY,
72 __ERROR_EXPECTED_OBJECT_VALUE,
73 __ERROR_TOO_DEEP_NESTING,
74 __ERROR_EXPECTED_ARRAY_CLOSE,
75 __ERROR_EXPECTED_OBJECT_CLOSE,
76 __ERROR_MISMATCHED_BRACKET,
77 __ERROR_ESCAPE_NOT_ALLOWED,
78 __ERROR_CHARACTERS_NOT_ALLOWED,
79 __ERROR_INVALID_ESCAPE,
80 __ERROR_INVALID_STATE,
81 __ERROR_UNFINISHED_STRING,
83 uint64_t array_stack[8] = {0};
90 for (; iter->
cur < iter->
end; iter->
cur++) {
91 unsigned char cur = (
unsigned char)*iter->
cur;
94#define _SPA_ERROR(reason) { err = __ERROR_ ## reason; goto error; }
96 flag = iter->
state & __FLAGS;
97 switch (iter->
state & ~__FLAGS) {
99 flag &= ~(__KEY_FLAG | __PREV_ARRAY_FLAG);
100 iter->
state = __STRUCT | flag;
105 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ',':
108 if (flag & __ARRAY_FLAG)
110 if (!(flag & __KEY_FLAG))
112 iter->
state |= __SUB_FLAG;
115 iter->
state = __COMMENT | flag;
118 if (flag & __KEY_FLAG)
120 if (!(flag & __ARRAY_FLAG))
123 iter->
state = __STRING | flag;
126 if (!(flag & __ARRAY_FLAG)) {
131 if ((iter->
state & __SUB_FLAG) && !(flag & __KEY_FLAG))
135 iter->
state = __STRUCT | __SUB_FLAG | flag;
142 if (iter->
depth == 0) {
145 uint64_t mask = 1ULL << ((iter->
depth-1) & 0x3f);
153 if (++iter->
depth > 1)
158 if ((flag & __ARRAY_FLAG) &&
cur !=
']')
160 if (!(flag & __ARRAY_FLAG) &&
cur !=
'}')
162 if (flag & __KEY_FLAG) {
166 iter->
state = __STRUCT | __SUB_FLAG | flag;
167 if (iter->
depth == 0) {
175 if (iter->
depth == 0) {
178 uint64_t mask = 1ULL << ((iter->
depth-1) & 0x3f);
191 if (!(
cur >= 32 &&
cur <= 126))
193 if (flag & __KEY_FLAG)
195 if (!(flag & __ARRAY_FLAG))
198 iter->
state = __BARE | flag;
204 case '\t':
case ' ':
case '\r':
case '\n':
206 case ':':
case ',':
case '=':
case ']':
case '}':
207 iter->
state = __STRUCT | flag;
210 return iter->
cur - *value;
216 if (
cur >= 32 &&
cur <= 126)
223 iter->
state = __ESC | flag;
226 iter->
state = __STRUCT | flag;
229 return ++iter->
cur - *value;
238 iter->
state = __UTF8 | flag;
241 if (
cur >= 32 &&
cur <= 127)
248 if (--utf8_remain == 0)
249 iter->
state = __STRING | flag;
255 case '"':
case '\\':
case '/':
case 'b':
case 'f':
256 case 'n':
case 'r':
case 't':
case 'u':
257 iter->
state = __STRING | flag;
263 case '\n':
case '\r':
264 iter->
state = __STRUCT | flag;
275 switch (iter->
state & ~__FLAGS) {
276 case __STRING:
case __UTF8:
case __ESC:
284 if ((iter->
state & __SUB_FLAG) && (iter->
state & __KEY_FLAG)) {
289 if ((iter->
state & ~__FLAGS) != __STRUCT) {
290 iter->
state = __STRUCT | (iter->
state & __FLAGS);
291 return iter->
cur - *value;
316 static const char *reasons[] = {
318 "Invalid array separator",
319 "Expected object key",
320 "Expected object value",
322 "Expected array close bracket",
323 "Expected object close brace",
324 "Mismatched bracket",
325 "Escape not allowed",
326 "Character not allowed",
330 "Expected key separator",
337 int linepos = 1, colpos = 1, code;
340 for (l = p = start; p && p != iter->
cur; ++p) {
354 loc->
reason = code == 0 ? strerror(errno) : reasons[code];
373 return len > 0 && (*val ==
'{' || *val ==
'[');
390 return sub.
cur + 1 - value;
396 return len > 0 && *val ==
'{';
406 return len > 0 && *val ==
'[';
416 return len == 4 && strncmp(val,
"null", 4) == 0;
426 if (len <= 0 || len >= (
int)
sizeof(buf))
429 for (pos = 0; pos < len; ++pos) {
431 case '+':
case '-':
case '0' ...
'9':
case '.':
case 'e':
case 'E':
break;
436 memcpy(buf, val, len);
440 return len > 0 &&
end == buf + len;
461 val = signbit(val) ? FLT_MIN : FLT_MAX;
474 if (len <= 0 || len >= (
int)
sizeof(buf))
477 memcpy(buf, val, len);
480 *result = strtol(buf, &
end, 0);
481 return len > 0 &&
end == buf + len;
500 return len == 4 && strncmp(val,
"true", 4) == 0;
505 return len == 5 && strncmp(val,
"false", 5) == 0;
533 return len > 1 && *val ==
'"';
540 for (i = 0; i < num; i++) {
542 if (v >=
'0' && v <=
'9')
544 else if (v >=
'a' && v <=
'f')
546 else if (v >=
'A' && v <=
'F')
550 *res = (*res << 4) | v;
562 memmove(result, val, len);
565 for (p = val+1; p < val + len; p++) {
578 else if (*p ==
'u') {
579 uint8_t prefix[] = { 0, 0xc0, 0xe0, 0xf0 };
580 uint32_t idx, n, v, cp, enc[] = { 0x80, 0x800, 0x10000 };
581 if (val + len - p < 5 ||
588 if (cp >= 0xd800 && cp <= 0xdbff) {
589 if (val + len - p < 7 ||
590 p[1] !=
'\\' || p[2] !=
'u' ||
592 v < 0xdc00 || v > 0xdfff)
595 cp = 0x010000 + (((cp & 0x3ff) << 10) | (v & 0x3ff));
596 }
else if (cp >= 0xdc00 && cp <= 0xdfff)
599 for (idx = 0; idx < 3; idx++)
602 for (n = idx; n > 0; n--, cp >>= 6)
603 result[n] = (cp | 0x80) & 0xbf;
604 *result++ = (cp | prefix[idx]) & 0xff;
608 }
else if (*p ==
'\"') {
635 static const char hex[] = {
"0123456789abcdef" };
636#define __PUT(c) { if (len < size) *str++ = c; len++; }
660 if (*val > 0 && *val < 0x20) {
663 __PUT(hex[((*val)>>4)&0xf]);
__PUT(hex[(*val)&0xf]);
static bool spa_json_is_string(const char *val, int len)
Definition json.h:540
static bool spa_json_is_float(const char *val, int len)
Definition json.h:452
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json.h:429
static bool spa_json_is_true(const char *val, int len)
Definition json.h:507
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json.h:564
static int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition json.h:368
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json.h:58
static int spa_json_parse_hex(const char *p, int num, uint32_t *res)
Definition json.h:545
static bool spa_json_is_false(const char *val, int len)
Definition json.h:512
static int spa_json_get_int(struct spa_json *iter, int *res)
Definition json.h:497
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json.h:522
static int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition json.h:407
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:632
static bool spa_json_is_bool(const char *val, int len)
Definition json.h:517
#define SPA_JSON_INIT(data, size)
Definition json.h:49
static bool spa_json_get_error(struct spa_json *iter, const char *start, struct spa_error_location *loc)
Return it there was a parse error, and its possible location.
Definition json.h:322
#define SPA_JSON_ERROR_FLAG
Definition json.h:43
static char * spa_json_format_float(char *str, int size, float val)
Definition json.h:466
static bool spa_json_is_array(const char *val, int len)
Definition json.h:413
static int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition json.h:530
static bool spa_json_is_null(const char *val, int len)
Definition json.h:423
#define SPA_JSON_ENTER(iter)
Definition json.h:56
static int spa_json_encode_string(char *str, int size, const char *val)
Definition json.h:641
static int spa_json_parse_int(const char *val, int len, int *result)
Definition json.h:478
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json.h:68
static int spa_json_is_container(const char *val, int len)
Definition json.h:380
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json.h:51
static bool spa_json_is_int(const char *val, int len)
Definition json.h:492
static int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition json.h:417
static int spa_json_get_float(struct spa_json *iter, float *res)
Definition json.h:457
static int spa_json_is_object(const char *val, int len)
Definition json.h:403
static int spa_json_parse_string(const char *val, int len, char *result)
Definition json.h:627
static int spa_json_container_len(struct spa_json *iter, const char *value, int len 1)
Return length of container at current position, starting at value.
Definition json.h:390
static float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition string.h:261
static char * spa_dtoa(char *str, size_t size, double val)
Definition string.h:354
#define SPA_UNUSED
Definition defs.h:307
#define SPA_CLAMP(v, low, high)
Definition defs.h:177
#define SPA_FLAG_UPDATE(field, flag, val)
Definition defs.h:104
#define SPA_N_ELEMENTS(arr)
Definition defs.h:143
#define SPA_FLAG_IS_SET(field, flag)
Definition defs.h:90
#define SPA_UNLIKELY(x)
Definition defs.h:369
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition defs.h:84
#define SPA_FLAG_CLEAR(field, flag)
Definition defs.h:94
#define SPA_PTRDIFF(p1, p2)
Definition defs.h:238
#define _SPA_ERROR(reason)
int line
Definition defs.h:415
const char * location
Definition defs.h:418
int col
Definition defs.h:416
size_t len
Definition defs.h:417
const char * reason
Definition defs.h:419
uint32_t depth
Definition json.h:45
const char * cur
Definition json.h:39
uint32_t state
Definition json.h:44
const char * end
Definition json.h:40
struct spa_json * parent
Definition json.h:41