MySQL 9.3.0
Source Code Documentation
utils_json.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, 2025, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef MYSQLSHDK_LIBS_UTILS_UTILS_JSON_H_
27#define MYSQLSHDK_LIBS_UTILS_UTILS_JSON_H_
28
29#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
30#include "my_rapidjson_size_t.h"
31#endif
32
33#include <rapidjson/rapidjson.h>
34
35#include <rapidjson/document.h>
36
37#include <cstdint>
38#include <memory>
39#include <optional>
40#include <string>
41#include <string_view>
42
43// #include "mysqlshdk/include/mysqlshdk_export.h"
44// #include "mysqlshdk/include/scripting/types.h"
46
47namespace shcore {
48
49class /*SHCORE_PUBLIC*/ JSON_dumper final {
50 public:
51 class Writer_base;
52
53 explicit JSON_dumper(bool pprint = false, size_t binary_limit = 0);
55
56 void start_array();
57 void end_array();
58 void start_object();
59 void end_object();
60
61 void append_value(const Value &value);
62 void append_value(std::string_view key, const Value &value);
63 void append(const Value &value);
64 void append(std::string_view key, const Value &value);
65
66 void append(const Dictionary_t &value);
67 void append(std::string_view key, const Dictionary_t &value);
68
69 void append(const Array_t &value);
70 void append(std::string_view key, const Array_t &value);
71
72 void append_null() const;
73 void append_null(std::string_view key) const;
74
75 void append_bool(bool data) const;
76 void append_bool(std::string_view key, bool data) const;
77 void append(bool data) const;
78 void append(std::string_view key, bool data) const;
79
80 void append_int(int data) const;
81 void append_int(std::string_view key, int data) const;
82 void append(int data) const;
83 void append(std::string_view key, int data) const;
84
85 void append_uint(unsigned int data) const;
86 void append_uint(std::string_view key, unsigned int data) const;
87 void append(unsigned int data) const;
88 void append(std::string_view key, unsigned int data) const;
89
90 void append_int64(int64_t data) const;
91 void append_int64(std::string_view key, int64_t data) const;
92
93 void append_uint64(uint64_t data) const;
94 void append_uint64(std::string_view key, uint64_t data) const;
95
96 void append(long int data) const;
97 void append(std::string_view key, long int data) const;
98
99 void append(unsigned long int data) const;
100 void append(std::string_view key, unsigned long int data) const;
101
102 void append(long long int data) const;
103 void append(std::string_view key, long long int data) const;
104
105 void append(unsigned long long int data) const;
106 void append(std::string_view key, unsigned long long int data) const;
107
108 void append_string(std::string_view data) const;
109 void append_string(std::string_view key, std::string_view data) const;
110 void append(std::string_view data) const;
111 inline void append(const char *data) const { append_string(data); }
112 void append(std::string_view key, std::string_view data) const;
113 inline void append(std::string_view key, const char *data) const {
114 append_string(key, data);
115 }
116
117 void append_float(double data) const;
118 void append_float(std::string_view key, double data) const;
119 void append(double data) const;
120 void append(std::string_view key, double data) const;
121
122 void append_json(const std::string &data) const;
123
124 int deep_level() const { return _deep_level; }
125
126 const std::string &str() const;
127
128 private:
130 size_t _binary_limit{0};
131
132 std::unique_ptr<Writer_base> _writer;
133};
134
135namespace json {
136
137using JSON = rapidjson::Document;
138
139/**
140 * Parses a JSON string.
141 *
142 * NOTE: this does not throw exceptions, parsed document can be potentially
143 * invalid.
144 *
145 * @param json JSON to be parsed.
146 *
147 * @returns parsed document
148 */
149JSON parse(std::string_view json);
150
151/**
152 * Parses a JSON string, input is expected to be a JSON object.
153 *
154 * @param json JSON to be parsed.
155 *
156 * @returns parsed document
157 *
158 * @throws std::runtime_error if parsing fails, or result is not a JSON object.
159 */
160JSON parse_object_or_throw(std::string_view json);
161
162/**
163 * Fetches a string value of a required field.
164 *
165 * @param json JSON object.
166 * @param name Name of the field.
167 * @param allow_empty Don't throw if value is an empty string.
168 *
169 * @returns string value
170 *
171 * @throws std::runtime_error if field does not exist or its value is not a
172 * string
173 */
174std::string required(const JSON &json, const char *name,
175 bool allow_empty = false);
176
177/**
178 * Fetches an unsigned integer value of a required field.
179 *
180 * @param json JSON object.
181 * @param name Name of the field.
182 *
183 * @returns unsigned integer value
184 *
185 * @throws std::runtime_error if field does not exist or its value is not an
186 * unsigned integer
187 */
188uint64_t required_uint(const JSON &json, const char *name);
189
190/**
191 * Fetches a string value of an optional field.
192 *
193 * @param json JSON object.
194 * @param name Name of the field.
195 * @param allow_empty Don't throw if value is an empty string.
196 *
197 * @returns string value if JSON contains the specified field
198 *
199 * @throws std::runtime_error if value is not a string
200 */
201std::optional<std::string> optional(const JSON &json, const char *name,
202 bool allow_empty = false);
203
204/**
205 * Fetches an unsigned integer value of an optional field.
206 *
207 * @param json JSON object.
208 * @param name Name of the field.
209 *
210 * @returns unsigned integer value if JSON contains the specified field
211 *
212 * @throws std::runtime_error if value is not an unsigned integer
213 */
214std::optional<uint64_t> optional_uint(const JSON &json, const char *name);
215
216/**
217 * Converts a JSON object to a string.
218 *
219 * @param json A JSON object
220 *
221 * @returns String representation of a JSON object.
222 */
223std::string to_string(const JSON &json);
224
225/**
226 * Converts a JSON object to a nicely formatted string.
227 *
228 * @param json A JSON object
229 *
230 * @returns String representation of a JSON object.
231 */
232std::string to_pretty_string(const JSON &json);
233
234} // namespace json
235
236} // namespace shcore
237
238#endif // MYSQLSHDK_LIBS_UTILS_UTILS_JSON_H_
Definition: utils_json.cc:106
Definition: utils_json.h:49
void start_array()
Definition: utils_json.cc:189
void append(const Value &value)
Definition: utils_json.cc:305
int deep_level() const
Definition: utils_json.h:124
int _deep_level
Definition: utils_json.h:129
void append_uint(unsigned int data) const
Definition: utils_json.cc:358
void append(std::string_view key, const char *data) const
Definition: utils_json.h:113
void append_value(const Value &value)
Definition: utils_json.cc:206
JSON_dumper(bool pprint=false, size_t binary_limit=0)
Definition: utils_json.cc:179
void append(const char *data) const
Definition: utils_json.h:111
std::unique_ptr< Writer_base > _writer
Definition: utils_json.h:132
void append_json(const std::string &data) const
Definition: utils_json.cc:469
void start_object()
Definition: utils_json.cc:197
void end_object()
Definition: utils_json.cc:201
void append_bool(bool data) const
Definition: utils_json.cc:332
void append_int(int data) const
Definition: utils_json.cc:345
void append_float(double data) const
Definition: utils_json.cc:454
void append_uint64(uint64_t data) const
Definition: utils_json.cc:382
size_t _binary_limit
Definition: utils_json.h:130
void append_int64(int64_t data) const
Definition: utils_json.cc:373
void append_string(std::string_view data) const
Definition: utils_json.cc:438
const std::string & str() const
Definition: utils_json.cc:475
void append_null() const
Definition: utils_json.cc:325
void end_array()
Definition: utils_json.cc:193
Define rapidjson::SizeType to be std::uint64_t.
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
uint64_t required_uint(const JSON &json, const char *name)
Fetches an unsigned integer value of a required field.
Definition: utils_json.cc:528
std::string to_string(const JSON &json)
Converts a JSON object to a string.
Definition: utils_json.cc:567
std::optional< std::string > optional(const JSON &json, const char *name, bool allow_empty)
Fetches a string value of an optional field.
Definition: utils_json.cc:538
std::optional< uint64_t > optional_uint(const JSON &json, const char *name)
Fetches an unsigned integer value of an optional field.
Definition: utils_json.cc:553
JSON parse_object_or_throw(std::string_view json)
Parses a JSON string, input is expected to be a JSON object.
Definition: utils_json.cc:502
std::string to_pretty_string(const JSON &json)
Converts a JSON object to a nicely formatted string.
Definition: utils_json.cc:574
std::string required(const JSON &json, const char *name, bool allow_empty)
Fetches a string value of a required field.
Definition: utils_json.cc:517
JSON parse(std::string_view json)
Parses a JSON string.
Definition: utils_json.cc:494
rapidjson::Document JSON
Definition: utils_json.h:137
Definition: file_system_exceptions.h:34
Value::Array_type_ref Array_t
Definition: jit_executor_value.h:431
Value::Map_type_ref Dictionary_t
Definition: jit_executor_value.h:430
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Pointer to a function that may be implemented in any language.
Definition: jit_executor_value.h:130