json-pod.h
Go to the documentation of this file.
/* Simple Plugin API
*
* Copyright © 2022 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_UTILS_JSON_POD_H
#define SPA_UTILS_JSON_POD_H
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/utils/string.h>
#include <spa/utils/json.h>
#include <spa/pod/pod.h>
#include <spa/pod/builder.h>
#include <spa/debug/types.h>
static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id,
const struct spa_type_info *info, struct spa_json *iter, const char *value, int len)
{
const struct spa_type_info *ti;
char key[256];
struct spa_pod_frame f[1];
struct spa_json it[1];
int l, res;
const char *v;
uint32_t type;
if (spa_json_is_object(value, len) && info != NULL) {
if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL)
return -EINVAL;
spa_pod_builder_push_object(b, &f[0], info->parent, id);
spa_json_enter(iter, &it[0]);
while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
const struct spa_type_info *pi;
if ((l = spa_json_next(&it[0], &v)) <= 0)
break;
if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
type = pi->type;
else if (!spa_atou32(key, &type, 0))
continue;
spa_pod_builder_prop(b, type, 0);
if ((res = spa_json_to_pod_part(b, flags, id, pi, &it[0], v, l)) < 0)
return res;
}
spa_pod_builder_pop(b, &f[0]);
}
else if (spa_json_is_array(value, len)) {
if (info == NULL || info->parent == SPA_TYPE_Struct) {
spa_pod_builder_push_struct(b, &f[0]);
} else {
spa_pod_builder_push_array(b, &f[0]);
info = info->values;
}
spa_json_enter(iter, &it[0]);
while ((l = spa_json_next(&it[0], &v)) > 0)
if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
return res;
spa_pod_builder_pop(b, &f[0]);
}
else if (spa_json_is_float(value, len)) {
float val = 0.0f;
spa_json_parse_float(value, len, &val);
switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
case SPA_TYPE_Bool:
spa_pod_builder_bool(b, val >= 0.5f);
break;
case SPA_TYPE_Id:
spa_pod_builder_id(b, val);
break;
case SPA_TYPE_Int:
spa_pod_builder_int(b, val);
break;
case SPA_TYPE_Long:
spa_pod_builder_long(b, val);
break;
case SPA_TYPE_Struct:
if (spa_json_is_int(value, len))
spa_pod_builder_int(b, val);
else
spa_pod_builder_float(b, val);
break;
case SPA_TYPE_Float:
spa_pod_builder_float(b, val);
break;
case SPA_TYPE_Double:
spa_pod_builder_double(b, val);
break;
default:
spa_pod_builder_none(b);
break;
}
}
else if (spa_json_is_bool(value, len)) {
bool val = false;
spa_json_parse_bool(value, len, &val);
spa_pod_builder_bool(b, val);
}
else if (spa_json_is_null(value, len)) {
spa_pod_builder_none(b);
}
else {
char *val = (char*)alloca(len+1);
spa_json_parse_stringn(value, len, val, len+1);
switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
case SPA_TYPE_Id:
if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
type = ti->type;
else if (!spa_atou32(val, &type, 0))
return -EINVAL;
spa_pod_builder_id(b, type);
break;
case SPA_TYPE_Struct:
case SPA_TYPE_String:
spa_pod_builder_string(b, val);
break;
default:
spa_pod_builder_none(b);
break;
}
}
return 0;
}
static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
const struct spa_type_info *info, const char *value, int len)
{
struct spa_json iter;
const char *val;
spa_json_init(&iter, value, len);
if ((len = spa_json_next(&iter, &val)) <= 0)
return -EINVAL;
return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_UTILS_JSON_POD_H */
builder.h
spa/pod/builder.h
spa_debug_type_find
static const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition: types.h:46
spa_debug_type_find_short
static const struct spa_type_info * spa_debug_type_find_short(const struct spa_type_info *info, const char *name)
Definition: types.h:104
spa_json_to_pod
static int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags, const struct spa_type_info *info, const char *value, int len)
Definition: json-pod.h:161
spa_json_to_pod_part
static int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id, const struct spa_type_info *info, struct spa_json *iter, const char *value, int len)
Definition: json-pod.h:52
spa_json_is_float
static bool spa_json_is_float(const char *val, int len)
Definition: json.h:251
spa_json_parse_float
static int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:245
spa_json_parse_stringn
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition: json.h:325
spa_json_enter
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:73
spa_json_parse_bool
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:302
spa_json_get_string
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:376
spa_json_is_bool
static bool spa_json_is_bool(const char *val, int len)
Definition: json.h:297
spa_json_is_array
static bool spa_json_is_array(const char *val, int len)
Definition: json.h:229
spa_json_is_null
static bool spa_json_is_null(const char *val, int len)
Definition: json.h:239
spa_json_next
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:83
spa_json_init
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:66
spa_json_is_int
static bool spa_json_is_int(const char *val, int len)
Definition: json.h:272
spa_json_is_object
static int spa_json_is_object(const char *val, int len)
Definition: json.h:219
spa_pod_builder_prop
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition: builder.h:468
spa_pod_builder_string
static int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition: builder.h:323
spa_pod_builder_float
static int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition: builder.h:283
spa_pod_builder_pop
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:186
spa_pod_builder_double
static int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition: builder.h:292
spa_pod_builder_id
static int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition: builder.h:256
spa_pod_builder_none
static int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition: builder.h:231
spa_pod_builder_push_array
static int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:390
spa_pod_builder_push_struct
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:440
spa_pod_builder_bool
static int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition: builder.h:247
spa_pod_builder_int
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition: builder.h:265
spa_pod_builder_push_object
static int spa_pod_builder_push_object(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t type, uint32_t id)
Definition: builder.h:453
spa_pod_builder_long
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition: builder.h:274
spa_atou32
static bool spa_atou32(const char *str, uint32_t *val, int base)
Convert str to an uint32_t with the given base and store the result in val.
Definition: string.h:146
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition: type.h:54
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition: type.h:55
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition: type.h:52
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition: type.h:56
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition: type.h:57
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition: type.h:53
SPA_TYPE_String
@ SPA_TYPE_String
Definition: type.h:58
SPA_TYPE_Struct
@ SPA_TYPE_Struct
Definition: type.h:64
json.h
spa/utils/json.h
pod.h
spa/pod/pod.h
string.h
spa/utils/string.h
spa_json
Definition: json.h:55
spa_pod_builder
Definition: builder.h:73
spa_pod_frame
Definition: iter.h:47
spa_type_info
Definition: type.h:162
spa_type_info::type
uint32_t type
Definition: type.h:163
spa_type_info::parent
uint32_t parent
Definition: type.h:164
spa_type_info::values
const struct spa_type_info * values
Definition: type.h:166
types.h
spa/debug/types.h