5#ifndef SPA_UTILS_JSON_BUILDER_H
6#define SPA_UTILS_JSON_BUILDER_H
20#include <spa/utils/cleanup.h>
28#ifndef SPA_API_JSON_BUILDER
30 #define SPA_API_JSON_BUILDER SPA_API_IMPL
32 #define SPA_API_JSON_BUILDER static inline
47#define SPA_JSON_BUILDER_FLAG_CLOSE (1<<0)
48#define SPA_JSON_BUILDER_FLAG_INDENT (1<<1)
49#define SPA_JSON_BUILDER_FLAG_SPACE (1<<2)
50#define SPA_JSON_BUILDER_FLAG_PRETTY (SPA_JSON_BUILDER_FLAG_INDENT|SPA_JSON_BUILDER_FLAG_SPACE)
51#define SPA_JSON_BUILDER_FLAG_COLOR (1<<3)
52#define SPA_JSON_BUILDER_FLAG_SIMPLE (1<<4)
53#define SPA_JSON_BUILDER_FLAG_RAW (1<<5)
62#define SPA_JSON_BUILDER_COLOR_NORMAL 0
63#define SPA_JSON_BUILDER_COLOR_KEY 1
64#define SPA_JSON_BUILDER_COLOR_LITERAL 2
65#define SPA_JSON_BUILDER_COLOR_NUMBER 3
66#define SPA_JSON_BUILDER_COLOR_STRING 4
67#define SPA_JSON_BUILDER_COLOR_CONTAINER 5
81 b->
comma = simple ? space ?
"" :
" " :
",";
82 b->
key_sep = simple ? space ?
" =" :
"=" :
":";
93 char **mem,
size_t *size, uint32_t flags)
97 if ((f = open_memstream(mem, size)) == NULL)
103 char *mem,
size_t size, uint32_t flags)
107 if ((f = fmemopen(mem, size,
"w")) == NULL)
118 if (fclose(b->
f) != 0 &&
res == 0)
125 bool raw,
const char *before,
const char *
val,
int size,
const char *after)
130 len = fprintf(f,
"%s%.*s%s", before, size,
val, after) - 1;
132 len = fprintf(f,
"%s\"", before);
133 for (i = 0; i < size &&
val[i]; i++) {
136 case '\n': len += fprintf(f,
"\\n");
break;
137 case '\r': len += fprintf(f,
"\\r");
break;
138 case '\b': len += fprintf(f,
"\\b");
break;
139 case '\t': len += fprintf(f,
"\\t");
break;
140 case '\f': len += fprintf(f,
"\\f");
break;
142 case '"': len += fprintf(f,
"\\%c", v);
break;
144 if (v > 0 && v < 0x20)
145 len += fprintf(f,
"\\u%04x", v);
147 len += fprintf(f,
"%c", v);
151 len += fprintf(f,
"\"%s", after);
158 char type,
const char *
val,
int val_len)
166 if (
val == NULL || val_len == 0) {
173 type = simple ?
'C' :
'S';
174 else if (val_len > 0 && (*
val ==
'}' || *
val ==
']'))
196 fprintf(b->
f,
"%s%s%*s", b->
delim, b->
count == 0 ?
"" : indent ?
"\n" : space ?
" " :
"",
197 indent ? b->
level : 0,
"");
203 fprintf(b->
f,
"%s%s", b->
key_sep, space ?
" " :
"");
241 const char *
key,
const char *
val)
256 const char *
key,
bool val)
261 const char *
key, int64_t
val)
264 snprintf(str,
sizeof(str),
"%" PRIi64,
val);
268 const char *
key, uint64_t
val)
271 snprintf(str,
sizeof(str),
"%" PRIu64,
val);
282 const char *
key,
const char *
val)
288 const
char *
key, const
char *
fmt, va_list
va)
299 const
char *
key, const
char *
fmt, ...)
335 bool recurse,
const char *
key,
int key_len,
const char *
val,
int val_len)
404 const
char *
fmt, ...)
432 spa_autofree
char *mem = NULL;
440 return spa_steal_ptr(mem);
uint32_t int int res
Definition core.h:433
#define SPA_ANSI_BRIGHT_BLUE
Definition ansi.h:66
#define SPA_ANSI_BRIGHT_GREEN
Definition ansi.h:62
#define SPA_ANSI_BRIGHT_MAGENTA
Definition ansi.h:68
#define SPA_ANSI_RESET
Ansi escape sequences.
Definition ansi.h:33
#define SPA_ANSI_BRIGHT_CYAN
Definition ansi.h:70
#define SPA_ANSI_BRIGHT_YELLOW
Definition ansi.h:64
SPA_API_JSON_BUILDER void spa_json_builder_object_null(struct spa_json_builder *b, const char *key)
Definition json-builder.h:270
SPA_API_JSON_BUILDER int spa_json_builder_file(struct spa_json_builder *b, FILE *f, uint32_t flags)
Definition json-builder.h:91
SPA_API_JSON_BUILDER void spa_json_builder_object_bool(struct spa_json_builder *b, const char *key, bool val)
Definition json-builder.h:275
SPA_API_JSON_BUILDER void spa_json_builder_array_null(struct spa_json_builder *b)
Definition json-builder.h:393
#define SPA_JSON_BUILDER_FLAG_CLOSE
Definition json-builder.h:55
SPA_API_JSON_BUILDER void spa_json_builder_object_value(struct spa_json_builder *b, bool recurse, const char *key, const char *val)
Definition json-builder.h:366
#define SPA_JSON_BUILDER_COLOR_LITERAL
Definition json-builder.h:81
const char const char * fmt
Definition json-builder.h:308
SPA_API_JSON_BUILDER void spa_json_builder_array_int(struct spa_json_builder *b, int64_t val)
Definition json-builder.h:402
SPA_API_JSON_BUILDER int spa_json_builder_encode_string(struct spa_json_builder *b, bool raw, const char *before, const char *val, int size, const char *after)
Definition json-builder.h:144
SPA_API_JSON_BUILDER int spa_json_builder_memstream(struct spa_json_builder *b, char **mem, size_t *size, uint32_t flags)
Definition json-builder.h:112
SPA_API_JSON_BUILDER void spa_json_builder_array_push(struct spa_json_builder *b, const char *val)
Definition json-builder.h:388
bool const char const char char * val
Definition json-builder.h:377
SPA_API_JSON_BUILDER void spa_json_builder_object_push(struct spa_json_builder *b, const char *key, const char *val)
Definition json-builder.h:260
SPA_API_JSON_BUILDER void spa_json_builder_array_value(struct spa_json_builder *b, bool recurse, const char *val)
Definition json-builder.h:431
SPA_API_JSON_BUILDER void spa_json_builder_object_value_full(struct spa_json_builder *b, bool recurse, const char *key, int key_len, const char *val, int val_len)
Definition json-builder.h:354
SPA_API_JSON_BUILDER int spa_json_builder_membuf(struct spa_json_builder *b, char *mem, size_t size, uint32_t flags)
Definition json-builder.h:122
#define SPA_JSON_BUILDER_FLAG_COLOR
Definition json-builder.h:63
#define SPA_JSON_BUILDER_COLOR_CONTAINER
Definition json-builder.h:87
#define SPA_JSON_BUILDER_FLAG_SPACE
Definition json-builder.h:59
SPA_API_JSON_BUILDER char * spa_json_builder_reformat(const char *json, uint32_t flags)
Definition json-builder.h:449
SPA_API_JSON_BUILDER void spa_json_builder_object_uint(struct spa_json_builder *b, const char *key, uint64_t val)
Definition json-builder.h:287
#define SPA_JSON_BUILDER_FLAG_INDENT
Definition json-builder.h:57
SPA_API_JSON_BUILDER void spa_json_builder_array_string(struct spa_json_builder *b, const char *val)
Definition json-builder.h:417
SPA_API_JSON_BUILDER void spa_json_builder_array_double(struct spa_json_builder *b, double val)
Definition json-builder.h:412
#define SPA_JSON_BUILDER_COLOR_STRING
Definition json-builder.h:85
SPA_API_JSON_BUILDER void spa_json_builder_add_simple(struct spa_json_builder *b, const char *key, int key_len, char type, const char *val, int val_len)
Definition json-builder.h:177
const char const char va_start(va, fmt)
#define SPA_JSON_BUILDER_COLOR_NUMBER
Definition json-builder.h:83
SPA_API_JSON_BUILDER void spa_json_builder_object_value_iter(struct spa_json_builder *b, struct spa_json *it, const char *key, int key_len, const char *val, int len)
Definition json-builder.h:327
#define SPA_JSON_BUILDER_FLAG_RAW
Definition json-builder.h:67
#define SPA_JSON_BUILDER_COLOR_NORMAL
Definition json-builder.h:77
SPA_API_JSON_BUILDER void spa_json_builder_array_uint(struct spa_json_builder *b, uint64_t val)
Definition json-builder.h:407
spa_json_builder_object_stringv(b, key, fmt, va)
const char * key
Definition json-builder.h:308
SPA_API_JSON_BUILDER void spa_json_builder_object_int(struct spa_json_builder *b, const char *key, int64_t val)
Definition json-builder.h:280
SPA_API_JSON_BUILDER void spa_json_builder_object_string(struct spa_json_builder *b, const char *key, const char *val)
Definition json-builder.h:301
bool recurse
Definition json-builder.h:374
SPA_API_JSON_BUILDER void spa_json_builder_array_bool(struct spa_json_builder *b, bool val)
Definition json-builder.h:397
#define SPA_JSON_BUILDER_FLAG_SIMPLE
Definition json-builder.h:65
const char const char va_list va
Definition json-builder.h:309
SPA_API_JSON_BUILDER int spa_json_builder_close(struct spa_json_builder *b)
Definition json-builder.h:132
SPA_API_JSON_BUILDER void spa_json_builder_object_double(struct spa_json_builder *b, const char *key, double val)
Definition json-builder.h:294
SPA_API_JSON_BUILDER void spa_json_builder_pop(struct spa_json_builder *b, const char *val)
Definition json-builder.h:265
SPA_API_JSON_UTILS int spa_json_begin(struct spa_json *iter, const char *data, size_t size, const char **val)
Definition json.h:47
SPA_API_JSON bool spa_json_is_string(const char *val, int len)
Definition json-core.h:548
SPA_API_JSON int spa_json_next(struct spa_json *iter, const char **value)
Get the next token.
Definition json-core.h:101
SPA_API_JSON int spa_json_is_object(const char *val, int len)
Definition json-core.h:420
SPA_API_JSON char * spa_json_format_float(char *str, int size, float val)
Definition json-core.h:458
SPA_API_JSON bool spa_json_is_null(const char *val, int len)
Definition json-core.h:432
SPA_API_JSON bool spa_json_is_bool(const char *val, int len)
Definition json-core.h:533
SPA_API_JSON bool spa_json_is_int(const char *val, int len)
Definition json-core.h:483
SPA_API_JSON bool spa_json_make_simple_string(const char **val, int *len)
Definition json-core.h:562
SPA_API_JSON bool spa_json_is_json_number(const char *val, int len)
Definition json-core.h:489
SPA_API_JSON void spa_json_enter(struct spa_json *iter, struct spa_json *sub)
Definition json-core.h:78
SPA_API_JSON bool spa_json_is_array(const char *val, int len)
Definition json-core.h:426
SPA_API_JSON bool spa_json_is_float(const char *val, int len)
Definition json-core.h:452
SPA_API_JSON int spa_json_is_container(const char *val, int len)
Definition json-core.h:410
#define spa_zero(x)
Definition defs.h:522
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:297
#define SPA_API_JSON_BUILDER
Definition json-builder.h:39
Definition json-builder.h:52
uint32_t indent
Definition json-builder.h:71
const char * comma
Definition json-builder.h:74
FILE * f
Definition json-builder.h:53
uint32_t count
Definition json-builder.h:72
const char * color[8]
Definition json-builder.h:88
const char * key_sep
Definition json-builder.h:75
const char * delim
Definition json-builder.h:73
uint32_t flags
Definition json-builder.h:68
uint32_t indent_off
Definition json-builder.h:69
uint32_t level
Definition json-builder.h:70
Definition json-core.h:49