ujson
Complete and simple JSON reader and writer written in C
ujson_writer.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright (C) 2021-2024 Cyril Hrubis <metan@ucw.cz>
4  */
5 
24 #ifndef UJSON_WRITER_H
25 #define UJSON_WRITER_H
26 
27 #include <ujson_common.h>
28 
30 struct ujson_writer {
31  unsigned int depth;
32  char depth_type[UJSON_RECURSION_MAX/8];
33  char depth_first[UJSON_RECURSION_MAX/8];
34 
36  void (*err_print)(void *err_print_priv, const char *line);
37  void *err_print_priv;
38  char err[UJSON_ERR_MAX];
39 
41  int (*out)(struct ujson_writer *self, const char *buf, size_t buf_size);
42  void *out_priv;
43 };
44 
53 #define UJSON_WRITER_INIT(vout, vout_priv) { \
54  .err_print = UJSON_ERR_PRINT, \
55  .err_print_priv = UJSON_ERR_PRINT_PRIV, \
56  .out = vout, \
57  .out_priv = vout_priv \
58 }
59 
72 
81 
89 static inline int ujson_writer_err(ujson_writer *self)
90 {
91  return !!self->err[0];
92 }
93 
106 int ujson_obj_start(ujson_writer *self, const char *id);
107 
118 
131 int ujson_arr_start(ujson_writer *self, const char *id);
132 
143 
155 int ujson_null_add(ujson_writer *self, const char *id);
156 
169 int ujson_int_add(ujson_writer *self, const char *id, long val);
170 
183 int ujson_bool_add(ujson_writer *self, const char *id, int val);
184 
197 int ujson_float_add(ujson_writer *self, const char *id, double val);
198 
211 int ujson_str_add(ujson_writer *self, const char *id, const char *str);
212 
223 
224 #endif /* UJSON_WRITER_H */
A JSON writer.
Definition: ujson_writer.h:30
int(* out)(struct ujson_writer *self, const char *buf, size_t buf_size)
Definition: ujson_writer.h:41
void(* err_print)(void *err_print_priv, const char *line)
Definition: ujson_writer.h:36
Common JSON reader/writer definitions.
#define UJSON_RECURSION_MAX
Maximal recursion depth allowed.
Definition: ujson_common.h:19
#define UJSON_ERR_MAX
Maximal error message length.
Definition: ujson_common.h:15
int ujson_arr_finish(ujson_writer *self)
Finishes a JSON array.
int ujson_int_add(ujson_writer *self, const char *id, long val)
Adds an integer value.
int ujson_str_add(ujson_writer *self, const char *id, const char *str)
Adds a string value.
static int ujson_writer_err(ujson_writer *self)
Returns true if writer error happened.
Definition: ujson_writer.h:89
ujson_writer * ujson_writer_file_open(const char *path)
Allocates a JSON file writer.
int ujson_null_add(ujson_writer *self, const char *id)
Adds a null value.
int ujson_arr_start(ujson_writer *self, const char *id)
Starts a JSON array.
int ujson_writer_finish(ujson_writer *self)
Finalizes json writer.
int ujson_bool_add(ujson_writer *self, const char *id, int val)
Adds a bool value.
int ujson_writer_file_close(ujson_writer *self)
Closes and frees a JSON file writer.
int ujson_obj_finish(ujson_writer *self)
Finishes a JSON object.
int ujson_float_add(ujson_writer *self, const char *id, double val)
Adds a float value.
int ujson_obj_start(ujson_writer *self, const char *id)
Starts a JSON object.