25 #ifndef SPA_UTILS_JSON_H
26 #define SPA_UTILS_JSON_H
58 #define SPA_JSON_INIT(data,size) (struct spa_json) { (data), (data)+(size), }
64 #define SPA_JSON_ENTER(iter) (struct spa_json) { (iter)->cur, (iter)->end, (iter), }
71 #define SPA_JSON_SAVE(iter) (struct spa_json) { (iter)->cur, (iter)->end, }
78 enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT };
81 for (; iter->
cur < iter->
end; iter->
cur++) {
82 unsigned char cur = (
unsigned char)*iter->
cur;
84 switch (iter->
state) {
86 iter->
state = __STRUCT;
91 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ':':
case '=':
case ',':
94 iter->
state = __COMMENT;
98 iter->
state = __STRING;
102 if (++iter->
depth > 1)
107 if (iter->
depth == 0) {
116 iter->
state = __BARE;
121 case '\t':
case ' ':
case '\r':
case '\n':
122 case ':':
case ',':
case '=':
case ']':
case '}':
123 iter->
state = __STRUCT;
126 return iter->
cur - *value;
135 iter->
state = __STRUCT;
138 return ++iter->
cur - *value;
147 iter->
state = __UTF8;
150 if (
cur >= 32 &&
cur <= 126)
157 if (--utf8_remain == 0)
158 iter->
state = __STRING;
164 case '"':
case '\\':
case '/':
case 'b':
case 'f':
165 case 'n':
case 'r':
case 't':
case 'u':
166 iter->
state = __STRING;
172 case '\n':
case '\r':
173 iter->
state = __STRUCT;
178 if (iter->
depth != 0)
180 if (iter->
state != __STRUCT) {
181 iter->
state = __STRUCT;
182 return iter->
cur - *value;
198 return len > 0 && (*val ==
'{' || *val ==
'[');
207 return sub.
cur + 1 - value;
213 return len > 0 && *val ==
'{';
223 return len > 0 && *val ==
'[';
233 return len == 4 && strncmp(val,
"null", 4) == 0;
240 *result = strtof(val, &
end);
241 return end == val + len;
261 *result = strtol(val, &
end, 0);
262 return end == val + len;
281 return len == 4 && strncmp(val,
"true", 4) == 0;
286 return len == 5 && strncmp(val,
"false", 5) == 0;
314 return len > 1 && *val ==
'"';
324 strncpy(result, val, len);
327 for (p = val+1; p < val + len; p++) {
340 else if (*p ==
'u') {
342 uint16_t v = strtol(p+1, &
end, 16);
348 *result++ = (v >> 8) & 0xff;
349 *result++ = v & 0xff;
353 }
else if (*p ==
'\"') {
380 static const char hex[] = {
"0123456789abcdef" };
381 #define __PUT(c) { if (len < size) *str++ = c; len++; }
405 if (*val > 0 && *val < 0x20) {
408 __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:209
static bool spa_json_is_string(const char *val, int len)
Definition: json.h:320
static bool spa_json_is_float(const char *val, int len)
Definition: json.h:251
static int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:245
static bool spa_json_is_true(const char *val, int len)
Definition: json.h:287
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition: json.h:325
static int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition: json.h:195
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:73
static bool spa_json_is_false(const char *val, int len)
Definition: json.h:292
static int spa_json_get_int(struct spa_json *iter, int *res)
Definition: json.h:277
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:302
static int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:223
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:376
static bool spa_json_is_bool(const char *val, int len)
Definition: json.h:297
#define SPA_JSON_INIT(data, size)
Definition: json.h:64
static bool spa_json_is_array(const char *val, int len)
Definition: json.h:229
static int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition: json.h:310
static bool spa_json_is_null(const char *val, int len)
Definition: json.h:239
#define SPA_JSON_ENTER(iter)
Definition: json.h:71
static int spa_json_encode_string(char *str, int size, const char *val)
Definition: json.h:385
static int spa_json_parse_int(const char *val, int len, int *result)
Definition: json.h:266
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:83
static int spa_json_is_container(const char *val, int len)
Definition: json.h:204
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:66
static bool spa_json_is_int(const char *val, int len)
Definition: json.h:272
static int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:233
static int spa_json_get_float(struct spa_json *iter, float *res)
Definition: json.h:256
static int spa_json_is_object(const char *val, int len)
Definition: json.h:219
static int spa_json_parse_string(const char *val, int len, char *result)
Definition: json.h:371
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition: defs.h:76
uint32_t depth
Definition: json.h:60
const char * cur
Definition: json.h:56
uint32_t state
Definition: json.h:59
const char * end
Definition: json.h:57
struct spa_json * parent
Definition: json.h:58