25 #ifndef SPA_UTILS_JSON_H
26 #define SPA_UTILS_JSON_H
61 #define SPA_JSON_INIT(data,size) ((struct spa_json) { (data), (data)+(size), })
67 #define SPA_JSON_ENTER(iter) ((struct spa_json) { (iter)->cur, (iter)->end, (iter), })
74 #define SPA_JSON_SAVE(iter) ((struct spa_json) { (iter)->cur, (iter)->end, })
81 enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT };
84 for (; iter->
cur < iter->
end; iter->
cur++) {
85 unsigned char cur = (
unsigned char)*iter->
cur;
87 switch (iter->
state) {
89 iter->
state = __STRUCT;
94 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ':':
case '=':
case ',':
97 iter->
state = __COMMENT;
101 iter->
state = __STRING;
105 if (++iter->
depth > 1)
110 if (iter->
depth == 0) {
119 iter->
state = __BARE;
124 case '\t':
case ' ':
case '\r':
case '\n':
125 case ':':
case ',':
case '=':
case ']':
case '}':
126 iter->
state = __STRUCT;
129 return iter->
cur - *value;
138 iter->
state = __STRUCT;
141 return ++iter->
cur - *value;
150 iter->
state = __UTF8;
153 if (
cur >= 32 &&
cur <= 126)
160 if (--utf8_remain == 0)
161 iter->
state = __STRING;
167 case '"':
case '\\':
case '/':
case 'b':
case 'f':
168 case 'n':
case 'r':
case 't':
case 'u':
169 iter->
state = __STRING;
175 case '\n':
case '\r':
176 iter->
state = __STRUCT;
181 if (iter->
depth != 0)
183 if (iter->
state != __STRUCT) {
184 iter->
state = __STRUCT;
185 return iter->
cur - *value;
201 return len > 0 && (*val ==
'{' || *val ==
'[');
210 return sub.
cur + 1 - value;
216 return len > 0 && *val ==
'{';
226 return len > 0 && *val ==
'[';
236 return len == 4 && strncmp(val,
"null", 4) == 0;
243 if (strspn(val,
"+-0123456789.Ee") < (
size_t)len)
246 return len > 0 &&
end == val + len;
268 else if (val == -INFINITY)
280 *result = strtol(val, &
end, 0);
281 return len > 0 &&
end == val + len;
300 return len == 4 && strncmp(val,
"true", 4) == 0;
305 return len == 5 && strncmp(val,
"false", 5) == 0;
333 return len > 1 && *val ==
'"';
340 for (i = 0; i < num; i++) {
342 if (v >=
'0' && v <=
'9')
344 else if (v >=
'a' && v <=
'f')
346 else if (v >=
'A' && v <=
'F')
350 *res = (*res << 4) | v;
362 strncpy(result, val, len);
365 for (p = val+1; p < val + len; p++) {
378 else if (*p ==
'u') {
379 uint8_t prefix[] = { 0, 0xc0, 0xe0, 0xf0 };
380 uint32_t idx, n, v, cp, enc[] = { 0x80, 0x800, 0x10000 };
381 if (val + len - p < 5 ||
388 if (cp >= 0xd800 && cp <= 0xdbff) {
389 if (val + len - p < 7 ||
390 p[1] !=
'\\' || p[2] !=
'u' ||
392 v < 0xdc00 || v > 0xdfff)
395 cp = 0x010000 | ((cp & 0x3ff) << 10) | (v & 0x3ff);
396 }
else if (cp >= 0xdc00 && cp <= 0xdfff)
399 for (idx = 0; idx < 3; idx++)
402 for (n = idx; n > 0; n--, cp >>= 6)
403 result[n] = (cp | 0x80) & 0xbf;
404 *result++ = (cp | prefix[idx]) & 0xff;
408 }
else if (*p ==
'\"') {
435 static const char hex[] = {
"0123456789abcdef" };
436 #define __PUT(c) { if (len < size) *str++ = c; len++; }
460 if (*val > 0 && *val < 0x20) {
463 __PUT(hex[((*val)>>4)&0xf]);
__PUT(hex[(*val)&0xf]);
static int spa_json_container_len(struct spa_json *iter, const char *value, int len)
Definition: json.h:212
static bool spa_json_is_string(const char *val, int len)
Definition: json.h:339
static bool spa_json_is_float(const char *val, int len)
Definition: json.h:257
static int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:248
static bool spa_json_is_true(const char *val, int len)
Definition: json.h:306
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition: json.h:363
static int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition: json.h:198
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:76
static int spa_json_parse_hex(const char *p, int num, uint32_t *res)
Definition: json.h:344
static bool spa_json_is_false(const char *val, int len)
Definition: json.h:311
static int spa_json_get_int(struct spa_json *iter, int *res)
Definition: json.h:296
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:321
static int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:226
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:431
static bool spa_json_is_bool(const char *val, int len)
Definition: json.h:316
#define SPA_JSON_INIT(data, size)
Definition: json.h:67
static bool spa_json_is_array(const char *val, int len)
Definition: json.h:232
static int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition: json.h:329
static bool spa_json_is_null(const char *val, int len)
Definition: json.h:242
#define SPA_JSON_ENTER(iter)
Definition: json.h:74
static int spa_json_encode_string(char *str, int size, const char *val)
Definition: json.h:440
static char * spa_json_format_float(char *str, int size, float val)
Definition: json.h:271
static int spa_json_parse_int(const char *val, int len, int *result)
Definition: json.h:285
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:86
static int spa_json_is_container(const char *val, int len)
Definition: json.h:207
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:69
static bool spa_json_is_int(const char *val, int len)
Definition: json.h:291
static int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:236
static int spa_json_get_float(struct spa_json *iter, float *res)
Definition: json.h:262
static int spa_json_is_object(const char *val, int len)
Definition: json.h:222
static int spa_json_parse_string(const char *val, int len, char *result)
Definition: json.h:426
static float spa_strtof(const char *str, char **endptr)
Convert str to a float in the C locale.
Definition: string.h:281
static char * spa_dtoa(char *str, size_t size, double val)
Definition: string.h:374
#define SPA_UNLIKELY(x)
Definition: defs.h:355
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition: defs.h:86
uint32_t depth
Definition: json.h:63
const char * cur
Definition: json.h:59
uint32_t state
Definition: json.h:62
const char * end
Definition: json.h:60
struct spa_json * parent
Definition: json.h:61