JsoNeat
Some JSON parse/iterate C++ classes build on top of Jsmn
Loading...
Searching...
No Matches
to_json_cbuf.hh
1/**
2 * \brief Serialize objects to JSON using char array buffers. For smaller objects which
3 * fit into such a buffer.
4 */
5
6#pragma once
7
8#include "jsoneat.hh"
9
10namespace jsoneat::to_json::cbuf {
11
12/**
13 * \brief Print value of a JSON key/value pair and append trailing comma.
14 * \param dst output buffer
15 * \param dst_size output buffer size
16 * \param val input value
17 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
18 */
19inline int to_json_val(char *dst, size_t dst_size, bool val) {
20 return snprintf(dst, dst_size, //
21 R"(%s,)", val ? "true" : "false");
22}
23
24/**
25 * \brief Print value of a JSON key/value pair and append trailing comma.
26 * \param dst output buffer
27 * \param dst_size output buffer size
28 * \param val input value
29 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
30 */
31inline int to_json_val(char *dst, size_t dst_size, int val) {
32 return snprintf(dst, dst_size, //
33 R"(%d,)", val);
34}
35/**
36 * \brief Print value of a JSON key/value pair and append trailing comma.
37 * \param dst output buffer
38 * \param dst_size output buffer size
39 * \param val input value
40 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
41 */
42inline int to_json_val(char *dst, size_t dst_size, long val) {
43 return snprintf(dst, dst_size, //
44 R"(%ld,)", val);
45}
46/**
47 * \brief Print value of a JSON key/value pair and append trailing comma.
48 * \param dst output buffer
49 * \param dst_size output buffer size
50 * \param val input value
51 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
52 */
53inline int to_json_val(char *dst, size_t dst_size, long long val) {
54 return snprintf(dst, dst_size, //
55 R"(%lld,)", val);
56}
57/**
58 * \brief Print value of a JSON key/value pair and append trailing comma.
59 * \param dst output buffer
60 * \param dst_size output buffer size
61 * \param val input value
62 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
63 */
64inline int to_json_val(char *dst, size_t dst_size, unsigned val) {
65 return snprintf(dst, dst_size, //
66 R"(%u,)", val);
67}
68
69/**
70 * \brief Print value of a JSON key/value pair and append trailing comma.
71 * \param dst output buffer
72 * \param dst_size output buffer size
73 * \param val input value
74 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
75 */
76inline int to_json_val(char *dst, size_t dst_size, float val) {
77 return snprintf(dst, dst_size, //
78 R"(%f,)", val);
79}
80/**
81 * \brief Print value of a JSON key/value pair and append trailing comma.
82 * \param dst output buffer
83 * \param dst_size output buffer size
84 * \param val input value
85 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
86 */
87template<size_t SIZE>
88int to_json_val(char *dst, size_t dst_size, const char (&val)[SIZE]) {
89 return snprintf(dst, dst_size, //
90 R"("%s",)", val);
91}
92
93/**
94 * \brief Print value of a JSON key/value pair and append trailing comma.
95 * \param dst output buffer
96 * \param dst_size output buffer size
97 * \param val input value, which is an object
98 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
99 */
100template<class C, typename std::enable_if<std::is_class_v<C>>::type* = nullptr>
101int to_json_val(char *dst, size_t dst_size, const C &val, bool append_comma = true) {
102 int res = 0;
103
104 if (res < dst_size)
105 dst[res] = '{';
106 ++res;
107
108 if (res < dst_size)
109 res += val._to_json(dst + res, dst_size - res);
110 else
111 res += val._to_json(dst, 0);
112
113 //if (dst[res - 1] == ',')
114 --res;
115
116 if (res < dst_size)
117 dst[res] = '}';
118 ++res;
119
120 if (append_comma) {
121 if (res < dst_size)
122 dst[res] = ',';
123 ++res;
124 }
125
126 if (res < dst_size)
127 dst[res] = '\0';
128
129 return res;
130
131}
132
133/**
134 * \brief Print value of a JSON key/value pair and append trailing comma.
135 * \param dst output buffer
136 * \param dst_size output buffer size
137 * \param val input value, which is a std::array
138 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
139 */
140template<typename T, size_t SIZE>
141int to_json_val(char *dst, size_t dst_size, const std::array<T, SIZE> &val, bool append_comma = true) {
142 int res = 0;
143
144 if (res < dst_size)
145 dst[res] = '[';
146 ++res;
147
148 for (int i = 0; i < SIZE; ++i) {
149 if (res < dst_size)
150 res += to_json_val(dst + res, dst_size - res, val[i]);
151 else
152 res += to_json_val(dst, 0, val[i]);
153
154 }
155 // if (dst[res - 1] == ',')
156 --res;
157
158 if (res < dst_size)
159 dst[res] = ']';
160 ++res;
161
162 if (append_comma) {
163 if (res < dst_size)
164 dst[res] = ',';
165 ++res;
166 }
167
168 if (res < dst_size)
169 dst[res] = '\0';
170
171 return res;
172}
173
174/**
175 * \brief Print value of a JSON key/value pair and append trailing comma.
176 * \param dst output buffer
177 * \param dst_size output buffer size
178 * \param val input value, which is a C style array
179 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
180 */
181template<typename T, size_t SIZE>
182int to_json_val(char *dst, size_t dst_size, const T (&val)[SIZE], bool append_comma = true) {
183 int res = 0;
184
185 if (res < dst_size)
186 dst[res] = '[';
187 ++res;
188
189 for (int i = 0; i < SIZE; ++i) {
190 if (res < dst_size)
191 res += to_json_val(dst + res, dst_size - res, val[i]);
192 else
193 res += to_json_val(dst, 0, val[i]);
194
195 }
196 // if (dst[res - 1] == ',')
197 --res;
198
199 if (res < dst_size)
200 dst[res] = ']';
201 ++res;
202
203 if (append_comma) {
204 if (res < dst_size)
205 dst[res] = ',';
206 ++res;
207 }
208
209 if (res < dst_size)
210 dst[res] = '\0';
211
212 return res;
213}
214
215/**
216 * \brief Print key and value of a JSON key/value pair and append trailing comma.
217 * \param dst output buffer
218 * \param dst_size output buffer size
219 * \param kvp input key/value pair
220 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
221 */
222template<class T>
223int to_json_kvp(char *dst, size_t dst_size, const jsoneat::KvPair<T> kvp) {
224 auto res = snprintf(dst, dst_size, R"("%s":)", kvp.key);
225 if (res < dst_size)
226 return res + to_json_val(dst + res, dst_size - res, kvp.val);
227 else
228 return res + to_json_val(dst, 0, kvp.val);
229}
230
231/**
232 * \brief Print key and value of a JSON key/value pair and append trailing comma.
233 * \param dst output buffer
234 * \param dst_size output buffer size
235 * \param kvp input key/value pair. Value is C style char array.
236 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
237 */
238template<size_t SIZE>
239int to_json_kvp(char *dst, size_t dst_size, const jsoneat::KvPair<char[SIZE]> kvp) {
240 return snprintf(dst, dst_size, //
241 R"("%s":"%s",)", kvp.key, kvp.val);
242}
243
244namespace {
245/**
246 * \brief This overloaded function is called last in recursion
247 * \return Always 0
248 */
249inline int to_json_args(char*, size_t) {
250 return 0;
251}
252} // namespace
253
254/**
255 * \brief
256 * \tparam T Prints an argument-list of key/value pairs as JSON
257 * \tparam Args list of types which should each be an instance of struct template jsoneat::KvPair
258 * \param dst output buffer
259 * \param dst_size output buffer size
260 * \param pair First key/value pair in argument list
261 * \param args Rest of argument list
262 * \return Like snprintf(3), the number of characters needed. If smaller than dst_size, the output was fully written
263 */
264template<typename T, typename ... Args>
265int to_json_args(char *dst, size_t dst_size, const T &pair, Args ... args) {
266 auto res = to_json_kvp(dst, dst_size, pair);
267
268 if (res < dst_size)
269 return res + to_json_args(dst + res, dst_size - res, args...);
270 else
271 return res + to_json_args(dst, 0, args...);
272}
273
274}
int to_json_val(char *dst, size_t dst_size, unsigned val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:64
int to_json_val(char *dst, size_t dst_size, const T(&val)[SIZE], bool append_comma=true)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:182
int to_json_val(char *dst, size_t dst_size, const std::array< T, SIZE > &val, bool append_comma=true)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:141
int to_json_kvp(char *dst, size_t dst_size, const jsoneat::KvPair< char[SIZE]> kvp)
Print key and value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:239
int to_json_val(char *dst, size_t dst_size, const char(&val)[SIZE])
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:88
int to_json_val(char *dst, size_t dst_size, long val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:42
int to_json_val(char *dst, size_t dst_size, bool val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:19
int to_json_kvp(char *dst, size_t dst_size, const jsoneat::KvPair< T > kvp)
Print key and value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:223
int to_json_val(char *dst, size_t dst_size, float val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:76
int to_json_val(char *dst, size_t dst_size, long long val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:53
int to_json_val(char *dst, size_t dst_size, const C &val, bool append_comma=true)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:101
int to_json_val(char *dst, size_t dst_size, int val)
Print value of a JSON key/value pair and append trailing comma.
Definition to_json_cbuf.hh:31
int to_json_args(char *dst, size_t dst_size, const T &pair, Args ... args)
Definition to_json_cbuf.hh:265
pair of a reference and a json-key
Definition jsoneat.hh:700