Go to the documentation of this file.
25 #ifndef SPA_UTILS_JSON_H
26 #define SPA_UTILS_JSON_H
57 #define SPA_JSON_INIT(data,size) (struct spa_json) { (data), (data)+(size), }
63 #define SPA_JSON_ENTER(iter) (struct spa_json) { (iter)->cur, (iter)->end, (iter), }
75 enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT };
78 for (; iter->
cur < iter->
end; iter->
cur++) {
79 unsigned char cur = (
unsigned char)*iter->
cur;
81 switch (iter->
state) {
83 iter->
state = __STRUCT;
88 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ':':
case '=':
case ',':
91 iter->
state = __COMMENT;
95 iter->
state = __STRING;
99 if (++iter->
depth > 1)
104 if (iter->
depth == 0) {
113 iter->
state = __BARE;
118 case '\t':
case ' ':
case '\r':
case '\n':
119 case ':':
case ',':
case '=':
case ']':
case '}':
120 iter->
state = __STRUCT;
123 return iter->
cur - *value;
132 iter->
state = __STRUCT;
135 return ++iter->
cur - *value;
144 iter->
state = __UTF8;
147 if (
cur >= 32 &&
cur <= 126)
154 if (--utf8_remain == 0)
155 iter->
state = __STRING;
161 case '"':
case '\\':
case '/':
case 'b':
case 'f':
162 case 'n':
case 'r':
case 't':
case 'u':
163 iter->
state = __STRING;
169 case '\n':
case '\r':
170 iter->
state = __STRUCT;
175 if (iter->
depth != 0)
177 if (iter->
state != __STRUCT) {
178 iter->
state = __STRUCT;
179 return iter->
cur - *value;
195 return len > 0 && (*val ==
'{' || *val ==
'[');
204 return sub.
cur + 1 - value;
210 return len > 0 && *val ==
'{';
220 return len > 0 && *val ==
'[';
230 return len == 4 && strncmp(val,
"null", 4) == 0;
237 *result = strtof(val, &
end);
238 return end == val + len;
258 *result = strtol(val, &
end, 0);
259 return end == val + len;
278 return len == 4 && strncmp(val,
"true", 4) == 0;
283 return len == 5 && strncmp(val,
"false", 5) == 0;
311 return len > 1 && *val ==
'"';
319 strncpy(result, val, len);
322 for (p = val+1; p < val + len; p++) {
335 else if (*p ==
'u') {
337 uint16_t v = strtol(p+1, &
end, 16);
343 *result++ = (v >> 8) & 0xff;
344 *result++ = v & 0xff;
348 }
else if (*p ==
'\"') {
362 if ((len =
spa_json_next(iter, &value)) <= 0 || maxlen <= len)
370 static const char hex[] = {
"0123456789abcdef" };
371 #define __PUT(c) { if (len < size) *str++ = c; len++; }
395 if (*val > 0 && *val < 0x20) {
398 __PUT(hex[((*val)>>4)&0xf]);
__PUT(hex[(*val)&0xf]);
int spa_json_get_int(struct spa_json *iter, int *res)
Definition: json.h:266
bool spa_json_is_true(const char *val, int len)
Definition: json.h:276
int spa_json_parse_int(const char *val, int len, int *result)
Definition: json.h:255
struct spa_json * parent
Definition: json.h:52
#define SPA_JSON_INIT(data, size)
Definition: json.h:57
int spa_json_container_len(struct spa_json *iter, const char *value, int len)
Definition: json.h:198
int spa_json_get_float(struct spa_json *iter, float *res)
Definition: json.h:245
int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:212
user data to add to an object
Definition: filter.c:75
int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition: json.h:184
const char * cur
Definition: json.h:50
bool spa_json_is_bool(const char *val, int len)
Definition: json.h:286
int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:234
int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:291
bool spa_json_is_float(const char *val, int len)
Definition: json.h:240
int spa_json_is_container(const char *val, int len)
Definition: json.h:193
const char * end
Definition: json.h:51
int spa_json_is_object(const char *val, int len)
Definition: json.h:208
bool spa_json_is_null(const char *val, int len)
Definition: json.h:228
void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:59
int spa_json_parse_string(const char *val, int len, char *result)
Definition: json.h:314
int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:358
uint32_t state
Definition: json.h:53
void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:65
int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:72
uint32_t depth
Definition: json.h:54
#define SPA_FALLTHROUGH
SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch cases that fall through w...
Definition: defs.h:69
#define SPA_JSON_ENTER(iter)
Definition: json.h:63
bool spa_json_is_string(const char *val, int len)
Definition: json.h:309
int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:222
int spa_json_encode_string(char *str, int size, const char *val)
Definition: json.h:367
int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition: json.h:299
bool spa_json_is_array(const char *val, int len)
Definition: json.h:218
bool spa_json_is_int(const char *val, int len)
Definition: json.h:261
bool spa_json_is_false(const char *val, int len)
Definition: json.h:281