json_dumper: add support to convert dots in underscores.
[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 #define JSON_DUMPER_DOT_TO_UNDERSCORE   (1 << 1)    /* Convert dots to underscores in keys */
53     int     flags;
54     /* for internal use, initialize with zeroes. */
55     int     current_depth;
56     guint8  state[JSON_DUMPER_MAX_DEPTH];
57 } json_dumper;
58
59 WS_DLL_PUBLIC void
60 json_dumper_begin_object(json_dumper *dumper);
61
62 WS_DLL_PUBLIC void
63 json_dumper_set_member_name(json_dumper *dumper, const char *name);
64
65 WS_DLL_PUBLIC void
66 json_dumper_end_object(json_dumper *dumper);
67
68 WS_DLL_PUBLIC void
69 json_dumper_begin_array(json_dumper *dumper);
70
71 WS_DLL_PUBLIC void
72 json_dumper_end_array(json_dumper *dumper);
73
74 WS_DLL_PUBLIC void
75 json_dumper_value_string(json_dumper *dumper, const char *value);
76
77 /**
78  * Dump number, "true", "false" or "null" values.
79  */
80 WS_DLL_PUBLIC void
81 json_dumper_value_anyf(json_dumper *dumper, const char *format, ...)
82 G_GNUC_PRINTF(2, 3);
83
84 /**
85  * Finishes dumping data. Returns TRUE if everything is okay and FALSE if
86  * something went wrong (open/close mismatch, missing values, etc.).
87  */
88 WS_DLL_PUBLIC gboolean
89 json_dumper_finish(json_dumper *dumper);
90
91 #ifdef __cplusplus
92 }
93 #endif
94
95 #endif /* __JSON_DUMPER_H__ */
96
97 /*
98  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
99  *
100  * Local variables:
101  * c-basic-offset: 4
102  * tab-width: 8
103  * indent-tabs-mode: nil
104  * End:
105  *
106  * vi: set shiftwidth=4 tabstop=8 expandtab:
107  * :indentSize=4:tabSize=8:noTabs=true:
108  */