PipeWire: spa/utils/json-pod.h Source File

PipeWire 1.2.5

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;
53 spa_pod_builder_prop(b, type, 0);
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) {
63 spa_pod_builder_push_struct(b, &f[0]);
64 } else {
65 spa_pod_builder_push_array(b, &f[0]);
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:
84 spa_pod_builder_id(b, (uint32_t)val);
85 break;
86 case SPA_TYPE_Int:
87 spa_pod_builder_int(b, (int32_t)val);
88 break;
89 case SPA_TYPE_Long:
90 spa_pod_builder_long(b, (int64_t)val);
91 break;
92 case SPA_TYPE_Struct:
93 if (spa_json_is_int(value, len))
94 spa_pod_builder_int(b, (int32_t)val);
95 else
96 spa_pod_builder_float(b, val);
97 break;
98 case SPA_TYPE_Float:
99 spa_pod_builder_float(b, val);
100 break;
101 case SPA_TYPE_Double:
102 spa_pod_builder_double(b, val);
103 break;
104 default:
105 spa_pod_builder_none(b);
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);
112 spa_pod_builder_bool(b, val);
113 }
114 else if (spa_json_is_null(value, len)) {
115 spa_pod_builder_none(b);
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;
126 spa_pod_builder_id(b, type);
127 break;
128 case SPA_TYPE_Struct:
129 case SPA_TYPE_String:
130 spa_pod_builder_string(b, val);
131 break;
132 default:
133 spa_pod_builder_none(b);
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;
145
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 */
builder.h
spa/pod/builder.h
spa_debug_type_find
static const struct spa_type_info * spa_debug_type_find(const struct spa_type_info *info, uint32_t type)
Definition types.h:26
spa_debug_type_find_short
static const struct spa_type_info * spa_debug_type_find_short(const struct spa_type_info *info, const char *name)
Definition types.h:84
spa_json_to_pod
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
spa_json_to_pod_part
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
spa_json_is_float
static bool spa_json_is_float(const char *val, int len)
Definition json.h:452
spa_json_parse_float
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json.h:429
spa_json_parse_stringn
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json.h:566
spa_json_enter
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json.h:58
spa_json_parse_bool
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json.h:524
spa_json_get_string
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:634
spa_json_is_bool
static bool spa_json_is_bool(const char *val, int len)
Definition json.h:519
spa_json_is_array
static bool spa_json_is_array(const char *val, int len)
Definition json.h:413
spa_json_is_null
static bool spa_json_is_null(const char *val, int len)
Definition json.h:423
spa_json_next
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json.h:68
spa_json_init
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json.h:51
spa_json_is_int
static bool spa_json_is_int(const char *val, int len)
Definition json.h:494
spa_json_is_object
static int spa_json_is_object(const char *val, int len)
Definition json.h:403
spa_pod_builder_prop
static int spa_pod_builder_prop(struct spa_pod_builder *builder, uint32_t key, uint32_t flags)
Definition builder.h:450
spa_pod_builder_string
static int spa_pod_builder_string(struct spa_pod_builder *builder, const char *str)
Definition builder.h:305
spa_pod_builder_float
static int spa_pod_builder_float(struct spa_pod_builder *builder, float val)
Definition builder.h:265
spa_pod_builder_double
static int spa_pod_builder_double(struct spa_pod_builder *builder, double val)
Definition builder.h:274
spa_pod_builder_id
static int spa_pod_builder_id(struct spa_pod_builder *builder, uint32_t val)
Definition builder.h:238
spa_pod_builder_none
static int spa_pod_builder_none(struct spa_pod_builder *builder)
Definition builder.h:213
spa_pod_builder_pop
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:168
spa_pod_builder_push_array
static int spa_pod_builder_push_array(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:372
spa_pod_builder_push_struct
static int spa_pod_builder_push_struct(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:422
spa_pod_builder_bool
static int spa_pod_builder_bool(struct spa_pod_builder *builder, bool val)
Definition builder.h:229
spa_pod_builder_int
static int spa_pod_builder_int(struct spa_pod_builder *builder, int32_t val)
Definition builder.h:247
spa_pod_builder_push_object
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
spa_pod_builder_long
static int spa_pod_builder_long(struct spa_pod_builder *builder, int64_t val)
Definition builder.h:256
spa_atou32
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
@ SPA_TYPE_Int
Definition type.h:34
SPA_TYPE_Long
@ SPA_TYPE_Long
Definition type.h:35
SPA_TYPE_Bool
@ SPA_TYPE_Bool
Definition type.h:32
SPA_TYPE_Float
@ SPA_TYPE_Float
Definition type.h:36
SPA_TYPE_Double
@ SPA_TYPE_Double
Definition type.h:37
SPA_TYPE_Id
@ SPA_TYPE_Id
Definition type.h:33
SPA_TYPE_String
@ SPA_TYPE_String
Definition type.h:38
SPA_TYPE_Struct
@ SPA_TYPE_Struct
Definition type.h:44
json.h
spa/utils/json.h
pod.h
spa/pod/pod.h
string.h
spa/utils/string.h
spa_json
Definition json.h:38
spa_pod_builder
Definition builder.h:53
spa_pod_frame
Definition iter.h:27
spa_type_info
Definition type.h:143
spa_type_info::type
uint32_t type
Definition type.h:144
spa_type_info::values
const struct spa_type_info * values
Definition type.h:147
types.h
spa/debug/types.h