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

PipeWire 1.0.3

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 spa_pod_builder_pop(b, &f[0]);
58 }
59 else if (spa_json_is_array(value, len)) {
60 if (info == NULL || info->parent == SPA_TYPE_Struct) {
61 spa_pod_builder_push_struct(b, &f[0]);
62 } else {
63 spa_pod_builder_push_array(b, &f[0]);
64 info = info->values;
65 }
66 spa_json_enter(iter, &it[0]);
67 while ((l = spa_json_next(&it[0], &v)) > 0)
68 if ((res = spa_json_to_pod_part(b, flags, id, info, &it[0], v, l)) < 0)
69 return res;
70 spa_pod_builder_pop(b, &f[0]);
71 }
72 else if (spa_json_is_float(value, len)) {
73 float val = 0.0f;
74 spa_json_parse_float(value, len, &val);
75 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
76 case SPA_TYPE_Bool:
77 spa_pod_builder_bool(b, val >= 0.5f);
78 break;
79 case SPA_TYPE_Id:
80 spa_pod_builder_id(b, val);
81 break;
82 case SPA_TYPE_Int:
83 spa_pod_builder_int(b, val);
84 break;
85 case SPA_TYPE_Long:
86 spa_pod_builder_long(b, val);
87 break;
88 case SPA_TYPE_Struct:
89 if (spa_json_is_int(value, len))
90 spa_pod_builder_int(b, val);
91 else
92 spa_pod_builder_float(b, val);
93 break;
94 case SPA_TYPE_Float:
95 spa_pod_builder_float(b, val);
96 break;
97 case SPA_TYPE_Double:
98 spa_pod_builder_double(b, val);
99 break;
100 default:
101 spa_pod_builder_none(b);
102 break;
103 }
104 }
105 else if (spa_json_is_bool(value, len)) {
106 bool val = false;
107 spa_json_parse_bool(value, len, &val);
108 spa_pod_builder_bool(b, val);
109 }
110 else if (spa_json_is_null(value, len)) {
111 spa_pod_builder_none(b);
112 }
113 else {
114 char *val = (char*)alloca(len+1);
115 spa_json_parse_stringn(value, len, val, len+1);
116 switch (info ? info->parent : (uint32_t)SPA_TYPE_Struct) {
117 case SPA_TYPE_Id:
118 if ((ti = spa_debug_type_find_short(info->values, val)) != NULL)
119 type = ti->type;
120 else if (!spa_atou32(val, &type, 0))
121 return -EINVAL;
122 spa_pod_builder_id(b, type);
123 break;
124 case SPA_TYPE_Struct:
125 case SPA_TYPE_String:
126 spa_pod_builder_string(b, val);
127 break;
128 default:
129 spa_pod_builder_none(b);
130 break;
131 }
132 }
133 return 0;
134}
135
136static inline int spa_json_to_pod(struct spa_pod_builder *b, uint32_t flags,
137 const struct spa_type_info *info, const char *value, int len)
138{
139 struct spa_json iter;
140 const char *val;
141
142 spa_json_init(&iter, value, len);
143 if ((len = spa_json_next(&iter, &val)) <= 0)
144 return -EINVAL;
145
146 return spa_json_to_pod_part(b, flags, info->type, info, &iter, val, len);
147}
148
153#ifdef __cplusplus
154} /* extern "C" */
155#endif
156
157#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:141
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:237
spa_json_parse_float
static int spa_json_parse_float(const char *val, int len, float *result)
Definition json.h:228
spa_json_parse_stringn
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition json.h:343
spa_json_enter
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json.h:56
spa_json_parse_bool
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition json.h:301
spa_json_get_string
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition json.h:411
spa_json_is_bool
static bool spa_json_is_bool(const char *val, int len)
Definition json.h:296
spa_json_is_array
static bool spa_json_is_array(const char *val, int len)
Definition json.h:212
spa_json_is_null
static bool spa_json_is_null(const char *val, int len)
Definition json.h:222
spa_json_next
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json.h:66
spa_json_init
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition json.h:49
spa_json_is_int
static bool spa_json_is_int(const char *val, int len)
Definition json.h:271
spa_json_is_object
static int spa_json_is_object(const char *val, int len)
Definition json.h:202
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
types.h
spa/debug/types.h