MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
serializer_to_text.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_SERIALIZERTOTEXT_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SERIALIZERTOTEXT_H_
28
29#include <sstream>
30
31#include "my_rapidjson_size_t.h"
32
33#include <rapidjson/document.h>
34#include <rapidjson/ostreamwrapper.h>
35#include <rapidjson/writer.h>
36
38
40#include "helper/optional.h"
41
42namespace helper {
43namespace json {
44
46 public:
47 class Object {
48 public:
49 Object(Object &&other) { *this = std::move(other); }
50
51 Object(SerializerToText *serializer = nullptr) : serializer_{serializer} {
52 initialize();
53 }
54
56
58 bool is_usable() const { return !finalized_; }
59
61 finalize();
62 serializer_ = other.serializer_;
63 finalized_ = other.finalized_;
64 other.finalized_ = true;
65 return *this;
66 }
67
68 void finalize() {
69 if (!serializer_) return;
70 if (!finalized_) serializer_->writer_.EndObject();
71 finalized_ = true;
72 }
73
74 private:
75 void initialize() {
76 if (!serializer_) return;
77 serializer_->writer_.StartObject();
78 finalized_ = false;
79 }
80
82 bool finalized_{true};
83 };
84
85 class Array {
86 public:
87 Array(Array &&other) { *this = std::move(other); }
88 Array(SerializerToText *serializer = nullptr) : serializer_{serializer} {
89 initialize();
90 }
92
95 bool is_usable() const { return finalized_; }
96
97 Array &operator=(Array &&other) {
98 finalize();
99 serializer_ = other.serializer_;
100 finalized_ = other.finalized_;
101 other.finalized_ = true;
102 return *this;
103 }
104
105 template <typename Arr>
106 Array &add(const Arr &arr) {
107 auto it = std::begin(arr);
108 while (it != std::end(arr)) {
109 *serializer_ << *it;
110 ++it;
111 }
112
113 return *this;
114 }
115
116 private:
117 void initialize() {
118 if (!serializer_) return;
119 serializer_->writer_.StartArray();
120 finalized_ = false;
121 }
122
123 void finalize() {
124 if (!serializer_) return;
125 if (!finalized_) serializer_->writer_.EndArray();
126 finalized_ = true;
127 }
128
130 bool finalized_{false};
131 };
132
133 public:
134 explicit SerializerToText(const bool bigint_encode_as_string = false)
135 : bigint_encode_as_string_{bigint_encode_as_string} {}
136
137 std::string get_result() {
138 writer_.Flush();
139 return value_.str();
140 }
141
142 Object add_object() { return Object(this); }
143 Array add_array() { return Array(this); }
144
147 return *this;
148 }
149
150 SerializerToText &operator<<(const std::string &value) {
151 add_value(value.c_str(), value.length(), JsonType::kString);
152 return *this;
153 }
154
156 writer_.Double(value);
157 return *this;
158 }
159
161 writer_.Int(value);
162 return *this;
163 }
164
165 SerializerToText &operator<<(const unsigned int value) {
166 writer_.Uint(value);
167 return *this;
168 }
169
172 writer_.Uint64(value);
173 } else {
174 auto str = std::to_string(value);
175 writer_.String(str.c_str(), str.length());
176 }
177 return *this;
178 }
179
182 writer_.Int64(value);
183 } else {
184 auto str = std::to_string(value);
185 writer_.String(str.c_str(), str.length());
186 }
187 return *this;
188
189 return *this;
190 }
191
193 writer_.Bool(value);
194 return *this;
195 }
196
199 return add_value(value, value ? strlen(value) : 0, ct);
200 }
201
202 SerializerToText &add_value(const char *value, uint32_t length, JsonType ct) {
203 if (!value) {
204 writer_.Null();
205 return *this;
206 }
207
208 switch (ct) {
209 case JsonType::kJson:
210 case JsonType::kBool: {
211 writer_.RawValue(value, length, rapidjson::kObjectType);
212 } break;
213
214 case JsonType::kNull:
215 writer_.Null();
216 break;
217
219 // This functions should take into account `bigint_encode_as_string_`.
220 writer_.RawValue(value, length, rapidjson::kNumberType);
221 break;
222
223 case JsonType::kBlob:
226 writer_.String(value, length);
227 break;
228 }
229
230 return *this;
231 }
232
233 SerializerToText &add_value(const rapidjson::Value &value) {
234 rapidjson::StringBuffer json_buf;
235 {
236 rapidjson::Writer<rapidjson::StringBuffer> json_writer(json_buf);
237
238 value.Accept(json_writer);
239 }
240
241 return add_value(json_buf.GetString(), json_buf.GetLength(),
243 }
244
245 void flush() { writer_.Flush(); }
246
248 writer_.Key(key);
249 return Array(this);
250 }
251
253 writer_.Key(key);
254 return Object(this);
255 }
256
257 template <typename Str1, typename Str2>
258 SerializerToText &member_add_value(const Str1 &key, const Str2 &value,
259 JsonType ct) {
261 return *this;
262 }
263
264 template <typename Str1, typename Value>
266 add_member_impl(get_raw(key), std::forward<Value>(value));
267 return *this;
268 }
269
270 template <typename Str1>
271 SerializerToText &member_add_value(const Str1 &key, const char *str,
272 uint32_t len) {
273 writer_.Key(key);
275 return *this;
276 }
277
278 template <typename Str1>
279 SerializerToText &member_add_value(const Str1 &key, const char *str,
280 uint32_t len, JsonType ct) {
281 writer_.Key(key);
282 add_value(str, len, ct);
283 return *this;
284 }
285
286 template <typename Str1>
288 writer_.Key(key);
289 writer_.Null();
290 return *this;
291 }
292
293 private:
294 const char *get_raw(const char *value) { return value; }
295 const char *get_raw(const std::string &value) { return value.c_str(); }
296 const char *get_raw(const bool value) { return value ? "1" : "0"; }
297
298 template <typename Value>
299 void add_member_impl(const char *key, Value &&value) {
300 writer_.Key(key);
301 *this << (std::forward<Value>(value));
302 }
303
304 template <typename Value>
306 if (value) {
307 writer_.Key(key);
308 *this << (value.value());
309 }
310 }
311
312 template <typename Value>
314 if (value) {
315 writer_.Key(key);
316 *this << (value.value());
317 }
318 }
319
320 void add_member_impl(const char *key, const char *value, JsonType ct) {
321 writer_.Key(key);
322 add_value(value, ct);
323 }
324
326 std::stringstream value_;
327 rapidjson::OStreamWrapper ostream_{value_};
328 rapidjson::Writer<rapidjson::OStreamWrapper> writer_{ostream_};
329};
330
331} // namespace json
332} // namespace helper
333
334#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_SERIALIZERTOTEXT_H_
Lightweight implementation of optional.
Definition: optional.h:39
Definition: serializer_to_text.h:85
SerializerToText * operator->()
Definition: serializer_to_text.h:93
Array & add(const Arr &arr)
Definition: serializer_to_text.h:106
Array(Array &&other)
Definition: serializer_to_text.h:87
void initialize()
Definition: serializer_to_text.h:117
Array & operator=(Array &&other)
Definition: serializer_to_text.h:97
bool finalized_
Definition: serializer_to_text.h:130
void finalize()
Definition: serializer_to_text.h:123
bool is_usable() const
Definition: serializer_to_text.h:95
SerializerToText * serializer_
Definition: serializer_to_text.h:129
Array(SerializerToText *serializer=nullptr)
Definition: serializer_to_text.h:88
SerializerToText & operator*()
Definition: serializer_to_text.h:94
~Array()
Definition: serializer_to_text.h:91
Definition: serializer_to_text.h:47
void finalize()
Definition: serializer_to_text.h:68
bool is_usable() const
Definition: serializer_to_text.h:58
Object(Object &&other)
Definition: serializer_to_text.h:49
~Object()
Definition: serializer_to_text.h:55
SerializerToText * serializer_
Definition: serializer_to_text.h:81
SerializerToText * operator->()
Definition: serializer_to_text.h:57
bool finalized_
Definition: serializer_to_text.h:82
Object & operator=(Object &&other)
Definition: serializer_to_text.h:60
void initialize()
Definition: serializer_to_text.h:75
Object(SerializerToText *serializer=nullptr)
Definition: serializer_to_text.h:51
Definition: serializer_to_text.h:45
void add_member_impl(const char *key, helper::Optional< Value > &value)
Definition: serializer_to_text.h:305
void flush()
Definition: serializer_to_text.h:245
SerializerToText & add_value(const char *value, JsonType ct=JsonType::kString)
Definition: serializer_to_text.h:197
void add_member_impl(const char *key, const helper::Optional< Value > &value)
Definition: serializer_to_text.h:313
SerializerToText & operator<<(const uint64_t value)
Definition: serializer_to_text.h:170
SerializerToText & member_add_value(const Str1 &key, Value &&value)
Definition: serializer_to_text.h:265
SerializerToText & operator<<(const char *value)
Definition: serializer_to_text.h:145
std::string get_result()
Definition: serializer_to_text.h:137
const char * get_raw(const std::string &value)
Definition: serializer_to_text.h:295
void add_member_impl(const char *key, const char *value, JsonType ct)
Definition: serializer_to_text.h:320
SerializerToText & member_add_value(const Str1 &key, const char *str, uint32_t len, JsonType ct)
Definition: serializer_to_text.h:279
SerializerToText & operator<<(const int value)
Definition: serializer_to_text.h:160
const char * get_raw(const char *value)
Definition: serializer_to_text.h:294
SerializerToText & operator<<(const int64_t value)
Definition: serializer_to_text.h:180
Object add_object()
Definition: serializer_to_text.h:142
std::stringstream value_
Definition: serializer_to_text.h:326
SerializerToText(const bool bigint_encode_as_string=false)
Definition: serializer_to_text.h:134
rapidjson::Writer< rapidjson::OStreamWrapper > writer_
Definition: serializer_to_text.h:328
SerializerToText & member_add_null_value(const Str1 &key)
Definition: serializer_to_text.h:287
SerializerToText & member_add_value(const Str1 &key, const Str2 &value, JsonType ct)
Definition: serializer_to_text.h:258
SerializerToText & operator<<(const unsigned int value)
Definition: serializer_to_text.h:165
void add_member_impl(const char *key, Value &&value)
Definition: serializer_to_text.h:299
Array member_add_array(const char *key)
Definition: serializer_to_text.h:247
SerializerToText & operator<<(const float value)
Definition: serializer_to_text.h:155
Array add_array()
Definition: serializer_to_text.h:143
SerializerToText & operator<<(const std::string &value)
Definition: serializer_to_text.h:150
rapidjson::OStreamWrapper ostream_
Definition: serializer_to_text.h:327
bool bigint_encode_as_string_
Definition: serializer_to_text.h:325
Object member_add_object(const char *key)
Definition: serializer_to_text.h:252
SerializerToText & member_add_value(const Str1 &key, const char *str, uint32_t len)
Definition: serializer_to_text.h:271
SerializerToText & add_value(const char *value, uint32_t length, JsonType ct)
Definition: serializer_to_text.h:202
SerializerToText & add_value(const rapidjson::Value &value)
Definition: serializer_to_text.h:233
const char * get_raw(const bool value)
Definition: serializer_to_text.h:296
SerializerToText & operator<<(const bool value)
Definition: serializer_to_text.h:192
static std::string to_string(const LEX_STRING &str)
Definition: lex_string.h:50
Define rapidjson::SizeType to be std::uint64_t.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
MysqlCacheManager::Object Object
Definition: mysql_cache_manager.cc:101
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
Definition: cache.h:33
JsonType
Definition: mysql_column_types.h:35
@ kBool
Definition: mysql_column_types.h:35
@ kJson
Definition: mysql_column_types.h:35
@ kBlob
Definition: mysql_column_types.h:35
@ kNumeric
Definition: mysql_column_types.h:35
@ kObject
Definition: mysql_column_types.h:35
@ kString
Definition: mysql_column_types.h:35
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
const char * begin(const char *const c)
Definition: base64.h:44
@ Array
C++ Object.
Definition: jit_executor_value.h:67
required string key
Definition: replication_asynchronous_connection_failover.proto:60