PipeWire 1.4.1
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
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
35SPA_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;
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) {
70 } else {
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:
109 break;
110 default:
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)) {
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;
133 break;
134 case SPA_TYPE_Struct:
135 case SPA_TYPE_String:
137 break;
138 default:
140 break;
141 }
142 }
143 return 0;
144}
145
146SPA_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;
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
168SPA_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
178#ifdef __cplusplus
179} /* extern "C" */
180#endif
181
182#endif /* SPA_UTILS_JSON_POD_H */
spa/pod/builder.h
uint32_t int int res
Definition core.h:433
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_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_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_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_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_API_JSON_UTILS int spa_json_object_next(struct spa_json *iter, char *key, int maxkeylen, const char **value)
Definition json.h:150
SPA_API_JSON_UTILS int spa_json_begin(struct spa_json *iter, const char *data, size_t size, const char **val)
Definition json.h:46
SPA_API_JSON int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json-core.h:91
SPA_API_JSON int spa_json_is_object(const char *val, int len)
Definition json-core.h:397
SPA_API_JSON int spa_json_parse_float(const char *val, int len, float *result)
Definition json-core.h:415
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:345
SPA_API_JSON bool spa_json_is_null(const char *val, int len)
Definition json-core.h:409
SPA_API_JSON bool spa_json_is_bool(const char *val, int len)
Definition json-core.h:487
SPA_API_JSON int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json-core.h:492
SPA_API_JSON bool spa_json_is_int(const char *val, int len)
Definition json-core.h:470
SPA_API_JSON int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json-core.h:526
SPA_API_JSON void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json-core.h:68
SPA_API_JSON bool spa_json_is_array(const char *val, int len)
Definition json-core.h:403
SPA_API_JSON bool spa_json_is_float(const char *val, int len)
Definition json-core.h:438
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:187
SPA_API_POD_BUILDER int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:266
SPA_API_POD_BUILDER int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:275
SPA_API_POD_BUILDER int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition builder.h:232
SPA_API_POD_BUILDER int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:324
SPA_API_POD_BUILDER int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition builder.h:257
SPA_API_POD_BUILDER int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition builder.h:293
SPA_API_POD_BUILDER int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:441
SPA_API_POD_BUILDER int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition builder.h:248
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:454
SPA_API_POD_BUILDER int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition builder.h:284
SPA_API_POD_BUILDER int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:391
SPA_API_POD_BUILDER int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:469
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:138
@ SPA_TYPE_Int
Definition type.h:45
@ SPA_TYPE_Long
Definition type.h:46
@ SPA_TYPE_Bool
Definition type.h:43
@ SPA_TYPE_Float
Definition type.h:47
@ SPA_TYPE_Double
Definition type.h:48
@ SPA_TYPE_Id
Definition type.h:44
@ SPA_TYPE_String
Definition type.h:49
@ SPA_TYPE_Struct
Definition type.h:55
#define spa_zero(x)
Definition defs.h:508
#define SPA_API_JSON_POD
Definition json-pod.h:29
spa/utils/json.h
spa/pod/pod.h
spa/utils/string.h
Definition defs.h:439
Definition json-core.h:48
Definition builder.h:63
Definition iter.h:37
Definition type.h:154
uint32_t type
Definition type.h:155
const struct spa_type_info * values
Definition type.h:158
spa/debug/types.h