+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 182 lines · 4.3 KB · SPDX: MIT
main
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  
26 /** \defgroup spa_json_pod JSON to POD
27 * JSON to POD conversion
28 */
29  
30 /**
31 * \addtogroup spa_json_pod
32 * \{
33 */
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  
162 error:
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 */