PipeWire  0.3.67
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
9 extern "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 
27 static 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  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) {
62  } else {
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:
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)) {
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;
123  break;
124  case SPA_TYPE_Struct:
125  case SPA_TYPE_String:
126  spa_pod_builder_string(b, val);
127  break;
128  default:
130  break;
131  }
132  }
133  return 0;
134 }
135 
136 static 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 */
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:141
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:237
static int spa_json_parse_float(const char *val, int len, float *result)
Definition: json.h:228
static int spa_json_parse_stringn(const char *val, int len, char *result, int maxlen)
Definition: json.h:343
static void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition: json.h:56
static int spa_json_parse_bool(const char *val, int len, bool *result)
Definition: json.h:301
static int spa_json_get_string(struct spa_json *iter, char *res, int maxlen)
Definition: json.h:411
static bool spa_json_is_bool(const char *val, int len)
Definition: json.h:296
static bool spa_json_is_array(const char *val, int len)
Definition: json.h:212
static bool spa_json_is_null(const char *val, int len)
Definition: json.h:222
static int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition: json.h:66
static void spa_json_init(struct spa_json *iter, const char *data, size_t size)
Definition: json.h:49
static bool spa_json_is_int(const char *val, int len)
Definition: json.h:271
static int spa_json_is_object(const char *val, int len)
Definition: json.h:202
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 void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition: builder.h:168
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 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: spa/include/spa/utils/type.h:34
@ SPA_TYPE_Long
Definition: spa/include/spa/utils/type.h:35
@ SPA_TYPE_Bool
Definition: spa/include/spa/utils/type.h:32
@ SPA_TYPE_Float
Definition: spa/include/spa/utils/type.h:36
@ SPA_TYPE_Double
Definition: spa/include/spa/utils/type.h:37
@ SPA_TYPE_Id
Definition: spa/include/spa/utils/type.h:33
@ SPA_TYPE_String
Definition: spa/include/spa/utils/type.h:38
@ SPA_TYPE_Struct
Definition: spa/include/spa/utils/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: spa/include/spa/utils/type.h:142
uint32_t type
Definition: spa/include/spa/utils/type.h:143
uint32_t parent
Definition: spa/include/spa/utils/type.h:144
const struct spa_type_info * values
Definition: spa/include/spa/utils/type.h:146
spa/debug/types.h