5#ifndef SPA_UTILS_JSON_BUILDER_H
6#define SPA_UTILS_JSON_BUILDER_H
27#ifndef SPA_API_JSON_BUILDER
29 #define SPA_API_JSON_BUILDER SPA_API_IMPL
31 #define SPA_API_JSON_BUILDER static inline
46#define SPA_JSON_BUILDER_FLAG_CLOSE (1<<0)
47#define SPA_JSON_BUILDER_FLAG_INDENT (1<<1)
48#define SPA_JSON_BUILDER_FLAG_SPACE (1<<2)
49#define SPA_JSON_BUILDER_FLAG_PRETTY (SPA_JSON_BUILDER_FLAG_INDENT|SPA_JSON_BUILDER_FLAG_SPACE)
50#define SPA_JSON_BUILDER_FLAG_COLOR (1<<3)
51#define SPA_JSON_BUILDER_FLAG_SIMPLE (1<<4)
52#define SPA_JSON_BUILDER_FLAG_RAW (1<<5)
61#define SPA_JSON_BUILDER_COLOR_NORMAL 0
62#define SPA_JSON_BUILDER_COLOR_KEY 1
63#define SPA_JSON_BUILDER_COLOR_LITERAL 2
64#define SPA_JSON_BUILDER_COLOR_NUMBER 3
65#define SPA_JSON_BUILDER_COLOR_STRING 4
66#define SPA_JSON_BUILDER_COLOR_CONTAINER 5
80 b->
comma = simple ? space ?
"" :
" " :
",";
81 b->
key_sep = simple ? space ?
" =" :
"=" :
":";
92 char **mem,
size_t *size, uint32_t flags)
96 if ((f = open_memstream(mem, size)) == NULL)
102 char *mem,
size_t size, uint32_t flags)
106 if ((f = fmemopen(mem, size,
"w")) == NULL)
118 bool raw,
const char *before,
const char *
val,
int size,
const char *after)
123 len = fprintf(f,
"%s%.*s%s", before, size,
val, after) - 1;
125 len = fprintf(f,
"%s\"", before);
126 for (i = 0; i < size &&
val[i]; i++) {
129 case '\n': len += fprintf(f,
"\\n");
break;
130 case '\r': len += fprintf(f,
"\\r");
break;
131 case '\b': len += fprintf(f,
"\\b");
break;
132 case '\t': len += fprintf(f,
"\\t");
break;
133 case '\f': len += fprintf(f,
"\\f");
break;
135 case '"': len += fprintf(f,
"\\%c", v);
break;
137 if (v > 0 && v < 0x20)
138 len += fprintf(f,
"\\u%04x", v);
140 len += fprintf(f,
"%c", v);
144 len += fprintf(f,
"\"%s", after);
151 char type,
const char *
val,
int val_len)
159 if (
val == NULL || val_len == 0) {
166 type = simple ?
'C' :
'S';
167 else if (val_len > 0 && (*
val ==
'}' || *
val ==
']'))
189 fprintf(b->
f,
"%s%s%*s", b->
delim, b->
count == 0 ?
"" : indent ?
"\n" : space ?
" " :
"",
190 indent ? b->
level : 0,
"");
196 fprintf(b->
f,
"%s%s", b->
key_sep, space ?
" " :
"");
234 const char *
key,
const char *
val)
249 const char *
key,
bool val)
254 const char *
key, int64_t
val)
257 snprintf(str,
sizeof(str),
"%" PRIi64,
val);
261 const char *
key, uint64_t
val)
264 snprintf(str,
sizeof(str),
"%" PRIu64,
val);
275 const char *
key,
const char *
val)
281 const
char *
key, const
char *
fmt, va_list
va)
292 const
char *
key, const
char *
fmt, ...)
328 bool recurse,
const char *
key,
int key_len,
const char *
val,
int val_len)
397 const
char *
fmt, ...)
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:263
SPA_API_JSON_BUILDER int spa_json_builder_file(struct spa_json_builder *b, FILE *f, uint32_t flags)
Definition json-builder.h:90
SPA_API_JSON_BUILDER void spa_json_builder_object_bool(struct spa_json_builder *b, const char *key, bool val)
Definition json-builder.h:268
SPA_API_JSON_BUILDER void spa_json_builder_array_null(struct spa_json_builder *b)
Definition json-builder.h:386
#define SPA_JSON_BUILDER_FLAG_CLOSE
Definition json-builder.h:54
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:359
#define SPA_JSON_BUILDER_COLOR_LITERAL
Definition json-builder.h:80
const char const char * fmt
Definition json-builder.h:301
SPA_API_JSON_BUILDER void spa_json_builder_array_int(struct spa_json_builder *b, int64_t val)
Definition json-builder.h:395
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:137
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:111
SPA_API_JSON_BUILDER void spa_json_builder_array_push(struct spa_json_builder *b, const char *val)
Definition json-builder.h:381
bool const char const char char * val
Definition json-builder.h:370
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:253
SPA_API_JSON_BUILDER void spa_json_builder_array_value(struct spa_json_builder *b, bool recurse, const char *val)
Definition json-builder.h:424
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:347
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:121
#define SPA_JSON_BUILDER_FLAG_COLOR
Definition json-builder.h:62
#define SPA_JSON_BUILDER_COLOR_CONTAINER
Definition json-builder.h:86
#define SPA_JSON_BUILDER_FLAG_SPACE
Definition json-builder.h:58
SPA_API_JSON_BUILDER char * spa_json_builder_reformat(const char *json, uint32_t flags)
Definition json-builder.h:442
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:280
#define SPA_JSON_BUILDER_FLAG_INDENT
Definition json-builder.h:56
SPA_API_JSON_BUILDER void spa_json_builder_array_string(struct spa_json_builder *b, const char *val)
Definition json-builder.h:410
SPA_API_JSON_BUILDER void spa_json_builder_array_double(struct spa_json_builder *b, double val)
Definition json-builder.h:405
SPA_API_JSON_BUILDER void spa_json_builder_close(struct spa_json_builder *b)
Definition json-builder.h:131
#define SPA_JSON_BUILDER_COLOR_STRING
Definition json-builder.h:84
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:170
const char const char va_start(va, fmt)
#define SPA_JSON_BUILDER_COLOR_NUMBER
Definition json-builder.h:82
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:320
#define SPA_JSON_BUILDER_FLAG_RAW
Definition json-builder.h:66
#define SPA_JSON_BUILDER_COLOR_NORMAL
Definition json-builder.h:76
SPA_API_JSON_BUILDER void spa_json_builder_array_uint(struct spa_json_builder *b, uint64_t val)
Definition json-builder.h:400
spa_json_builder_object_stringv(b, key, fmt, va)
const char * key
Definition json-builder.h:301
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:273
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:294
bool recurse
Definition json-builder.h:367
SPA_API_JSON_BUILDER void spa_json_builder_array_bool(struct spa_json_builder *b, bool val)
Definition json-builder.h:390
#define SPA_JSON_BUILDER_FLAG_SIMPLE
Definition json-builder.h:64
const char const char va_list va
Definition json-builder.h:302
SPA_API_JSON_BUILDER void spa_json_builder_object_double(struct spa_json_builder *b, const char *key, double val)
Definition json-builder.h:287
SPA_API_JSON_BUILDER void spa_json_builder_pop(struct spa_json_builder *b, const char *val)
Definition json-builder.h:258
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:512
#define SPA_PRINTF_FUNC(fmt, arg1)
Definition defs.h:297
#define SPA_API_JSON_BUILDER
Definition json-builder.h:38
Definition json-builder.h:51
uint32_t indent
Definition json-builder.h:70
const char * comma
Definition json-builder.h:73
FILE * f
Definition json-builder.h:52
uint32_t count
Definition json-builder.h:71
const char * color[8]
Definition json-builder.h:87
const char * key_sep
Definition json-builder.h:74
const char * delim
Definition json-builder.h:72
uint32_t flags
Definition json-builder.h:67
uint32_t indent_off
Definition json-builder.h:68
uint32_t level
Definition json-builder.h:69
Definition json-core.h:49