PipeWire 1.6.8

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#include <spa/utils/string.h>
9#include <spa/utils/json.h>
10#include <spa/pod/pod.h>
11#include <spa/pod/builder.h>
12#include <spa/debug/types.h>
13
14#ifdef __cplusplus
15 extern "C" {
16#endif
17
18#ifndef SPA_API_JSON_POD
19 #ifdef SPA_API_IMPL
20 #define SPA_API_JSON_POD SPA_API_IMPL
21 #else
22 #define SPA_API_JSON_POD static inline
23 #endif
24#endif
25
29
34
35 SPA_API_JSON_POD int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id,
36 const struct spa_type_info *info, struct spa_json *iter, const char *value, int len)
37{
38 const struct spa_type_info *ti;
39 char key[256];
40 struct spa_pod_frame f[1];
41 struct spa_json it[1];
42 int l, res;
43 const char *v;
44 uint32_t type;
45
46 if (spa_json_is_object(value, len) && info != NULL) {
47 if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL)
48 return -EINVAL;
49
50 spa_pod_builder_push_object(b, &f[0], info->parent, id);
51
52 spa_json_enter(iter, &it[0]);
53 while ((l = spa_json_object_next(&it[0], key, sizeof(key), &v)) > 0) {
54 const struct spa_type_info *pi;
55 if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
56 type = pi->type;
57 else if (!spa_atou32(key, &type, 0))
58 continue;
59 spa_pod_builder_prop(b, type, 0);
60 if ((res = spa_json_to_pod_part(b, flags, id, pi, &it[0], v, l)) < 0)
61 return res;
62 }
63 if (l < 0)
64 return l;
65 spa_pod_builder_pop(b, &f[0]);
66 }
67 else if (spa_json_is_array(value, len)) {
68 if (info == NULL || info->parent == SPA_TYPE_Struct) {
69 spa_pod_builder_push_struct(b, &f[0]);
70 } else {
71 spa_pod_builder_push_array(b, &f[0]);
72 info = info->values;
73 }
74 spa_json_enter(iter, &it[0]);
75 while ((l = spa_json_next(&it[0], &v)) > 0)
76 if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
77 return res;
78 if (l < 0)
79 return l;
80 spa_pod_builder_pop(b, &f[0]);
81 }
82 else if (spa_json_is_float(value, len)) {
83 float val = 0.0f;
84 spa_json_parse_float(value, len, &val);
85 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
86 case SPA_TYPE_Bool:
87 spa_pod_builder_bool(b, val >= 0.5f);
88 break;
89 case SPA_TYPE_Id:
90 spa_pod_builder_id(b, (uint32_t)val);
91 break;
92 case SPA_TYPE_Int:
93 spa_pod_builder_int(b, (int32_t)val);
94 break;
95 case SPA_TYPE_Long:
96 spa_pod_builder_long(b, (int64_t)val);
97 break;
98 case SPA_TYPE_Struct:
99 if (spa_json_is_int(value, len))
100 spa_pod_builder_int(b, (int32_t)val);
101 else
102 spa_pod_builder_float(b, val);
103 break;
104 case SPA_TYPE_Float:
105 spa_pod_builder_float(b, val);
106 break;
107 case SPA_TYPE_Double:
108 spa_pod_builder_double(b, val);
109 break;
110 default:
111 spa_pod_builder_none(b);
112 break;
113 }
114 }
115 else if (spa_json_is_bool(value, len)) {
116 bool val = false;
117 spa_json_parse_bool(value, len, &val);
118 spa_pod_builder_bool(b, val);
119 }
120 else if (spa_json_is_null(value, len)) {
121 spa_pod_builder_none(b);
122 }
123 else {
124 char *val = (char*)alloca(len+1);
125 spa_json_parse_stringn(value, len, val, len+1);
126 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
127 case SPA_TYPE_Id:
128 if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
129 type = ti->type;
130 else if (!spa_atou32(val, &type, 0))
131 return -EINVAL;
132 spa_pod_builder_id(b, type);
133 break;
134 case SPA_TYPE_Struct:
135 case SPA_TYPE_String:
136 spa_pod_builder_string(b, val);
137 break;
138 default:
139 spa_pod_builder_none(b);
140 break;
141 }
142 }
143 return 0;
144}
145
146 SPA_API_JSON_POD int spa_json_to_pod_checked(struct spa_pod_builder *b, uint32_t flags,
147 const struct spa_type_info *info, const char *value, int len,
148 struct spa_error_location *loc)
149{
150 struct spa_json iter;
151 const char *val;
152 int res;
153
154 if (loc)
155 spa_zero(*loc);
156
157 if ((res = spa_json_begin(&iter, value, len, &val)) <= 0)
158 goto error;
159
160 res = spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
161
162error:
163 if (res < 0 && loc)
164 spa_json_get_error(&iter, value, loc);
165 return res;
166}
167
168 SPA_API_JSON_POD int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
169 const struct spa_type_info *info, const char *value, int len)
170{
171 return spa_json_to_pod_checked(b, flags, info, value, len, NULL);
172}
173
174
175 * \}
176 */
177
178#ifdef __cplusplus
179} /* extern "C" */
180#endif
181
182#endif /* SPA_UTILS_JSON_POD_H */
builder.h
spa/pod/builder.h
res
uint32_t int int res
Definition core.h:433
spa_debug_type_find_short
SPA_API_DEBUG_TYPES const struct spa_type_info * spa_debug_type_find_short(const struct spa_type_info *info, const char *name)
Definition types.h:92
spa_debug_type_find
SPA_API_DEBUG_TYPES const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition types.h:37
spa_json_to_pod_checked
SPA_API_JSON_POD 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:153
spa_json_to_pod
SPA_API_JSON_POD 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:175
spa_json_to_pod_part
SPA_API_JSON_POD 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:42
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
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_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_is_object
SPA_API_JSON int spa_json_is_object(const char *val, int len)
Definition json-core.h:416
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_error
SPA_API_JSON 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:364
spa_json_is_null
SPA_API_JSON bool spa_json_is_null(const char *val, int len)
Definition json-core.h:428
spa_json_is_bool
SPA_API_JSON bool spa_json_is_bool(const char *val, int len)
Definition json-core.h:506
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_is_int
SPA_API_JSON bool spa_json_is_int(const char *val, int len)
Definition json-core.h:489
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_is_array
SPA_API_JSON bool spa_json_is_array(const char *val, int len)
Definition json-core.h:422
spa_json_is_float
SPA_API_JSON bool spa_json_is_float(const char *val, int len)
Definition json-core.h:457
spa_pod_builder_pop
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:213
spa_pod_builder_int
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:296
spa_pod_builder_long
SPA_API_POD_BUILDER int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:305
spa_pod_builder_none
SPA_API_POD_BUILDER int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition builder.h:262
spa_pod_builder_string
SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:351
spa_pod_builder_id
SPA_API_POD_BUILDER int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition builder.h:287
spa_pod_builder_double
SPA_API_POD_BUILDER int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition builder.h:323
spa_pod_builder_push_struct
SPA_API_POD_BUILDER int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:462
spa_pod_builder_bool
SPA_API_POD_BUILDER int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition builder.h:278
spa_pod_builder_push_object
SPA_API_POD_BUILDER 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:475
spa_pod_builder_float
SPA_API_POD_BUILDER int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition builder.h:314
spa_pod_builder_push_array
SPA_API_POD_BUILDER int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:415
spa_pod_builder_prop
SPA_API_POD_BUILDER int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:490
spa_atou32
SPA_API_STRING 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:139
SPA_TYPE_Int
@ SPA_TYPE_Int
Definition type.h:45
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:46
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition type.h:43
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition type.h:47
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition type.h:48
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:44
SPA_TYPE_String
@ SPA_TYPE_String
Definition type.h:49
SPA_TYPE_Struct
@ SPA_TYPE_Struct
Definition type.h:55
spa_zero
#define spa_zero(x)
Definition defs.h:512
SPA_API_JSON_POD
#define SPA_API_JSON_POD
Definition json-pod.h:29
json.h
spa/utils/json.h
pod.h
spa/pod/pod.h
string.h
spa/utils/string.h
spa_error_location
Definition defs.h:443
spa_json
Definition json-core.h:49
spa_pod_builder
Definition builder.h:63
spa_pod_frame
Definition body.h:38
spa_pod_frame::parent
struct spa_pod_frame * parent
Definition body.h:40
spa_type_info
Definition type.h:156
spa_type_info::type
uint32_t type
Definition type.h:157
spa_type_info::values
const struct spa_type_info * values
Definition type.h:160
types.h
spa/debug/types.h