-
Notifications
You must be signed in to change notification settings - Fork 0
/
ujson_writer.h
224 lines (203 loc) · 5.76 KB
/
ujson_writer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright (C) 2021-2024 Cyril Hrubis <metan@ucw.cz>
*/
/**
* @file ujson_writer.h
* @brief A JSON writer.
*
* All the function that add values return zero on success and non-zero on a
* failure. Once an error has happened all subsequent attempts to add more
* values return with non-zero exit status immediatelly. This is designed
* so that we can add several values without checking each return value
* and only check if error has happened at the end of the sequence.
*
* Failures may occur:
* - if we call the functions out of order, e.g. attempt to finish array when
* we are not writing out an array.
* - if we run out of recursion stack
* - may be propagated from the writer function, e.g. allocation failure, no
* space on disk, etc.
*/
#ifndef UJSON_WRITER_H
#define UJSON_WRITER_H
#include <ujson_common.h>
/** @brief A JSON writer */
struct ujson_writer {
unsigned int depth;
char depth_type[UJSON_RECURSION_MAX/8];
char depth_first[UJSON_RECURSION_MAX/8];
/** Handler to print errors and warnings */
void (*err_print)(void *err_print_priv, const char *line);
void *err_print_priv;
char err[UJSON_ERR_MAX];
/** Handler to produce JSON output */
int (*out)(struct ujson_writer *self, const char *buf, size_t buf_size);
void *out_priv;
};
/**
* @brief An ujson_writer initializer with default values.
*
* @param vout A pointer to function to write out the data.
* @param vout_priv An user pointer passed to the out function.
*
* @return An ujson_writer initialized with default values.
*/
#define UJSON_WRITER_INIT(vout, vout_priv) { \
.err_print = UJSON_ERR_PRINT, \
.err_print_priv = UJSON_ERR_PRINT_PRIV, \
.out = vout, \
.out_priv = vout_priv \
}
/**
* @brief Allocates a JSON file writer.
*
* The call may fail either when file cannot be opened for writing or if
* allocation has failed. In all cases errno should be set correctly.
*
* @param path A path to the file, file is opened for writing and created if it
* does not exist.
*
* @return A ujson_writer pointer or NULL in a case of failure.
*/
ujson_writer *ujson_writer_file_open(const char *path);
/**
* @brief Closes and frees a JSON file writer.
*
* @param self A ujson_writer file writer.
*
* @return Zero on success, non-zero on a failure and errno is set.
*/
int ujson_writer_file_close(ujson_writer *self);
/**
* @brief Returns true if writer error happened.
*
* @param self A JSON writer.
*
* @return True if error has happened.
*/
static inline int ujson_writer_err(ujson_writer *self)
{
return !!self->err[0];
}
/**
* @brief Starts a JSON object.
*
* For a top level object the id must be NULL, every other object has to have
* non-NULL id. The call will also fail if maximal recursion depth
* UJSON_RECURSION_MAX has been reached.
*
* @param self A JSON writer.
* @param id An object name.
*
* @return Zero on a success, non-zero otherwise.
*/
int ujson_obj_start(ujson_writer *self, const char *id);
/**
* @brief Finishes a JSON object.
*
* The call will fail if we are currenlty not writing out an object.
*
* @param self A JSON writer.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_finish(ujson_writer *self);
/**
* @brief Starts a JSON array.
*
* For a top level array the id must be NULL, every other array has to have
* non-NULL id. The call will also fail if maximal recursion depth
* UJSON_RECURSION_MAX has been reached.
*
* @param self A JSON writer.
* @param id An array name.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_arr_start(ujson_writer *self, const char *id);
/**
* @brief Finishes a JSON array.
*
* The call will fail if we are currenlty not writing out an array.
*
* @param self A JSON writer.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_arr_finish(ujson_writer *self);
/**
* @brief Adds a null value.
*
* The id must be NULL inside of an array, and must be non-NULL inside of an
* object.
*
* @param self A JSON writer.
* @param id A null value name.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_null_add(ujson_writer *self, const char *id);
/**
* @brief Adds an integer value.
*
* The id must be NULL inside of an array, and must be non-NULL inside of an
* object.
*
* @param self A JSON writer.
* @param id An integer value name.
* @param val An integer value.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_int_add(ujson_writer *self, const char *id, long val);
/**
* @brief Adds a bool value.
*
* The id must be NULL inside of an array, and must be non-NULL inside of an
* object.
*
* @param self A JSON writer.
* @param id An boolean value name.
* @param val A boolean value.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_bool_add(ujson_writer *self, const char *id, int val);
/**
* @brief Adds a float value.
*
* The id must be NULL inside of an array, and must be non-NULL inside of an
* object.
*
* @param self A JSON writer.
* @param id A floating point value name.
* @param val A floating point value.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_float_add(ujson_writer *self, const char *id, double val);
/**
* @brief Adds a string value.
*
* The id must be NULL inside of an array, and must be non-NULL inside of an
* object.
*
* @param self A JSON writer.
* @param id A string value name.
* @param str An UTF8 string value.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_str_add(ujson_writer *self, const char *id, const char *str);
/**
* @brief Finalizes json writer.
*
* Finalizes the json writer, throws possible errors through the error printing
* function.
*
* @param self A JSON writer.
* @return Overall error value.
*/
int ujson_writer_finish(ujson_writer *self);
#endif /* UJSON_WRITER_H */