Branch data Line data Source code
1 : : #ifndef FLATCC_JSON_PRINTER_H
2 : : #define FLATCC_JSON_PRINTER_H
3 : :
4 : : /*
5 : : * Definitions for default implementation, do not assume these are
6 : : * always valid.
7 : : */
8 : : #define FLATCC_JSON_PRINT_FLUSH_SIZE (1024 * 16)
9 : : #define FLATCC_JSON_PRINT_RESERVE 64
10 : : #define FLATCC_JSON_PRINT_BUFFER_SIZE (FLATCC_JSON_PRINT_FLUSH_SIZE + FLATCC_JSON_PRINT_RESERVE)
11 : :
12 : : /* Initial size that grows exponentially. */
13 : : #define FLATCC_JSON_PRINT_DYN_BUFFER_SIZE 4096
14 : :
15 : :
16 : : #include <stdlib.h>
17 : : #include <string.h>
18 : :
19 : : #include "flatcc/flatcc_rtconfig.h"
20 : : #include "flatcc/flatcc_flatbuffers.h"
21 : :
22 : : /* -DFLATCC_PORTABLE may help if inttypes.h is missing. */
23 : : #ifndef PRId64
24 : : #include <inttypes.h>
25 : : #endif
26 : :
27 : : #define FLATCC_JSON_PRINT_ERROR_MAP(XX) \
28 : : XX(ok, "ok") \
29 : : /* \
30 : : * When the flatbuffer is null, has too small a header, or has \
31 : : * mismatching identifier when a match was requested. \
32 : : */ \
33 : : XX(bad_input, "bad input") \
34 : : XX(deep_recursion, "deep recursion") \
35 : : /* \
36 : : * When the output was larger than the available fixed size buffer, \
37 : : * or dynamic allocation could not grow the buffer sufficiently. \
38 : : */ \
39 : : XX(overflow, "overflow")
40 : :
41 : : enum flatcc_json_printer_error_no {
42 : : #define XX(no, str) flatcc_json_printer_error_##no,
43 : : FLATCC_JSON_PRINT_ERROR_MAP(XX)
44 : : #undef XX
45 : : };
46 : :
47 : : #define flatcc_json_printer_ok flatcc_json_printer_error_ok
48 : :
49 : : typedef struct flatcc_json_printer_ctx flatcc_json_printer_t;
50 : :
51 : : typedef void flatcc_json_printer_flush_f(flatcc_json_printer_t *ctx, int all);
52 : :
53 : : struct flatcc_json_printer_ctx {
54 : : char *buf;
55 : : size_t size;
56 : : size_t flush_size;
57 : : size_t total;
58 : : const char *pflush;
59 : : char *p;
60 : : uint8_t own_buffer;
61 : : uint8_t indent;
62 : : uint8_t unquote;
63 : : uint8_t noenum;
64 : : uint8_t skip_default;
65 : : uint8_t force_default;
66 : : int level;
67 : : int error;
68 : :
69 : : void *fp;
70 : : flatcc_json_printer_flush_f *flush;
71 : : };
72 : :
73 : : static inline void flatcc_json_printer_set_error(flatcc_json_printer_t *ctx, int err)
74 : : {
75 [ # # ][ # # ]: 0 : if (!ctx->error) {
[ # # ][ # # ]
[ # # ][ # # ]
76 : 0 : ctx->error = err;
77 : : }
78 : : }
79 : :
80 : : const char *flatcc_json_printer_error_string(int err);
81 : :
82 : : static inline int flatcc_json_printer_get_error(flatcc_json_printer_t *ctx)
83 : : {
84 : : return ctx->error;
85 : : }
86 : :
87 : : /*
88 : : * Call to reuse context between parses without without
89 : : * returning buffer. If a file pointer is being used,
90 : : * it will remain open.
91 : : *
92 : : * Reset does not affect the formatting settings indentation, and
93 : : * operational flags, but does zero the indentation level.
94 : : */
95 : : static inline void flatcc_json_printer_reset(flatcc_json_printer_t *ctx)
96 : : {
97 : : ctx->p = ctx->buf;
98 : : ctx->level = 0;
99 : : ctx->total = 0;
100 : : ctx->error = 0;
101 : : }
102 : :
103 : : /*
104 : : * A custom init function can be implemented with a custom flush
105 : : * function can be custom implemented. A few have been provided:
106 : : * init with external fixed size buffer, and init with dynamically
107 : : * growing buffer.
108 : : *
109 : : * Because there are a lot of small print functions, it is essentially
110 : : * always faster to print to local buffer than moving to io directly
111 : : * such as using fprintf or fwrite. The flush callback is used to
112 : : * move data when enough has been collected.
113 : : *
114 : : * `fp` should be of type `FILE *` but we do not enforce it here
115 : : * because it allows the header to be independent of <stdio.h>
116 : : * when not required. If `fp` is null, it defaults to stdout.
117 : : *
118 : : * Returns -1 on alloc error (no cleanup needed), or 0 on success.
119 : : * Eventually the clear method must be called to return memory.
120 : : *
121 : : * The file pointer may be stdout or a custom file. The file pointer
122 : : * is not affected by reset or clear and should be closed manually.
123 : : *
124 : : * `set_flags` and related may be called subsequently to modify
125 : : * behavior.
126 : : */
127 : : int flatcc_json_printer_init(flatcc_json_printer_t *ctx, void *fp);
128 : :
129 : : /*
130 : : * Prints to external buffer and sets overflow error if buffer is too
131 : : * small. Earlier content is then overwritten. A custom version of this
132 : : * function could flush the content to elsewhere before allowing the
133 : : * buffer content to be overwritten. The `buffers_size` must be large
134 : : * enough to hold `FLATCC_JSON_PRINT_RESERVED_SIZE` which is small but
135 : : * large enough value to hold entire numbers and the like.
136 : : *
137 : : * It is not strictly necessary to call clear because the buffer is
138 : : * external, but still good form and case the context type is changed
139 : : * later.
140 : : *
141 : : * Returns -1 on buffer size error (no cleanup needed), or 0 on success.
142 : : *
143 : : * `set_flags` and related may be called subsequently to modify
144 : : * behavior.
145 : : */
146 : : int flatcc_json_printer_init_buffer(flatcc_json_printer_t *ctx, char *buffer, size_t buffer_size);
147 : :
148 : : /*
149 : : * Returns the current buffer pointer and also the content size in
150 : : * `buffer_size` if it is null. The operation is not very useful for
151 : : * file oriented printers (created with `init`) and will then only
152 : : * return the unflushed buffer content. For fixed size buffers
153 : : * (`init_buffer`), only the last content is available if the buffer
154 : : * overflowed. Works well with (`init_buffer`) when the dynamic buffer
155 : : * is be reused, otherwise `finalize_dynamic_buffer` could be more
156 : : * appropriate.
157 : : *
158 : : * The returned buffer is zero terminated.
159 : : *
160 : : * The returned pointer is only valid until next operation and should
161 : : * not deallocated manually.
162 : : */
163 : : void *flatcc_json_printer_get_buffer(flatcc_json_printer_t *ctx, size_t *buffer_size);
164 : :
165 : : /*
166 : : * Set to non-zero if names and enum symbols can be unquoted thus
167 : : * diverging from standard JSON while remaining compatible with `flatc`
168 : : * JSON flavor.
169 : : */
170 : : static inline void flatcc_json_printer_set_unquoted(flatcc_json_printer_t *ctx, int x)
171 : : {
172 : : ctx->unquote = !!x;
173 : : }
174 : :
175 : : /*
176 : : * Set to non-zero if enums should always be printed as numbers.
177 : : * Otherwise enums are printed as a symbol for member values, and as
178 : : * numbers for other values.
179 : : *
180 : : * NOTE: this setting will not affect code generated with enum mapping
181 : : * disabled - statically disabling enum mapping is signficantly faster
182 : : * for enums, less so for for union types.
183 : : */
184 : : static inline void flatcc_json_printer_set_noenum(flatcc_json_printer_t *ctx, int x)
185 : : {
186 : : ctx->noenum = !!x;
187 : : }
188 : :
189 : : /*
190 : : * Override priting an existing scalar field if it equals the default value.
191 : : * Note that this setting is not mutually exclusive to `set_force_default`.
192 : : */
193 : : static inline void flatcc_json_printer_set_skip_default(flatcc_json_printer_t *ctx, int x)
194 : : {
195 : : ctx->skip_default = !!x;
196 : : }
197 : :
198 : : /*
199 : : * Override skipping absent scalar fields and print the default value.
200 : : * Note that this setting is not mutually exclusive to `set_skip_default`.
201 : : */
202 : : static inline void flatcc_json_printer_set_force_default(flatcc_json_printer_t *ctx, int x)
203 : : {
204 : : ctx->force_default = !!x;
205 : : }
206 : :
207 : :
208 : : /*
209 : : * Set pretty-print indentation in number of spaces. 0 (default) is
210 : : * compact with no spaces or linebreaks (default), anything above
211 : : * triggers pretty print.
212 : : */
213 : : static inline void flatcc_json_printer_set_indent(flatcc_json_printer_t *ctx, uint8_t x)
214 : : {
215 : : ctx->indent = x;
216 : : }
217 : :
218 : : /*
219 : : * Override the default compact valid JSON format with a
220 : : * pretty printed non-strict version. Enums are translated
221 : : * to names, which is also the default.
222 : : */
223 : : static inline void flatcc_json_printer_set_nonstrict(flatcc_json_printer_t *ctx)
224 : : {
225 : : flatcc_json_printer_set_indent(ctx, 2);
226 : : flatcc_json_printer_set_unquoted(ctx, 1);
227 : : flatcc_json_printer_set_noenum(ctx, 0);
228 : : }
229 : :
230 : : enum flatcc_json_printer_flags {
231 : : flatcc_json_printer_f_unquote = 1,
232 : : flatcc_json_printer_f_noenum = 2,
233 : : flatcc_json_printer_f_skip_default = 4,
234 : : flatcc_json_printer_f_force_default = 8,
235 : : flatcc_json_printer_f_pretty = 16,
236 : : flatcc_json_printer_f_nonstrict = 32,
237 : : };
238 : :
239 : : /*
240 : : * May be called instead of setting operational modes individually.
241 : : * Formatting is strict quoted json witout pretty printing by default.
242 : : *
243 : : * flags are:
244 : : *
245 : : * `unquote`,
246 : : * `noenum`,
247 : : * `skip_default`,
248 : : * `force_default`,
249 : : * `pretty`,
250 : : * `nonstrict`
251 : : *
252 : : * `pretty` flag sets indentation to 2.
253 : : * `nonstrict` implies: `noenum`, `unquote`, `pretty`.
254 : : */
255 : : static inline void flatcc_json_printer_set_flags(flatcc_json_printer_t *ctx, int flags)
256 : : {
257 : : ctx->unquote = !!(flags & flatcc_json_printer_f_unquote);
258 : : ctx->noenum = !!(flags & flatcc_json_printer_f_noenum);
259 : : ctx->skip_default = !!(flags & flatcc_json_printer_f_skip_default);
260 : : ctx->force_default = !!(flags & flatcc_json_printer_f_force_default);
261 : : if (flags & flatcc_json_printer_f_pretty) {
262 : : flatcc_json_printer_set_indent(ctx, 2);
263 : : }
264 : : if (flags & flatcc_json_printer_f_nonstrict) {
265 : : flatcc_json_printer_set_nonstrict(ctx);
266 : : }
267 : : }
268 : :
269 : :
270 : : /*
271 : : * Detects if the conctext type uses dynamically allocated memory
272 : : * using malloc and realloc and frees any such memory.
273 : : *
274 : : * Not all context types needs to be cleared.
275 : : */
276 : : void flatcc_json_printer_clear(flatcc_json_printer_t *ctx);
277 : :
278 : : /*
279 : : * Ensures that there ia always buffer capacity for priting the next
280 : : * primitive with delimiters.
281 : : *
282 : : * Only flushes complete flush units and is inexpensive to call.
283 : : * The content buffer has an extra reserve which ensures basic
284 : : * data types and delimiters can always be printed after a partial
285 : : * flush. At the end, a `flush` is required to flush the
286 : : * remaining incomplete buffer data.
287 : : *
288 : : * Numbers do not call partial flush but will always fit into the reserve
289 : : * capacity after a partial flush, also surrounded by delimiters.
290 : : *
291 : : * Variable length operations generally submit a partial flush so it is
292 : : * safe to print a number after a name without flushing, but vectors of
293 : : * numbers must (and do) issue a partial flush between elements. This is
294 : : * handled automatically but must be considered if using the primitives
295 : : * for special purposes. Because repeated partial flushes are very cheap
296 : : * this is only a concern for high performance applications.
297 : : *
298 : : * When identiation is enabled, partial flush is also automatically
299 : : * issued .
300 : : */
301 : : static inline void flatcc_json_printer_flush_partial(flatcc_json_printer_t *ctx)
302 : : {
303 [ # # ][ - + ]: 145 : if (ctx->p >= ctx->pflush) {
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ - + ]
[ - + ][ # # ]
304 : 0 : ctx->flush(ctx, 0);
305 : : }
306 : : }
307 : :
308 : : /*
309 : : * Flush the remaining data not flushed by partial flush. It is valid to
310 : : * call at any point if it is acceptable to have unaligned flush units,
311 : : * but this is not desireable if, for example, compression or encryption
312 : : * is added to the flush pipeline.
313 : : *
314 : : * Not called automatically at the end of printing a flatbuffer object
315 : : * in case more data needs to be appended without submitting incomplete
316 : : * flush units prematurely - for example adding a newline at the end.
317 : : *
318 : : * The flush behavior depeends on the underlying `ctx` object, for
319 : : * example dynamic buffers have no distinction between partial and full
320 : : * flushes - here it is merely ensured that the buffer always has a
321 : : * reserve capacity left.
322 : : *
323 : : * Returns the total printed size.
324 : : */
325 : : static inline size_t flatcc_json_printer_flush(flatcc_json_printer_t *ctx)
326 : : {
327 : 0 : ctx->flush(ctx, 1);
328 : : return ctx->total;
329 : : }
330 : :
331 : : /*
332 : : * Helper functions to print anything into the json buffer.
333 : : * Strings are escaped.
334 : : *
335 : : * When pretty printing (indent > 0), level 0 has special significance -
336 : : * so if wrapping printed json in a manually printed container json
337 : : * object, these functions can help manage this.
338 : : */
339 : :
340 : : /* Escaped and quoted string. */
341 : : void flatcc_json_printer_string(flatcc_json_printer_t *ctx, const char *s, int n);
342 : : /* Unescaped and unquoted string. */
343 : : void flatcc_json_printer_write(flatcc_json_printer_t *ctx, const char *s, int n);
344 : : /* Print a newline and issues a partial flush. */
345 : : void flatcc_json_printer_nl(flatcc_json_printer_t *ctx);
346 : : /* Like numbers, a partial flush is not issued. */
347 : : void flatcc_json_printer_char(flatcc_json_printer_t *ctx, char c);
348 : : /* Indents and issues a partial flush. */
349 : : void flatcc_json_printer_indent(flatcc_json_printer_t *ctx);
350 : : /* Adjust identation level, usually +/-1. */
351 : : void flatcc_json_printer_add_level(flatcc_json_printer_t *ctx, int n);
352 : : /* Returns current identation level (0 is top level). */
353 : : int flatcc_json_printer_get_level(flatcc_json_printer_t *ctx);
354 : :
355 : : /*
356 : : * If called explicitly be aware that repeated calls to numeric
357 : : * printers may cause buffer overflow without flush in-between.
358 : : */
359 : : void flatcc_json_printer_uint8(flatcc_json_printer_t *ctx, uint8_t v);
360 : : void flatcc_json_printer_uint16(flatcc_json_printer_t *ctx, uint16_t v);
361 : : void flatcc_json_printer_uint32(flatcc_json_printer_t *ctx, uint32_t v);
362 : : void flatcc_json_printer_uint64(flatcc_json_printer_t *ctx, uint64_t v);
363 : : void flatcc_json_printer_int8(flatcc_json_printer_t *ctx, int8_t v);
364 : : void flatcc_json_printer_int16(flatcc_json_printer_t *ctx, int16_t v);
365 : : void flatcc_json_printer_int32(flatcc_json_printer_t *ctx, int32_t v);
366 : : void flatcc_json_printer_int64(flatcc_json_printer_t *ctx, int64_t v);
367 : : void flatcc_json_printer_bool(flatcc_json_printer_t *ctx, int v);
368 : : void flatcc_json_printer_float(flatcc_json_printer_t *ctx, float v);
369 : : void flatcc_json_printer_double(flatcc_json_printer_t *ctx, double v);
370 : :
371 : : void flatcc_json_printer_enum(flatcc_json_printer_t *ctx,
372 : : const char *symbol, int len);
373 : :
374 : : /*
375 : : * Convenience function to add a trailing newline, flush the buffer,
376 : : * test for error and reset the context for reuse.
377 : : *
378 : : * Returns total size printed or < 0 on error.
379 : : *
380 : : * This function makes most sense for file oriented output.
381 : : * See also `finalize_dynamic_buffer`.
382 : : */
383 : : static inline int flatcc_json_printer_finalize(flatcc_json_printer_t *ctx)
384 : : {
385 : : int ret;
386 : : flatcc_json_printer_nl(ctx);
387 : : ret = (int)flatcc_json_printer_flush(ctx);
388 : : if (ctx->error) {
389 : : ret = -1;
390 : : }
391 : : flatcc_json_printer_reset(ctx);
392 : : return ret;
393 : : }
394 : :
395 : : /*
396 : : * Allocates a small buffer and grows it dynamically.
397 : : * Buffer survives past reset. To reduce size between uses, call clear
398 : : * followed by init call. To reuse buffer just call reset between uses.
399 : : * If `buffer_size` is 0 a sensible default is being used. The size is
400 : : * automatically rounded up to reserved size if too small.
401 : : *
402 : : * Returns -1 on alloc error (no cleanup needed), or 0 on success.
403 : : * Eventually the clear method must be called to return memory.
404 : : *
405 : : * `set_flags` and related may be called subsequently to modify
406 : : * behavior.
407 : : */
408 : : int flatcc_json_printer_init_dynamic_buffer(flatcc_json_printer_t *ctx, size_t buffer_size);
409 : :
410 : : /*
411 : : * Similar to calling `finalize` but returns the buffer and does NOT
412 : : * reset, but rather clears printer object and the returned buffer must
413 : : * be deallocated with `free`.
414 : : *
415 : : * The returned buffer is zero terminated.
416 : : *
417 : : * NOTE: it is entirely optional to use this method. For repeated used
418 : : * of dynamic buffers, `newline` (or not) followed by `get_buffer`
419 : : * and `reset` will be an alternative.
420 : : *
421 : : * Stores the printed buffer size in `buffer_size` if it is not null.
422 : : *
423 : : * See also `get_dynamic_buffer`.
424 : : */
425 : : void *flatcc_json_printer_finalize_dynamic_buffer(flatcc_json_printer_t *ctx, size_t *buffer_size);
426 : :
427 : :
428 : : /*************************************************************
429 : : * The following is normally only used by generated code.
430 : : *************************************************************/
431 : :
432 : : typedef struct flatcc_json_printer_table_descriptor flatcc_json_printer_table_descriptor_t;
433 : :
434 : : struct flatcc_json_printer_table_descriptor {
435 : : const void *table;
436 : : const void *vtable;
437 : : int vsize;
438 : : int ttl;
439 : : int count;
440 : : };
441 : :
442 : : typedef void flatcc_json_printer_table_f(flatcc_json_printer_t *ctx,
443 : : flatcc_json_printer_table_descriptor_t *td);
444 : :
445 : : typedef void flatcc_json_printer_struct_f(flatcc_json_printer_t *ctx,
446 : : const void *p);
447 : :
448 : : /* Generated value to name map callbacks. */
449 : : typedef void flatcc_json_printer_uint8_enum_f(flatcc_json_printer_t *ctx, uint8_t v);
450 : : typedef void flatcc_json_printer_uint16_enum_f(flatcc_json_printer_t *ctx, uint16_t v);
451 : : typedef void flatcc_json_printer_uint32_enum_f(flatcc_json_printer_t *ctx, uint32_t v);
452 : : typedef void flatcc_json_printer_uint64_enum_f(flatcc_json_printer_t *ctx, uint64_t v);
453 : : typedef void flatcc_json_printer_int8_enum_f(flatcc_json_printer_t *ctx, int8_t v);
454 : : typedef void flatcc_json_printer_int16_enum_f(flatcc_json_printer_t *ctx, int16_t v);
455 : : typedef void flatcc_json_printer_int32_enum_f(flatcc_json_printer_t *ctx, int32_t v);
456 : : typedef void flatcc_json_printer_int64_enum_f(flatcc_json_printer_t *ctx, int64_t v);
457 : : typedef void flatcc_json_printer_bool_enum_f(flatcc_json_printer_t *ctx, flatbuffers_bool_t v);
458 : :
459 : : #define __define_print_scalar_field_proto(TN, T) \
460 : : void flatcc_json_printer_ ## TN ## _field(flatcc_json_printer_t *ctx, \
461 : : flatcc_json_printer_table_descriptor_t *td, \
462 : : int id, const char *name, int len, T v);
463 : :
464 : : #define __define_print_scalar_struct_field_proto(TN, T) \
465 : : void flatcc_json_printer_ ## TN ## _struct_field(flatcc_json_printer_t *ctx,\
466 : : int index, const void *p, size_t offset, \
467 : : const char *name, int len);
468 : :
469 : : #define __define_print_enum_struct_field_proto(TN, T) \
470 : : void flatcc_json_printer_ ## TN ## _enum_struct_field( \
471 : : flatcc_json_printer_t *ctx, \
472 : : int index, const void *p, size_t offset, \
473 : : const char *name, int len, \
474 : : flatcc_json_printer_ ## TN ##_enum_f *pf);
475 : :
476 : : #define __define_print_enum_field_proto(TN, T) \
477 : : void flatcc_json_printer_ ## TN ## _enum_field(flatcc_json_printer_t *ctx, \
478 : : flatcc_json_printer_table_descriptor_t *td, \
479 : : int id, const char *name, int len, T v, \
480 : : flatcc_json_printer_ ## TN ##_enum_f *pf);
481 : :
482 : : #define __define_print_scalar_vector_field_proto(TN, T) \
483 : : void flatcc_json_printer_ ## TN ## _vector_field(flatcc_json_printer_t *ctx,\
484 : : flatcc_json_printer_table_descriptor_t *td, \
485 : : int id, const char *name, int len);
486 : :
487 : : #define __define_print_enum_vector_field_proto(TN, T) \
488 : : void flatcc_json_printer_ ## TN ## _enum_vector_field( \
489 : : flatcc_json_printer_t *ctx, \
490 : : flatcc_json_printer_table_descriptor_t *td, \
491 : : int id, const char *name, int len, \
492 : : flatcc_json_printer_ ## TN ##_enum_f *pf);
493 : :
494 : : __define_print_scalar_field_proto(uint8, uint8_t)
495 : : __define_print_scalar_field_proto(uint16, uint16_t)
496 : : __define_print_scalar_field_proto(uint32, uint32_t)
497 : : __define_print_scalar_field_proto(uint64, uint64_t)
498 : : __define_print_scalar_field_proto(int8, int8_t)
499 : : __define_print_scalar_field_proto(int16, int16_t)
500 : : __define_print_scalar_field_proto(int32, int32_t)
501 : : __define_print_scalar_field_proto(int64, int64_t)
502 : : __define_print_scalar_field_proto(bool, flatbuffers_bool_t)
503 : : __define_print_scalar_field_proto(float, float)
504 : : __define_print_scalar_field_proto(double, double)
505 : :
506 : : __define_print_enum_field_proto(uint8, uint8_t)
507 : : __define_print_enum_field_proto(uint16, uint16_t)
508 : : __define_print_enum_field_proto(uint32, uint32_t)
509 : : __define_print_enum_field_proto(uint64, uint64_t)
510 : : __define_print_enum_field_proto(int8, int8_t)
511 : : __define_print_enum_field_proto(int16, int16_t)
512 : : __define_print_enum_field_proto(int32, int32_t)
513 : : __define_print_enum_field_proto(int64, int64_t)
514 : : __define_print_enum_field_proto(bool, flatbuffers_bool_t)
515 : :
516 : : __define_print_scalar_struct_field_proto(uint8, uint8_t)
517 : : __define_print_scalar_struct_field_proto(uint16, uint16_t)
518 : : __define_print_scalar_struct_field_proto(uint32, uint32_t)
519 : : __define_print_scalar_struct_field_proto(uint64, uint64_t)
520 : : __define_print_scalar_struct_field_proto(int8, int8_t)
521 : : __define_print_scalar_struct_field_proto(int16, int16_t)
522 : : __define_print_scalar_struct_field_proto(int32, int32_t)
523 : : __define_print_scalar_struct_field_proto(int64, int64_t)
524 : : __define_print_scalar_struct_field_proto(bool, flatbuffers_bool_t)
525 : : __define_print_scalar_struct_field_proto(float, float)
526 : : __define_print_scalar_struct_field_proto(double, double)
527 : :
528 : : __define_print_enum_struct_field_proto(uint8, uint8_t)
529 : : __define_print_enum_struct_field_proto(uint16, uint16_t)
530 : : __define_print_enum_struct_field_proto(uint32, uint32_t)
531 : : __define_print_enum_struct_field_proto(uint64, uint64_t)
532 : : __define_print_enum_struct_field_proto(int8, int8_t)
533 : : __define_print_enum_struct_field_proto(int16, int16_t)
534 : : __define_print_enum_struct_field_proto(int32, int32_t)
535 : : __define_print_enum_struct_field_proto(int64, int64_t)
536 : : __define_print_enum_struct_field_proto(bool, flatbuffers_bool_t)
537 : :
538 : : __define_print_scalar_vector_field_proto(uint8, uint8_t)
539 : : __define_print_scalar_vector_field_proto(uint16, uint16_t)
540 : : __define_print_scalar_vector_field_proto(uint32, uint32_t)
541 : : __define_print_scalar_vector_field_proto(uint64, uint64_t)
542 : : __define_print_scalar_vector_field_proto(int8, int8_t)
543 : : __define_print_scalar_vector_field_proto(int16, int16_t)
544 : : __define_print_scalar_vector_field_proto(int32, int32_t)
545 : : __define_print_scalar_vector_field_proto(int64, int64_t)
546 : : __define_print_scalar_vector_field_proto(bool, flatbuffers_bool_t)
547 : : __define_print_scalar_vector_field_proto(float, float)
548 : : __define_print_scalar_vector_field_proto(double, double)
549 : :
550 : : __define_print_enum_vector_field_proto(uint8, uint8_t)
551 : : __define_print_enum_vector_field_proto(uint16, uint16_t)
552 : : __define_print_enum_vector_field_proto(uint32, uint32_t)
553 : : __define_print_enum_vector_field_proto(uint64, uint64_t)
554 : : __define_print_enum_vector_field_proto(int8, int8_t)
555 : : __define_print_enum_vector_field_proto(int16, int16_t)
556 : : __define_print_enum_vector_field_proto(int32, int32_t)
557 : : __define_print_enum_vector_field_proto(int64, int64_t)
558 : : __define_print_enum_vector_field_proto(bool, flatbuffers_bool_t)
559 : :
560 : : /*
561 : : * If `fid` is null, the identifier is not checked and is allowed to be
562 : : * entirely absent.
563 : : *
564 : : * The buffer must at least be aligned to uoffset_t on systems that
565 : : * require aligned memory addresses (as always for flatbuffers).
566 : : */
567 : : int flatcc_json_printer_table_as_root(flatcc_json_printer_t *ctx,
568 : : const void *buf, size_t bufsiz, const char *fid,
569 : : flatcc_json_printer_table_f *pf);
570 : :
571 : : int flatcc_json_printer_struct_as_root(flatcc_json_printer_t *ctx,
572 : : const void *buf, size_t bufsiz, const char *fid,
573 : : flatcc_json_printer_struct_f *pf);
574 : :
575 : : /*
576 : : * Call before and after enum flags to ensure proper quotation. Enum
577 : : * quotes may be configured runtime, but regardless of this, multiple
578 : : * flags may be forced to be quoted depending on compile time flag since
579 : : * not all parsers may be able to handle unquoted space separated values
580 : : * even if they handle non-strict unquoted json otherwise.
581 : : *
582 : : * Flags should only be called when not empty (0) and when there are no
583 : : * unknown flags in the value. Otherwise print the numeric value. The
584 : : * auto generated code deals with this.
585 : : *
586 : : * This bit twiddling hack may be useful:
587 : : *
588 : : * `multiple = 0 != (v & (v - 1);`
589 : : */
590 : : void flatcc_json_printer_delimit_enum_flags(flatcc_json_printer_t *ctx, int multiple);
591 : :
592 : : /* The index increments from 0 to handle space. It is not the flag bit position. */
593 : : void flatcc_json_printer_enum_flag(flatcc_json_printer_t *ctx, int index, const char *symbol, int len);
594 : :
595 : : /* A struct inside another struct, as opposed to inside a table or a root. */
596 : : void flatcc_json_printer_embedded_struct_field(flatcc_json_printer_t *ctx,
597 : : int index, const void *p, size_t offset,
598 : : const char *name, int len,
599 : : flatcc_json_printer_struct_f pf);
600 : :
601 : : void flatcc_json_printer_struct_field(flatcc_json_printer_t *ctx,
602 : : flatcc_json_printer_table_descriptor_t *td,
603 : : int id, const char *name, int len,
604 : : flatcc_json_printer_struct_f *pf);
605 : :
606 : : void flatcc_json_printer_string_field(flatcc_json_printer_t *ctx,
607 : : flatcc_json_printer_table_descriptor_t *td,
608 : : int id, const char *name, int len);
609 : :
610 : : void flatcc_json_printer_string_vector_field(flatcc_json_printer_t *ctx,
611 : : flatcc_json_printer_table_descriptor_t *td,
612 : : int id, const char *name, int len);
613 : :
614 : : void flatcc_json_printer_table_field(flatcc_json_printer_t *ctx,
615 : : flatcc_json_printer_table_descriptor_t *td,
616 : : int id, const char *name, int len,
617 : : flatcc_json_printer_table_f pf);
618 : :
619 : : void flatcc_json_printer_struct_vector_field(flatcc_json_printer_t *ctx,
620 : : flatcc_json_printer_table_descriptor_t *td,
621 : : int id, const char *name, int len,
622 : : size_t size,
623 : : flatcc_json_printer_struct_f pf);
624 : :
625 : : void flatcc_json_printer_table_vector_field(flatcc_json_printer_t *ctx,
626 : : flatcc_json_printer_table_descriptor_t *td,
627 : : int id, const char *name, int len,
628 : : flatcc_json_printer_table_f pf);
629 : :
630 : : void flatcc_json_printer_struct_as_nested_root(flatcc_json_printer_t *ctx,
631 : : flatcc_json_printer_table_descriptor_t *td,
632 : : int id, const char *name, int len,
633 : : const char *fid,
634 : : flatcc_json_printer_struct_f *pf);
635 : :
636 : : void flatcc_json_printer_table_as_nested_root(flatcc_json_printer_t *ctx,
637 : : flatcc_json_printer_table_descriptor_t *td,
638 : : int id, const char *name, int len,
639 : : const char *fid,
640 : : flatcc_json_printer_table_f pf);
641 : :
642 : : int flatcc_json_printer_read_union_type(
643 : : flatcc_json_printer_table_descriptor_t *td,
644 : : int id);
645 : :
646 : : void flatcc_json_printer_union_type(flatcc_json_printer_t *ctx,
647 : : flatcc_json_printer_table_descriptor_t *td,
648 : : const char *name, int len, int type,
649 : : const char *type_name, int type_len);
650 : :
651 : : #endif /* FLATCC_JSON_PRINTER_H */
|