Replace JSON-GLib by custom JSON dumper library
[metze/wireshark/wip.git] / wsutil / json_dumper.h
1 /* wsjson.h
2  * Routines for serializing data as JSON.
3  *
4  * Copyright 2018, Peter Wu <peter@lekensteyn.nl>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #ifndef __JSON_DUMPER_H__
14 #define __JSON_DUMPER_H__
15
16 #include "ws_symbol_export.h"
17 #include <glib.h>
18 #include <stdio.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 /**
25  * Example:
26  *
27  *  json_dumper dumper = {
28  *      .output_file = stdout,
29  *      .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT,
30  *  };
31  *  json_dumper_begin_object(&dumper);
32  *  json_dumper_set_member_name(&dumper, "key");
33  *  json_dumper_value_string(&dumper, "value");
34  *  json_dumper_set_member_name(&dumper, "array");
35  *  json_dumper_begin_array(&dumper);
36  *  json_dumper_value_anyf(&dumper, "true");
37  *  json_dumper_value_anyf(&dumper, "1.0");
38  *  json_dumper_begin_object(&dumper);
39  *  json_dumper_end_object(&dumper);
40  *  json_dumper_begin_array(&dumper);
41  *  json_dumper_end_array(&dumper);
42  *  json_dumper_end_array(&dumper);
43  *  json_dumper_end_object(&dumper);
44  *  json_dumper_finish(&dumper);
45  */
46
47 /** Maximum object/array nesting depth. */
48 #define JSON_DUMPER_MAX_DEPTH   15
49 typedef struct json_dumper {
50     FILE   *output_file;    /**< Output file, must be set. */
51 #define JSON_DUMPER_FLAGS_PRETTY_PRINT  (1 << 0)    /* Enable pretty printing. */
52     int     flags;
53     /* for internal use, initialize with zeroes. */
54     int     current_depth;
55     guint8  state[JSON_DUMPER_MAX_DEPTH];
56 } json_dumper;
57
58 WS_DLL_PUBLIC void
59 json_dumper_begin_object(json_dumper *dumper);
60
61 WS_DLL_PUBLIC void
62 json_dumper_set_member_name(json_dumper *dumper, const char *name);
63
64 WS_DLL_PUBLIC void
65 json_dumper_end_object(json_dumper *dumper);
66
67 WS_DLL_PUBLIC void
68 json_dumper_begin_array(json_dumper *dumper);
69
70 WS_DLL_PUBLIC void
71 json_dumper_end_array(json_dumper *dumper);
72
73 WS_DLL_PUBLIC void
74 json_dumper_value_string(json_dumper *dumper, const char *value);
75
76 /**
77  * Dump number, "true", "false" or "null" values.
78  */
79 WS_DLL_PUBLIC void
80 json_dumper_value_anyf(json_dumper *dumper, const char *format, ...)
81 G_GNUC_PRINTF(2, 3);
82
83 /**
84  * Finishes dumping data. Returns TRUE if everything is okay and FALSE if
85  * something went wrong (open/close mismatch, missing values, etc.).
86  */
87 WS_DLL_PUBLIC gboolean
88 json_dumper_finish(json_dumper *dumper);
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94 #endif /* __JSON_DUMPER_H__ */
95
96 /*
97  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
98  *
99  * Local variables:
100  * c-basic-offset: 4
101  * tab-width: 8
102  * indent-tabs-mode: nil
103  * End:
104  *
105  * vi: set shiftwidth=4 tabstop=8 expandtab:
106  * :indentSize=4:tabSize=8:noTabs=true:
107  */