Loading...
Searching...
No Matches
json.h
Go to the documentation of this file.
/* Simple Plugin API */
/* SPDX-FileCopyrightText: Copyright © 2020 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_UTILS_JSON_UTILS_H
#define SPA_UTILS_JSON_UTILS_H
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include <spa/utils/json-core.h>
#ifdef __cplusplus
extern "C" {
#else
#include <stdbool.h>
#endif
#ifndef SPA_API_JSON_UTILS
#ifdef SPA_API_IMPL
#define SPA_API_JSON_UTILS SPA_API_IMPL
#else
#define SPA_API_JSON_UTILS static inline
#endif
#endif
SPA_API_JSON_UTILS int spa_json_begin(struct spa_json * iter, const char *data, size_t size, const char **val)
{
spa_json_init(iter, data, size);
return spa_json_next(iter, val);
}
/* float */
SPA_API_JSON_UTILS int spa_json_get_float(struct spa_json *iter, float *res)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return len;
return spa_json_parse_float(value, len, res);
}
/* int */
SPA_API_JSON_UTILS int spa_json_get_int(struct spa_json *iter, int *res)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return len;
return spa_json_parse_int(value, len, res);
}
/* bool */
SPA_API_JSON_UTILS int spa_json_get_bool(struct spa_json *iter, bool *res)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return len;
return spa_json_parse_bool(value, len, res);
}
/* string */
SPA_API_JSON_UTILS int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return len;
return spa_json_parse_stringn(value, len, res, maxlen);
}
SPA_API_JSON_UTILS int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
{
const char *value;
int len;
if ((len = spa_json_next(iter, &value)) <= 0)
return len;
if (!spa_json_is_container(value, len))
return -EPROTO;
if (*value != type)
return -EINVAL;
spa_json_enter(iter, sub);
return 1;
}
SPA_API_JSON_UTILS int spa_json_begin_container(struct spa_json * iter,
const char *data, size_t size, char type, bool relax)
{
int res;
spa_json_init(iter, data, size);
res = spa_json_enter_container(iter, iter, type);
if (res == -EPROTO && relax)
spa_json_init_relax(iter, type, data, size);
else if (res <= 0)
return res;
return 1;
}
SPA_API_JSON_UTILS int spa_json_container_len(struct spa_json *iter, const char *value, int len SPA_UNUSED)
{
const char *val;
struct spa_json sub;
int res;
spa_json_enter(iter, &sub);
while ((res = spa_json_next(&sub, &val)) > 0);
if (res < 0)
return 0;
return sub.cur + 1 - value;
}
/* object */
SPA_API_JSON_UTILS int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
{
return spa_json_enter_container(iter, sub, '{');
}
SPA_API_JSON_UTILS int spa_json_begin_object_relax(struct spa_json * iter, const char *data, size_t size)
{
return spa_json_begin_container(iter, data, size, '{', true);
}
SPA_API_JSON_UTILS int spa_json_begin_object(struct spa_json * iter, const char *data, size_t size)
{
return spa_json_begin_container(iter, data, size, '{', false);
}
SPA_API_JSON_UTILS int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
{
int res1, res2;
while (true) {
res1 = spa_json_get_string(iter, key, maxkeylen);
if (res1 <= 0 && res1 != -ENOSPC)
return res1;
res2 = spa_json_next(iter, value);
if (res2 <= 0 || res1 != -ENOSPC)
return res2;
}
}
SPA_API_JSON_UTILS int spa_json_object_find(struct spa_json *iter, const char *key, const char **value)
{
struct spa_json obj = SPA_JSON_SAVE(iter);
int res, len = strlen(key) + 3;
char k[len];
while ((res = spa_json_object_next(&obj, k, len, value)) > 0)
if (spa_streq(k, key))
return res;
return -ENOENT;
}
SPA_API_JSON_UTILS int spa_json_str_object_find(const char *obj, size_t obj_len,
const char *key, char *value, size_t maxlen)
{
struct spa_json iter;
int l;
const char *v;
if (spa_json_begin_object(&iter, obj, obj_len) <= 0)
return -EINVAL;
if ((l = spa_json_object_find(&iter, key, &v)) <= 0)
return l;
return spa_json_parse_stringn(v, l, value, maxlen);
}
/* array */
SPA_API_JSON_UTILS int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
{
return spa_json_enter_container(iter, sub, '[');
}
SPA_API_JSON_UTILS int spa_json_begin_array_relax(struct spa_json * iter, const char *data, size_t size)
{
return spa_json_begin_container(iter, data, size, '[', true);
}
SPA_API_JSON_UTILS int spa_json_begin_array(struct spa_json * iter, const char *data, size_t size)
{
return spa_json_begin_container(iter, data, size, '[', false);
}
#define spa_json_make_str_array_unpack(maxlen,type,conv) \
{ \
struct spa_json iter; \
char v[maxlen]; \
uint32_t count = 0; \
if (spa_json_begin_array_relax(&iter, arr, arr_len) <= 0) \
return -EINVAL; \
while (spa_json_get_string(&iter, v, sizeof(v)) > 0 && count < max) \
values[count++] = conv(v); \
return count; \
}
SPA_API_JSON_UTILS int spa_json_str_array_uint32(const char *arr, size_t arr_len,
uint32_t *values, size_t max)
{
spa_json_make_str_array_unpack(32,uint32_t, atoi);
}
* \}
*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_UTILS_JSON_UTILS_H */
res
uint32_t int int res
Definition core.h:433
spa_json_str_object_find
SPA_API_JSON_UTILS int spa_json_str_object_find(const char *obj, size_t obj_len, const char *key, char *value, size_t maxlen)
Definition json.h:176
spa_json_enter_array
SPA_API_JSON_UTILS int spa_json_enter_array(struct spa_json *iter, struct spa_json *sub)
Definition json.h:191
SPA_JSON_SAVE
#define SPA_JSON_SAVE(iter)
Definition json-core.h:84
spa_json_begin_array
SPA_API_JSON_UTILS int spa_json_begin_array(struct spa_json *iter, const char *data, size_t size)
Definition json.h:199
spa_json_object_next
SPA_API_JSON_UTILS int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
Definition json.h:151
spa_json_begin_object_relax
SPA_API_JSON_UTILS int spa_json_begin_object_relax(struct spa_json *iter, const char *data, size_t size)
Definition json.h:142
spa_json_init
SPA_API_JSON void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json-core.h:62
spa_json_begin_object
SPA_API_JSON_UTILS int spa_json_begin_object(struct spa_json *iter, const char *data, size_t size)
Definition json.h:146
spa_json_begin
SPA_API_JSON_UTILS int spa_json_begin(struct spa_json *iter, const char *data, size_t size, const char **val)
Definition json.h:47
spa_json_enter_object
SPA_API_JSON_UTILS int spa_json_enter_object(struct spa_json *iter, struct spa_json *sub)
Definition json.h:138
spa_json_begin_array_relax
SPA_API_JSON_UTILS int spa_json_begin_array_relax(struct spa_json *iter, const char *data, size_t size)
Definition json.h:195
spa_json_next
SPA_API_JSON int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json-core.h:101
spa_json_parse_float
SPA_API_JSON int spa_json_parse_float(const char *val, int len, float *result)
Definition json-core.h:434
spa_json_get_int
SPA_API_JSON_UTILS int spa_json_get_int(struct spa_json *iter, int *res)
Definition json.h:64
spa_json_parse_int
SPA_API_JSON int spa_json_parse_int(const char *val, int len, int *result)
Definition json-core.h:475
spa_json_container_len
SPA_API_JSON_UTILS int spa_json_container_len(struct spa_json *iter, const char *value, int len 1)
Return length of container at current position, starting at value.
Definition json.h:125
spa_json_get_bool
SPA_API_JSON_UTILS int spa_json_get_bool(struct spa_json *iter, bool *res)
Definition json.h:74
spa_json_object_find
SPA_API_JSON_UTILS int spa_json_object_find(struct spa_json *iter, const char *key, const char **value)
Definition json.h:164
spa_json_init_relax
SPA_API_JSON void spa_json_init_relax(struct spa_json *iter, char type, const char *data, size_t size)
Definition json-core.h:70
spa_json_parse_bool
SPA_API_JSON int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json-core.h:511
spa_json_str_array_uint32
SPA_API_JSON_UTILS int spa_json_str_array_uint32(const char *arr, size_t arr_len, uint32_t *values, size_t max)
Definition json.h:216
spa_json_parse_stringn
SPA_API_JSON int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json-core.h:545
spa_json_enter
SPA_API_JSON void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json-core.h:78
spa_json_enter_container
SPA_API_JSON_UTILS int spa_json_enter_container(struct spa_json *iter, struct spa_json *sub, char type)
Definition json.h:94
spa_json_make_str_array_unpack
#define spa_json_make_str_array_unpack(maxlen, type, conv)
Definition json.h:204
spa_json_begin_container
SPA_API_JSON_UTILS int spa_json_begin_container(struct spa_json *iter, const char *data, size_t size, char type, bool relax)
Definition json.h:108
spa_json_is_container
SPA_API_JSON int spa_json_is_container(const char *val, int len)
Definition json-core.h:410
spa_json_get_string
SPA_API_JSON_UTILS int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:84
spa_json_get_float
SPA_API_JSON_UTILS int spa_json_get_float(struct spa_json *iter, float *res)
Definition json.h:54
spa_streq
SPA_API_STRING bool spa_streq(const char *s1, const char *s2)
Definition string.h:52
SPA_UNUSED
#define SPA_UNUSED
Definition defs.h:309
json-core.h
spa/utils/json-core.h
SPA_API_JSON_UTILS
#define SPA_API_JSON_UTILS
Definition json.h:34
string.h
spa/utils/string.h
spa_json
Definition json-core.h:49
spa_json::cur
const char * cur
Definition json-core.h:50