PipeWire 0.3.65
json-pod.h
Go to the documentation of this file.
1/* Simple Plugin API
2 *
3 * Copyright © 2022 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef SPA_UTILS_JSON_POD_H
26#define SPA_UTILS_JSON_POD_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#include <spa/utils/string.h>
33#include <spa/utils/json.h>
34#include <spa/pod/pod.h>
35#include <spa/pod/builder.h>
36#include <spa/debug/types.h>
37
47static inline int spa_json_to_pod_part(struct spa_pod_builder *b, uint32_t flags, uint32_t id,
48 const struct spa_type_info *info, struct spa_json *iter, const char *value, int len)
49{
50 const struct spa_type_info *ti;
51 char key[256];
52 struct spa_pod_frame f[1];
53 struct spa_json it[1];
54 int l, res;
55 const char *v;
56 uint32_t type;
57
58 if (spa_json_is_object(value, len) && info != NULL) {
59 if ((ti = spa_debug_type_find(NULL, info->parent)) == NULL)
60 return -EINVAL;
61
62 spa_pod_builder_push_object(b, &f[0], info->parent, id);
63
64 spa_json_enter(iter, &it[0]);
65 while (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
66 const struct spa_type_info *pi;
67 if ((l = spa_json_next(&it[0], &v)) <= 0)
68 break;
69 if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
70 type = pi->type;
71 else if (!spa_atou32(key, &type, 0))
72 continue;
74 if ((res = spa_json_to_pod_part(b, flags, id, pi, &it[0], v, l)) < 0)
75 return res;
76 }
77 spa_pod_builder_pop(b, &f[0]);
78 }
79 else if (spa_json_is_array(value, len)) {
80 if (info == NULL || info->parent == SPA_TYPE_Struct) {
82 } else {
84 info = info->values;
85 }
86 spa_json_enter(iter, &it[0]);
87 while ((l = spa_json_next(&it[0], &v)) > 0)
88 if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
89 return res;
90 spa_pod_builder_pop(b, &f[0]);
91 }
92 else if (spa_json_is_float(value, len)) {
93 float val = 0.0f;
94 spa_json_parse_float(value, len, &val);
95 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
96 case SPA_TYPE_Bool:
97 spa_pod_builder_bool(b, val >= 0.5f);
98 break;
99 case SPA_TYPE_Id:
100 spa_pod_builder_id(b, val);
101 break;
102 case SPA_TYPE_Int:
103 spa_pod_builder_int(b, val);
104 break;
105 case SPA_TYPE_Long:
106 spa_pod_builder_long(b, val);
107 break;
108 case SPA_TYPE_Struct:
109 if (spa_json_is_int(value, len))
110 spa_pod_builder_int(b, val);
111 else
112 spa_pod_builder_float(b, val);
113 break;
114 case SPA_TYPE_Float:
115 spa_pod_builder_float(b, val);
116 break;
117 case SPA_TYPE_Double:
119 break;
120 default:
122 break;
123 }
124 }
125 else if (spa_json_is_bool(value, len)) {
126 bool val = false;
127 spa_json_parse_bool(value, len, &val);
128 spa_pod_builder_bool(b, val);
129 }
130 else if (spa_json_is_null(value, len)) {
132 }
133 else {
134 char *val = (char*)alloca(len+1);
135 spa_json_parse_stringn(value, len, val, len+1);
136 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
137 case SPA_TYPE_Id:
138 if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
139 type = ti->type;
140 else if (!spa_atou32(val, &type, 0))
141 return -EINVAL;
143 break;
144 case SPA_TYPE_Struct:
145 case SPA_TYPE_String:
147 break;
148 default:
150 break;
151 }
152 }
153 return 0;
154}
155
156static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
157 const struct spa_type_info *info, const char *value, int len)
158{
159 struct spa_json iter;
160 const char *val;
162 spa_json_init(&iter, value, len);
163 if ((len = spa_json_next(&iter, &val)) <= 0)
164 return -EINVAL;
165
166 return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
167}
168
173#ifdef __cplusplus
174} /* extern "C" */
175#endif
176
177#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:46
static const struct spa_type_info * spa_debug_type_find_short(const struct spa_type_info *info, const char *name)
Definition: types.h:104
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
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
static bool spa_json_is_float(const char *val, int len)
Definition: json.h:257
static int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:248
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition: json.h:363
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:76
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:321
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:431
static bool spa_json_is_bool(const char *val, int len)
Definition: json.h:316
static bool spa_json_is_array(const char *val, int len)
Definition: json.h:232
static bool spa_json_is_null(const char *val, int len)
Definition: json.h:242
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:86
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:69
static bool spa_json_is_int(const char *val, int len)
Definition: json.h:291
static int spa_json_is_object(const char *val, int len)
Definition: json.h:222
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition: builder.h:470
static int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition: builder.h:325
static int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition: builder.h:285
static int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition: builder.h:294
static int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition: builder.h:258
static int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition: builder.h:233
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:188
static int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:392
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:442
static int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition: builder.h:249
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition: builder.h:267
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:455
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition: builder.h:276
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:148
@ SPA_TYPE_Int
Definition: spa/include/spa/utils/type.h:54
@ SPA_TYPE_Long
Definition: spa/include/spa/utils/type.h:55
@ SPA_TYPE_Bool
Definition: spa/include/spa/utils/type.h:52
@ SPA_TYPE_Float
Definition: spa/include/spa/utils/type.h:56
@ SPA_TYPE_Double
Definition: spa/include/spa/utils/type.h:57
@ SPA_TYPE_Id
Definition: spa/include/spa/utils/type.h:53
@ SPA_TYPE_String
Definition: spa/include/spa/utils/type.h:58
@ SPA_TYPE_Struct
Definition: spa/include/spa/utils/type.h:64
spa/utils/json.h
spa/pod/pod.h
spa/utils/string.h
Definition: json.h:58
Definition: builder.h:73
Definition: iter.h:47
Definition: spa/include/spa/utils/type.h:162
uint32_t type
Definition: spa/include/spa/utils/type.h:163
uint32_t parent
Definition: spa/include/spa/utils/type.h:164
const struct spa_type_info * values
Definition: spa/include/spa/utils/type.h:166
spa/debug/types.h