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_double(&dumper, 1.0);
38  *  json_dumper_begin_base64(&dumper);
39  *  json_dumper_write_base64(&dumper, (const guchar *)"abcd", 4);
40  *  json_dumper_write_base64(&dumper, (const guchar *)"1234", 4);
41  *  json_dumper_end_base64(&dumper);
42  *  json_dumper_begin_object(&dumper);
43  *  json_dumper_end_object(&dumper);
44  *  json_dumper_begin_array(&dumper);
45  *  json_dumper_end_array(&dumper);
46  *  json_dumper_end_array(&dumper);
47  *  json_dumper_end_object(&dumper);
48  *  json_dumper_finish(&dumper);
49  */
50 
51 /** Maximum object/array nesting depth. */
52 #define JSON_DUMPER_MAX_DEPTH   1100
53 typedef struct json_dumper {
54     FILE   *output_file;    /**< Output file, must be set. */
55 #define JSON_DUMPER_FLAGS_PRETTY_PRINT  (1 << 0)    /* Enable pretty printing. */
56 #define JSON_DUMPER_DOT_TO_UNDERSCORE   (1 << 1)    /* Convert dots to underscores in keys */
57     int     flags;
58     /* for internal use, initialize with zeroes. */
59     int     current_depth;
60     gint    base64_state;
61     gint    base64_save;
62     guint8  state[JSON_DUMPER_MAX_DEPTH];
63 } json_dumper;
64 
65 WS_DLL_PUBLIC void
66 json_dumper_begin_object(json_dumper *dumper);
67 
68 WS_DLL_PUBLIC void
69 json_dumper_set_member_name(json_dumper *dumper, const char *name);
70 
71 WS_DLL_PUBLIC void
72 json_dumper_end_object(json_dumper *dumper);
73 
74 WS_DLL_PUBLIC void
75 json_dumper_begin_array(json_dumper *dumper);
76 
77 WS_DLL_PUBLIC void
78 json_dumper_end_array(json_dumper *dumper);
79 
80 WS_DLL_PUBLIC void
81 json_dumper_value_string(json_dumper *dumper, const char *value);
82 
83 WS_DLL_PUBLIC void
84 json_dumper_value_double(json_dumper *dumper, double value);
85 
86 /**
87  * Dump number, "true", "false" or "null" values.
88  */
89 WS_DLL_PUBLIC void
90 json_dumper_value_anyf(json_dumper *dumper, const char *format, ...)
91 G_GNUC_PRINTF(2, 3);
92 
93 /**
94  * Dump literal values (like json_dumper_value_anyf), but taking a va_list
95  * as parameter. String values MUST be properly quoted by the caller, no
96  * escaping occurs. Do not use with untrusted data.
97  */
98 WS_DLL_PUBLIC void
99 json_dumper_value_va_list(json_dumper *dumper, const char *format, va_list ap);
100 
101 WS_DLL_PUBLIC void
102 json_dumper_begin_base64(json_dumper *dumper);
103 
104 WS_DLL_PUBLIC void
105 json_dumper_end_base64(json_dumper *dumper);
106 
107 WS_DLL_PUBLIC void
108 json_dumper_write_base64(json_dumper *dumper, const guchar *data, size_t len);
109 
110 /**
111  * Finishes dumping data. Returns TRUE if everything is okay and FALSE if
112  * something went wrong (open/close mismatch, missing values, etc.).
113  */
114 WS_DLL_PUBLIC gboolean
115 json_dumper_finish(json_dumper *dumper);
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif /* __JSON_DUMPER_H__ */
122 
123 /*
124  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
125  *
126  * Local variables:
127  * c-basic-offset: 4
128  * tab-width: 8
129  * indent-tabs-mode: nil
130  * End:
131  *
132  * vi: set shiftwidth=4 tabstop=8 expandtab:
133  * :indentSize=4:tabSize=8:noTabs=true:
134  */
135