MySQL 9.3.0
Source Code Documentation
rapid_json_to_map.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 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,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License 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
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_RAPID_JSON_TO_MAP_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_RAPID_JSON_TO_MAP_H_
28
29#include <list>
30#include <string>
31
32#include <my_rapidjson_size_t.h>
33#include <rapidjson/reader.h>
34
35namespace helper {
36namespace json {
37
38/**
39 * This class is a adapter for Reader from RapidJson.
40 *
41 * This class adapts std::map of strings (keys and values are strings)
42 * to be destination of text document conversion done be `rapidjson::Reader`.
43 * There are some constrains to what is converted:
44 * * values from top level document are inserted into the map,
45 * * value must be a simple type, sub objects, or arrays are ignored.
46 */
48 : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,
49 RapidReaderHandlerToMapOfSimpleValues> {
50 public:
51 using Map = std::map<std::string, std::string>;
52 using Result = Map;
53 constexpr static rapidjson::ParseFlag k_parse_flags{
54 rapidjson::kParseNumbersAsStringsFlag};
55
57 : allowed_levels_{allowed_levels} {}
58
59 const Map &get_result() const { return result_; }
60
61 public: // template overwrites methods from `rapidjson::BaseReaderHandler`
62 bool Null() { return String("null", 4, false); }
63
64 bool Bool(bool value) {
65 if (value) return String("true", 4, false);
66
67 return String("false", 5, false);
68 }
69
70 bool String(const Ch *ch, rapidjson::SizeType size, bool) {
71 if (level_ < 1 || level_ > allowed_levels_ || arrays_ > 0) return true;
72
73 result_[get_current_key()] = std::string(ch, size);
74
75 return static_cast<Override &>(*this).Default();
76 }
77
78 // Ignoring following methods because, parser should be configured to call
79 // "RawNumber" method.
80 bool Int(int v) {
81 auto r = std::to_string(v);
82 return String(r.c_str(), r.length(), false);
83 }
84
85 bool Uint(unsigned v) {
86 auto r = std::to_string(v);
87 return String(r.c_str(), r.length(), false);
88 }
89
90 bool Int64(int64_t v) {
91 auto r = std::to_string(v);
92 return String(r.c_str(), r.length(), false);
93 }
94
95 bool Uint64(uint64_t v) {
96 auto r = std::to_string(v);
97 return String(r.c_str(), r.length(), false);
98 }
99
100 bool Double(double v) {
101 auto r = std::to_string(v);
102 return String(r.c_str(), r.length(), false);
103 }
104
105 /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use
106 /// length)
107 bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy) {
108 return String(str, len, copy);
109 }
110
111 bool StartObject() {
112 if (!key_.name.empty()) {
113 keys_.push_back(key_);
114 }
115 ++level_;
116 return true;
117 }
118
120 --level_;
121 if (!keys_.empty()) {
122 auto &b = keys_.back();
123 if (level_ == b.level) keys_.pop_back();
124 }
125
126 return true;
127 }
128
129 bool Key(const Ch *str, rapidjson::SizeType len, bool) {
130 key_.name.assign(str, len);
131 key_.level = level_;
132 return true;
133 }
134
135 // Ignore arrays
136 bool StartArray() {
137 ++level_;
138 ++arrays_;
139 return true;
140 }
141
143 --level_;
144 --arrays_;
145 if (!keys_.empty()) {
146 auto &b = keys_.back();
147 if (level_ == b.level) keys_.pop_back();
148 }
149 return true;
150 }
151
152 private:
153 std::string get_current_key() const {
154 std::string result;
155 for (const auto &key : keys_) {
156 result += key.name + ".";
157 }
158 return result + key_.name;
159 }
161 struct KeyValue {
162 std::string name;
163 int level;
164 };
165 std::list<KeyValue> keys_;
168 int level_{0};
169 int arrays_{0};
170};
171
172template <typename SubHandler>
174 : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,
175 ExtractSubObjectHandler<SubHandler>> {
176 using Base =
177 rapidjson::BaseReaderHandler<rapidjson::UTF8<>,
179 using Ch = typename Base::Ch;
180
181 public:
182 constexpr static rapidjson::ParseFlag k_parse_flags{
183 rapidjson::kParseNumbersAsStringsFlag};
184
185 using Result = typename SubHandler::Result;
186
187 ExtractSubObjectHandler(const std::string &key, SubHandler &sub_handler)
188 : key_{key}, sub_handler_{sub_handler} {}
189
190 bool Null() {
191 if (targer_) return sub_handler_.Null();
192
193 return true;
194 }
195
196 bool Bool(bool value) {
197 if (targer_) return sub_handler_.Bool(value);
198
199 return true;
200 }
201
202 bool String(const Ch *ch, rapidjson::SizeType size, bool b) {
203 if (targer_) return sub_handler_.String(ch, size, b);
204
205 return true;
206 }
207
208 bool Int(int v) {
209 if (targer_) return sub_handler_.Int(v);
210
211 return true;
212 }
213
214 bool Uint(unsigned v) {
215 if (targer_) return sub_handler_.Uint(v);
216
217 return true;
218 }
219
220 bool Int64(int64_t v) {
221 if (targer_) return sub_handler_.Int64(v);
222
223 return true;
224 }
225
226 bool Uint64(uint64_t v) {
227 if (targer_) return sub_handler_.Uint64(v);
228
229 return true;
230 }
231
232 bool Double(double v) {
233 if (targer_) return sub_handler_.Double(v);
234
235 return true;
236 }
237
238 /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use
239 /// length)
240 bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy) {
241 if (targer_) return sub_handler_.RawNumber(str, len, copy);
242
243 return true;
244 }
245
246 bool StartObject() {
247 if (targer_) sub_handler_.StartObject();
248 ++level_;
249 return true;
250 }
251
253 if (targer_) sub_handler_.EndObject(size);
254 --level_;
255 return true;
256 }
257
258 bool Key(const Ch *str, rapidjson::SizeType len, bool b) {
259 if (targer_) sub_handler_.Key(str, len, b);
260 if (level_ != 1) return true;
261 targer_ = key_ == std::string(str, len);
262 return true;
263 }
264
265 // Ignore arrays
266 bool StartArray() {
267 if (targer_) sub_handler_.StartArray();
268 ++level_;
269 return true;
270 }
271
273 if (targer_) sub_handler_.EndArray(size);
274 --level_;
275 return true;
276 }
277
278 Result get_result() { return sub_handler_.get_result(); }
279
280 private:
281 std::string key_;
282 SubHandler &sub_handler_;
283 int level_{0};
284 bool targer_{false};
285};
286
287} // namespace json
288} // namespace helper
289
290#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_RAPID_JSON_TO_MAP_H_
Definition: rapid_json_to_map.h:175
bool targer_
Definition: rapid_json_to_map.h:284
ExtractSubObjectHandler(const std::string &key, SubHandler &sub_handler)
Definition: rapid_json_to_map.h:187
bool Int64(int64_t v)
Definition: rapid_json_to_map.h:220
SubHandler & sub_handler_
Definition: rapid_json_to_map.h:282
bool Int(int v)
Definition: rapid_json_to_map.h:208
std::string key_
Definition: rapid_json_to_map.h:281
int level_
Definition: rapid_json_to_map.h:283
bool Null()
Definition: rapid_json_to_map.h:190
bool EndObject(rapidjson::SizeType size)
Definition: rapid_json_to_map.h:252
bool Uint64(uint64_t v)
Definition: rapid_json_to_map.h:226
Result get_result()
Definition: rapid_json_to_map.h:278
bool StartArray()
Definition: rapid_json_to_map.h:266
typename SubHandler::Result Result
Definition: rapid_json_to_map.h:185
rapidjson::BaseReaderHandler< rapidjson::UTF8<>, ExtractSubObjectHandler< SubHandler > > Base
Definition: rapid_json_to_map.h:178
bool StartObject()
Definition: rapid_json_to_map.h:246
bool String(const Ch *ch, rapidjson::SizeType size, bool b)
Definition: rapid_json_to_map.h:202
bool Bool(bool value)
Definition: rapid_json_to_map.h:196
bool EndArray(rapidjson::SizeType size)
Definition: rapid_json_to_map.h:272
bool Key(const Ch *str, rapidjson::SizeType len, bool b)
Definition: rapid_json_to_map.h:258
constexpr static rapidjson::ParseFlag k_parse_flags
Definition: rapid_json_to_map.h:182
bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length)
Definition: rapid_json_to_map.h:240
bool Uint(unsigned v)
Definition: rapid_json_to_map.h:214
typename Base::Ch Ch
Definition: rapid_json_to_map.h:179
bool Double(double v)
Definition: rapid_json_to_map.h:232
This class is a adapter for Reader from RapidJson.
Definition: rapid_json_to_map.h:49
int allowed_levels_
Definition: rapid_json_to_map.h:160
bool Double(double v)
Definition: rapid_json_to_map.h:100
bool StartObject()
Definition: rapid_json_to_map.h:111
bool Bool(bool value)
Definition: rapid_json_to_map.h:64
bool EndObject(rapidjson::SizeType)
Definition: rapid_json_to_map.h:119
bool Uint64(uint64_t v)
Definition: rapid_json_to_map.h:95
int arrays_
Definition: rapid_json_to_map.h:169
bool Uint(unsigned v)
Definition: rapid_json_to_map.h:85
bool Int(int v)
Definition: rapid_json_to_map.h:80
bool Int64(int64_t v)
Definition: rapid_json_to_map.h:90
bool EndArray(rapidjson::SizeType)
Definition: rapid_json_to_map.h:142
constexpr static rapidjson::ParseFlag k_parse_flags
Definition: rapid_json_to_map.h:53
std::string get_current_key() const
Definition: rapid_json_to_map.h:153
Map result_
Definition: rapid_json_to_map.h:167
bool Null()
Definition: rapid_json_to_map.h:62
int level_
Definition: rapid_json_to_map.h:168
KeyValue key_
Definition: rapid_json_to_map.h:166
bool String(const Ch *ch, rapidjson::SizeType size, bool)
Definition: rapid_json_to_map.h:70
RapidReaderHandlerToMapOfSimpleValues(int allowed_levels=1)
Definition: rapid_json_to_map.h:56
Map Result
Definition: rapid_json_to_map.h:52
std::list< KeyValue > keys_
Definition: rapid_json_to_map.h:165
std::map< std::string, std::string > Map
Definition: rapid_json_to_map.h:51
const Map & get_result() const
Definition: rapid_json_to_map.h:59
bool RawNumber(const Ch *str, rapidjson::SizeType len, bool copy)
enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length)
Definition: rapid_json_to_map.h:107
bool StartArray()
Definition: rapid_json_to_map.h:136
bool Key(const Ch *str, rapidjson::SizeType len, bool)
Definition: rapid_json_to_map.h:129
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
Define rapidjson::SizeType to be std::uint64_t.
void copy(Shards< COUNT > &dst, const Shards< COUNT > &src) noexcept
Copy the counters, overwrite destination.
Definition: ut0counter.h:354
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
Definition: cache.h:33
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
size_t size(const char *const c)
Definition: base64.h:46
typedef::std::uint64_t SizeType
Definition: my_rapidjson_size_t.h:39
Result
Definition: result.h:34
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
struct result result
Definition: result.h:34
required string key
Definition: replication_asynchronous_connection_failover.proto:60
std::string name
Definition: rapid_json_to_map.h:162
Definition: result.h:30