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), }
70 #define SPA_JSON_SAVE(iter) (struct spa_json) { (iter)->cur, (iter)->end, }
77 enum { __NONE, __STRUCT, __BARE, __STRING, __UTF8, __ESC, __COMMENT };
80 for (; iter->
cur < iter->
end; iter->
cur++) {
81 unsigned char cur = (
unsigned char)*iter->
cur;
83 switch (iter->
state) {
85 iter->
state = __STRUCT;
90 case '\0':
case '\t':
case ' ':
case '\r':
case '\n':
case ':':
case '=':
case ',':
93 iter->
state = __COMMENT;
97 iter->
state = __STRING;
101 if (++iter->
depth > 1)
106 if (iter->
depth == 0) {
115 iter->
state = __BARE;
120 case '\t':
case ' ':
case '\r':
case '\n':
121 case ':':
case ',':
case '=':
case ']':
case '}':
122 iter->
state = __STRUCT;
125 return iter->
cur - *value;
134 iter->
state = __STRUCT;
137 return ++iter->
cur - *value;
146 iter->
state = __UTF8;
149 if (
cur >= 32 &&
cur <= 126)
156 if (--utf8_remain == 0)
157 iter->
state = __STRING;
163 case '"':
case '\\':
case '/':
case 'b':
case 'f':
164 case 'n':
case 'r':
case 't':
case 'u':
165 iter->
state = __STRING;
171 case '\n':
case '\r':
172 iter->
state = __STRUCT;
177 if (iter->
depth != 0)
179 if (iter->
state != __STRUCT) {
180 iter->
state = __STRUCT;
181 return iter->
cur - *value;
197 return len > 0 && (*val ==
'{' || *val ==
'[');
206 return sub.
cur + 1 - value;
212 return len > 0 && *val ==
'{';
222 return len > 0 && *val ==
'[';
232 return len == 4 && strncmp(val,
"null", 4) == 0;
239 *result = strtof(val, &
end);
240 return end == val + len;
260 *result = strtol(val, &
end, 0);
261 return 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 ==
'"';
321 strncpy(result, val, len);
324 for (p = val+1; p < val + len; p++) {
337 else if (*p ==
'u') {
339 uint16_t v = strtol(p+1, &
end, 16);
345 *result++ = (v >> 8) & 0xff;
346 *result++ = v & 0xff;
350 }
else if (*p ==
'\"') {
364 if ((len =
spa_json_next(iter, &value)) <= 0 || maxlen <= len)
372 static const char hex[] = {
"0123456789abcdef" };
373 #define __PUT(c) { if (len < size) *str++ = c; len++; }
397 if (*val > 0 && *val < 0x20) {
400 __PUT(hex[((*val)>>4)&0xf]);
__PUT(hex[(*val)&0xf]);
int spa_json_get_int(struct spa_json *iter, int *res)
Definition: json.h:268
bool spa_json_is_true(const char *val, int len)
Definition: json.h:278
int spa_json_parse_int(const char *val, int len, int *result)
Definition: json.h:257
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:200
int spa_json_get_float(struct spa_json *iter, float *res)
Definition: json.h:247
int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:214
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:186
const char * cur
Definition: json.h:50
bool spa_json_is_bool(const char *val, int len)
Definition: json.h:288
int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:236
int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:293
bool spa_json_is_float(const char *val, int len)
Definition: json.h:242
int spa_json_is_container(const char *val, int len)
Definition: json.h:195
const char * end
Definition: json.h:51
int spa_json_is_object(const char *val, int len)
Definition: json.h:210
bool spa_json_is_null(const char *val, int len)
Definition: json.h:230
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:316
int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:360
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:74
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:311
int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:224
int spa_json_encode_string(char *str, int size, const char *val)
Definition: json.h:369
int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition: json.h:301
bool spa_json_is_array(const char *val, int len)
Definition: json.h:220
bool spa_json_is_int(const char *val, int len)
Definition: json.h:263
bool spa_json_is_false(const char *val, int len)
Definition: json.h:283