PipeWire 1.2.6
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 (spa_json_get_string(&it[0], key, sizeof(key)) > 0) {
46 const struct spa_type_info *pi;
47 if ((l = spa_json_next(&it[0], &v)) <= 0)
48 break;
49 if ((pi = spa_debug_type_find_short(ti->values, key)) != NULL)
50 type = pi->type;
51 else if (!spa_atou32(key, &type, 0))
52 continue;
54 if ((res = spa_json_to_pod_part(b, flags, id, pi, &it[0], v, l)) < 0)
55 return res;
56 }
57 if (l < 0)
58 return l;
59 spa_pod_builder_pop(b, &f[0]);
60 }
61 else if (spa_json_is_array(value, len)) {
62 if (info == NULL || info->parent == SPA_TYPE_Struct) {
64 } else {
66 info = info->values;
67 }
68 spa_json_enter(iter, &it[0]);
69 while ((l = spa_json_next(&it[0], &v)) > 0)
70 if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
71 return res;
72 if (l < 0)
73 return l;
74 spa_pod_builder_pop(b, &f[0]);
75 }
76 else if (spa_json_is_float(value, len)) {
77 float val = 0.0f;
78 spa_json_parse_float(value, len, &val);
79 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
80 case SPA_TYPE_Bool:
81 spa_pod_builder_bool(b, val >= 0.5f);
82 break;
83 case SPA_TYPE_Id:
85 break;
86 case SPA_TYPE_Int:
88 break;
89 case SPA_TYPE_Long:
91 break;
92 case SPA_TYPE_Struct:
93 if (spa_json_is_int(value, len))
95 else
97 break;
98 case SPA_TYPE_Float:
100 break;
101 case SPA_TYPE_Double:
103 break;
104 default:
106 break;
107 }
108 }
109 else if (spa_json_is_bool(value, len)) {
110 bool val = false;
111 spa_json_parse_bool(value, len, &val);
113 }
114 else if (spa_json_is_null(value, len)) {
116 }
117 else {
118 char *val = (char*)alloca(len+1);
119 spa_json_parse_stringn(value, len, val, len+1);
120 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
121 case SPA_TYPE_Id:
122 if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
123 type = ti->type;
124 else if (!spa_atou32(val, &type, 0))
125 return -EINVAL;
127 break;
128 case SPA_TYPE_Struct:
129 case SPA_TYPE_String:
131 break;
132 default:
134 break;
135 }
136 }
137 return 0;
138}
139
140static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
141 const struct spa_type_info *info, const char *value, int len)
142{
143 struct spa_json iter;
144 const char *val;
146 spa_json_init(&iter, value, len);
147 if ((len = spa_json_next(&iter, &val)) <= 0)
148 return -EINVAL;
149
150 return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
151}
152
157#ifdef __cplusplus
158} /* extern "C" */
159#endif
160
161#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:84
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:145
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.h:452
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json.h:429
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json.h:564
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json.h:58
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json.h:522
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:632
static bool spa_json_is_bool(const char *val, int len)
Definition json.h:517
static bool spa_json_is_array(const char *val, int len)
Definition json.h:413
static bool spa_json_is_null(const char *val, int len)
Definition json.h:423
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json.h:68
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json.h:51
static bool spa_json_is_int(const char *val, int len)
Definition json.h:492
static int spa_json_is_object(const char *val, int len)
Definition json.h:403
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:34
@ SPA_TYPE_Long
Definition type.h:35
@ SPA_TYPE_Bool
Definition type.h:32
@ SPA_TYPE_Float
Definition type.h:36
@ SPA_TYPE_Double
Definition type.h:37
@ SPA_TYPE_Id
Definition type.h:33
@ SPA_TYPE_String
Definition type.h:38
@ SPA_TYPE_Struct
Definition type.h:44
spa/utils/json.h
spa/pod/pod.h
spa/utils/string.h
Definition json.h:38
Definition builder.h:53
Definition iter.h:27
Definition type.h:143
uint32_t type
Definition type.h:144
spa/debug/types.h