5#ifndef SPA_UTILS_JSON_H
6#define SPA_UTILS_JSON_H
41#define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), 0, 0, 0 })
47#define SPA_JSON_ENTER(iter) ((struct spa_json) { (iter)->cur, (iter)->end, (iter), 0, 0 })
54#define SPA_JSON_SAVE(iter) ((struct spa_json) { (iter)->cur, (iter)->end, })
61 enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT };
64 for (; iter->
cur < iter->
end; iter->
cur++) {
65 unsigned char cur = (
unsigned char)*iter->
cur;
67 switch (iter->
state) {
69 iter->
state = __STRUCT;
74 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ':':
case '=':
case ',':
77 iter->
state = __COMMENT;
81 iter->
state = __STRING;
85 if (++iter->
depth > 1)
90 if (iter->
depth == 0) {
104 case '\t':
case ' ':
case '\r':
case '\n':
105 case ':':
case ',':
case '=':
case ']':
case '}':
106 iter->
state = __STRUCT;
109 return iter->
cur - *value;
118 iter->
state = __STRUCT;
121 return ++iter->
cur - *value;
130 iter->
state = __UTF8;
133 if (
cur >= 32 &&
cur <= 126)
140 if (--utf8_remain == 0)
141 iter->
state = __STRING;
147 case '"':
case '\\':
case '/':
case 'b':
case 'f':
148 case 'n':
case 'r':
case 't':
case 'u':
149 iter->
state = __STRING;
155 case '\n':
case '\r':
156 iter->
state = __STRUCT;
161 if (iter->
depth != 0)
163 if (iter->
state != __STRUCT) {
164 iter->
state = __STRUCT;
165 return iter->
cur - *value;
181 return len > 0 && (*val ==
'{' || *val ==
'[');
190 return sub.
cur + 1 - value;
196 return len > 0 && *val ==
'{';
206 return len > 0 && *val ==
'[';
216 return len == 4 && strncmp(val,
"null", 4) == 0;
223 if (strspn(val,
"+-0123456789.Ee") < (
size_t)len)
226 return len > 0 &&
end == val + len;
248 else if (val == -INFINITY)
260 *result = strtol(val, &
end, 0);
261 return len > 0 &&
end == val + len;
280 return len == 4 && strncmp(val,
"true", 4) == 0;
285 return len == 5 && strncmp(val,
"false", 5) == 0;
313 return len > 1 && *val ==
'"';
320 for (i = 0; i < num; i++) {
322 if (v >=
'0' && v <=
'9')
324 else if (v >=
'a' && v <=
'f')
326 else if (v >=
'A' && v <=
'F')
330 *res = (*res << 4) | v;
342 memmove(result, val, len);
345 for (p = val+1; p < val + len; p++) {
358 else if (*p ==
'u') {
359 uint8_t prefix[] = { 0, 0xc0, 0xe0, 0xf0 };
360 uint32_t idx, n, v, cp, enc[] = { 0x80, 0x800, 0x10000 };
361 if (val + len - p < 5 ||
368 if (cp >= 0xd800 && cp <= 0xdbff) {
369 if (val + len - p < 7 ||
370 p[1] !=
'\\' || p[2] !=
'u' ||
372 v < 0xdc00 || v > 0xdfff)
375 cp = 0x010000 + (((cp & 0x3ff) << 10) | (v & 0x3ff));
376 }
else if (cp >= 0xdc00 && cp <= 0xdfff)
379 for (idx = 0; idx < 3; idx++)
382 for (n = idx; n > 0; n--, cp >>= 6)
383 result[n] = (cp | 0x80) & 0xbf;
384 *result++ = (cp | prefix[idx]) & 0xff;
388 }
else if (*p ==
'\"') {
415 static const char hex[] = {
"0123456789abcdef" };
416#define __PUT(c) { if (len < size) *str++ = c; len++; }
440 if (*val > 0 && *val < 0x20) {
443 __PUT(hex[((*val)>>4)&0xf]);
__PUT(hex[(*val)&0xf]);
static bool spa_json_is_string(const char *val, int len)
Definition json.h:319
static bool spa_json_is_float(const char *val, int len)
Definition json.h:237
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json.h:228
static bool spa_json_is_true(const char *val, int len)
Definition json.h:286
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json.h:343
static int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition json.h:178
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json.h:56
static int spa_json_parse_hex(const char *p, int num, uint32_t *res)
Definition json.h:324
static bool spa_json_is_false(const char *val, int len)
Definition json.h:291
static int spa_json_get_int(struct spa_json *iter, int *res)
Definition json.h:276
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json.h:301
static int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition json.h:206
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:411
static bool spa_json_is_bool(const char *val, int len)
Definition json.h:296
#define SPA_JSON_INIT(data, size)
Definition json.h:47
static char * spa_json_format_float(char *str, int size, float val)
Definition json.h:251
static bool spa_json_is_array(const char *val, int len)
Definition json.h:212
static int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition json.h:309
static bool spa_json_is_null(const char *val, int len)
Definition json.h:222
#define SPA_JSON_ENTER(iter)
Definition json.h:54
static int spa_json_encode_string(char *str, int size, const char *val)
Definition json.h:420
static int spa_json_parse_int(const char *val, int len, int *result)
Definition json.h:265
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json.h:66
static int spa_json_is_container(const char *val, int len)
Definition json.h:187
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json.h:49
static bool spa_json_is_int(const char *val, int len)
Definition json.h:271
static int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition json.h:216
static int spa_json_get_float(struct spa_json *iter, float *res)
Definition json.h:242
static int spa_json_is_object(const char *val, int len)
Definition json.h:202
static int spa_json_parse_string(const char *val, int len, char *result)
Definition json.h:406
static int spa_json_container_len(struct spa_json *iter, const char *value, int len 1)
Definition json.h:192
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:289
#define SPA_UNLIKELY(x)
Definition defs.h:351
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition defs.h:70
uint32_t depth
Definition json.h:43
const char * cur
Definition json.h:39
uint32_t state
Definition json.h:42
const char * end
Definition json.h:40
struct spa_json * parent
Definition json.h:41