2 * Routines for serializing data as JSON.
4 * Copyright 2018, Peter Wu <peter@lekensteyn.nl>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #ifndef __JSON_DUMPER_H__
14 #define __JSON_DUMPER_H__
16 #include "ws_symbol_export.h"
31 * json_dumper dumper = {
32 * .output_file = stdout, // or .output_string = g_string_new(NULL)
33 * .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT,
35 * json_dumper_begin_object(&dumper);
36 * json_dumper_set_member_name(&dumper, "key");
37 * json_dumper_value_string(&dumper, "value");
38 * json_dumper_set_member_name(&dumper, "array");
39 * json_dumper_begin_array(&dumper);
40 * json_dumper_value_anyf(&dumper, "true");
41 * json_dumper_value_double(&dumper, 1.0);
42 * json_dumper_begin_base64(&dumper);
43 * json_dumper_write_base64(&dumper, (const unsigned char *)"abcd", 4);
44 * json_dumper_write_base64(&dumper, (const unsigned char *)"1234", 4);
45 * json_dumper_end_base64(&dumper);
46 * json_dumper_begin_object(&dumper);
47 * json_dumper_end_object(&dumper);
48 * json_dumper_begin_array(&dumper);
49 * json_dumper_end_array(&dumper);
50 * json_dumper_end_array(&dumper);
51 * json_dumper_end_object(&dumper);
52 * json_dumper_finish(&dumper);
55 /** Maximum object/array nesting depth. */
56 #define JSON_DUMPER_MAX_DEPTH 1100
57 typedef struct json_dumper
{
58 FILE *output_file
; /**< Output file. If it is not NULL, JSON will be dumped in the file. */
59 GString
*output_string
; /**< Output GLib strings. If it is not NULL, JSON will be dumped in the string. */
60 #define JSON_DUMPER_FLAGS_PRETTY_PRINT (1 << 0) /* Enable pretty printing. */
61 #define JSON_DUMPER_DOT_TO_UNDERSCORE (1 << 1) /* Convert dots to underscores in keys */
62 #define JSON_DUMPER_FLAGS_NO_DEBUG (1 << 17) /* Disable fatal ws_error messages on error(intended for speeding up fuzzing). */
64 /* for internal use, initialize with zeroes. */
65 unsigned current_depth
;
68 uint8_t state
[JSON_DUMPER_MAX_DEPTH
];
72 json_dumper_begin_object(json_dumper
*dumper
);
75 json_dumper_set_member_name(json_dumper
*dumper
, const char *name
);
78 json_dumper_end_object(json_dumper
*dumper
);
81 json_dumper_begin_array(json_dumper
*dumper
);
84 json_dumper_end_array(json_dumper
*dumper
);
87 json_dumper_value_string(json_dumper
*dumper
, const char *value
);
90 json_dumper_value_double(json_dumper
*dumper
, double value
);
93 * Dump number, "true", "false" or "null" values.
96 json_dumper_value_anyf(json_dumper
*dumper
, const char *format
, ...)
100 * Dump literal values (like json_dumper_value_anyf), but taking a va_list
101 * as parameter. String values MUST be properly quoted by the caller, no
102 * escaping occurs. Do not use with untrusted data.
105 json_dumper_value_va_list(json_dumper
*dumper
, const char *format
, va_list ap
);
108 json_dumper_begin_base64(json_dumper
*dumper
);
111 json_dumper_end_base64(json_dumper
*dumper
);
114 json_dumper_write_base64(json_dumper
*dumper
, const unsigned char *data
, size_t len
);
117 * Finishes dumping data. Returns true if everything is okay and false if
118 * something went wrong (open/close mismatch, missing values, etc.).
121 json_dumper_finish(json_dumper
*dumper
);
127 #endif /* __JSON_DUMPER_H__ */
130 * Editor modelines - https://www.wireshark.org/tools/modelines.html
135 * indent-tabs-mode: nil
138 * vi: set shiftwidth=4 tabstop=8 expandtab:
139 * :indentSize=4:tabSize=8:noTabs=true: