PipeWire 1.3.0
Loading...
Searching...
No Matches
json-pod.h
Go to the documentation of this file.
1/* Simple Plugin API */
2/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans */
3/* SPDX-License-Identifier: MIT */
4
5#ifndef SPA_UTILS_JSON_POD_H
6#define SPA_UTILS_JSON_POD_H
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <spa/utils/string.h>
13#include <spa/utils/json.h>
14#include <spa/pod/pod.h>
15#include <spa/pod/builder.h>
16#include <spa/debug/types.h>
17
27static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id,
28 const struct spa_type_info *info, struct spa_json *iter, const char *value, int len)
29{
30 const struct spa_type_info *ti;
31 char key[256];
32 struct spa_pod_frame f[1];
33 struct spa_json it[1];
34 int l, res;
35 const char *v;
36 uint32_t type;
37
38 if (spa_json_is_object(value, len) && info != NULL) {
39 if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL)
40 return -EINVAL;
41
42 spa_pod_builder_push_object(b, &f[0], info->parent, id);
43
44 spa_json_enter(iter, &it[0]);
45 while ((l = spa_json_object_next(&it[0], key, sizeof(key), &v)) > 0) {
46 const struct spa_type_info *pi;
47 if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
48 type = pi->type;
49 else if (!spa_atou32(key, &type, 0))
50 continue;
52 if ((res = spa_json_to_pod_part(b, flags, id, pi, &it[0], v, l)) < 0)
53 return res;
54 }
55 if (l < 0)
56 return l;
57 spa_pod_builder_pop(b, &f[0]);
58 }
59 else if (spa_json_is_array(value, len)) {
60 if (info == NULL || info->parent == SPA_TYPE_Struct) {
62 } else {
64 info = info->values;
65 }
66 spa_json_enter(iter, &it[0]);
67 while ((l = spa_json_next(&it[0], &v)) > 0)
68 if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
69 return res;
70 if (l < 0)
71 return l;
72 spa_pod_builder_pop(b, &f[0]);
73 }
74 else if (spa_json_is_float(value, len)) {
75 float val = 0.0f;
76 spa_json_parse_float(value, len, &val);
77 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
78 case SPA_TYPE_Bool:
79 spa_pod_builder_bool(b, val >= 0.5f);
80 break;
81 case SPA_TYPE_Id:
83 break;
84 case SPA_TYPE_Int:
86 break;
87 case SPA_TYPE_Long:
89 break;
90 case SPA_TYPE_Struct:
91 if (spa_json_is_int(value, len))
93 else
95 break;
96 case SPA_TYPE_Float:
98 break;
99 case SPA_TYPE_Double:
101 break;
102 default:
104 break;
105 }
106 }
107 else if (spa_json_is_bool(value, len)) {
108 bool val = false;
109 spa_json_parse_bool(value, len, &val);
111 }
112 else if (spa_json_is_null(value, len)) {
114 }
115 else {
116 char *val = (char*)alloca(len+1);
117 spa_json_parse_stringn(value, len, val, len+1);
118 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
119 case SPA_TYPE_Id:
120 if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
121 type = ti->type;
122 else if (!spa_atou32(val, &type, 0))
123 return -EINVAL;
125 break;
126 case SPA_TYPE_Struct:
127 case SPA_TYPE_String:
129 break;
130 default:
132 break;
133 }
134 }
135 return 0;
136}
137
138static inline int spa_json_to_pod_checked(struct spa_pod_builder *b, uint32_t flags,
139 const struct spa_type_info *info, const char *value, int len,
140 struct spa_error_location *loc)
141{
142 struct spa_json iter;
143 const char *val;
144 int res;
145
146 if (loc)
147 spa_zero(*loc);
148
149 if ((res = spa_json_begin(&iter, value, len, &val)) <= 0)
150 goto error;
151
152 res = spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
153
154error:
155 if (res < 0 && loc)
156 spa_json_get_error(&iter, value, loc);
157 return res;
158}
159
160static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
161 const struct spa_type_info *info, const char *value, int len)
162{
163 return spa_json_to_pod_checked(b, flags, info, value, len, NULL);
164}
170#ifdef __cplusplus
171} /* extern "C" */
172#endif
173
174#endif /* SPA_UTILS_JSON_POD_H */
spa/pod/builder.h
static const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition types.h:26
static const struct spa_type_info * spa_debug_type_find_short(const struct spa_type_info *info, const char *name)
Definition types.h:81
static int spa_json_to_pod_checked(struct spa_pod_builder *b, uint32_t flags, const struct spa_type_info *info, const char *value, int len, struct spa_error_location *loc)
Definition json-pod.h:143
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:165
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:32
static bool spa_json_is_float(const char *val, int len)
Definition json-core.h:418
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json-core.h:395
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json-core.h:506
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json-core.h:58
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json-core.h:472
static bool spa_json_is_bool(const char *val, int len)
Definition json-core.h:467
static int spa_json_begin(struct spa_json *iter, const char *data, size_t size, const char **val)
Definition json.h:36
static bool spa_json_get_error(struct spa_json *iter, const char *start, struct spa_error_location *loc)
Return if there was a parse error, and its possible location.
Definition json-core.h:325
static bool spa_json_is_array(const char *val, int len)
Definition json-core.h:383
static bool spa_json_is_null(const char *val, int len)
Definition json-core.h:389
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json-core.h:71
static int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
Definition json.h:140
static bool spa_json_is_int(const char *val, int len)
Definition json-core.h:450
static int spa_json_is_object(const char *val, int len)
Definition json-core.h:377
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:450
static int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:305
static int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition builder.h:265
static int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition builder.h:274
static int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition builder.h:238
static int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition builder.h:213
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:168
static int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:372
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:422
static int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition builder.h:229
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:247
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:435
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:256
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:128
@ SPA_TYPE_Int
Definition type.h:35
@ SPA_TYPE_Long
Definition type.h:36
@ SPA_TYPE_Bool
Definition type.h:33
@ SPA_TYPE_Float
Definition type.h:37
@ SPA_TYPE_Double
Definition type.h:38
@ SPA_TYPE_Id
Definition type.h:34
@ SPA_TYPE_String
Definition type.h:39
@ SPA_TYPE_Struct
Definition type.h:45
#define spa_zero(x)
Definition defs.h:483
spa/utils/json.h
spa/pod/pod.h
spa/utils/string.h
Definition defs.h:414
Definition json-core.h:38
Definition builder.h:53
Definition iter.h:27
Definition type.h:144
uint32_t type
Definition type.h:145
spa/debug/types.h