MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
text_to.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_TEXT_TO_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_TEXT_TO_H_
28
29#include <type_traits>
30
31#include <my_rapidjson_size_t.h>
32#include <rapidjson/document.h>
33#include <rapidjson/memorystream.h>
34#include <rapidjson/reader.h>
35
36namespace helper {
37namespace json {
38
39/**
40 * Convert text representation of json object/value to type defined by
41 * 'Handler'.
42 *
43 * The 'Handler' must be a child class of `rapidjson::BaseReaderHandler`. The
44 * child class must contain type/alias definition of returned result, under
45 * "Result" alias and `get_result` method. For example:
46 *
47 * class HandlerSomeExample
48 * : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,HandlerSomeExample>
49 * { public: using Result = std::string;
50 *
51 * ...
52 * };
53 */
54template <typename Handler, typename Container>
55bool text_to(Handler *handler, const Container &c) {
56 static_assert(
59 if (c.size() == 0) {
60 return false;
61 }
62 rapidjson::MemoryStream memory_stream{
63 reinterpret_cast<const char *>(&*c.begin()), c.size()};
64 rapidjson::Reader read;
65
66 return !read.Parse<Handler::k_parse_flags>(memory_stream, *handler).IsError();
67}
68
69template <typename Container>
70bool text_to(rapidjson::Document *doc, const Container &c) {
71 static_assert(
74 if (c.size() == 0) {
75 return false;
76 }
77 doc->Parse(reinterpret_cast<const char *>(&*c.begin()), c.size());
78 return !doc->HasParseError();
79}
80
81inline bool text_to(rapidjson::Document *doc, const std::string &str) {
82 doc->Parse(str.c_str());
83 return !doc->HasParseError();
84}
85
86inline bool text_to(rapidjson::Document *doc, const char *str) {
87 doc->Parse(str);
88 return !doc->HasParseError();
89}
90
91inline bool text_to(rapidjson::Document::Object *obj, const std::string &str) {
92 rapidjson::Document doc;
93
94 if (!text_to(&doc, str)) return false;
95 if (!doc.IsObject()) return false;
96
97 *obj = doc.GetObject();
98
99 return true;
100}
101
102inline bool text_to(rapidjson::Value *val, const std::string &str) {
103 rapidjson::Document doc;
104
105 if (!text_to(&doc, str)) return false;
106 if (!doc.IsObject()) return false;
107
108 *val = doc.GetObject();
109
110 return true;
111}
112
113/**
114 * Convert text representation of json object/value to type defined by
115 * 'Handler'.
116 *
117 * The 'Handler' must be a child class of `rapidjson::BaseReaderHandler`. The
118 * child class must contain type/alias definition of returned result, under
119 * "Result" alias and `get_result` method. For example:
120 *
121 * class HandlerSomeExample
122 * : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,HandlerSomeExample>
123 * { public: using Result = std::string;
124 *
125 * ...
126 * };
127 */
128template <typename Handler, typename Container, typename... HandlerArgs>
129typename Handler::Result text_to_handler(const Container &c,
130 HandlerArgs &&...args) {
131 Handler handler(std::forward<HandlerArgs>(args)...);
132
133 text_to<Handler, Container>(&handler, c);
134
135 return handler.get_result();
136}
137
138inline rapidjson::Document text_to_document(const std::string &str) {
139 rapidjson::Document result;
140 text_to(&result, str);
141 return result;
142}
143
144} // namespace json
145} // namespace helper
146
147#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_JSON_TEXT_TO_H_
The handler class is the interface for dynamically loadable storage engines.
Definition: handler.h:4618
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
Handler::Result text_to_handler(const Container &c, HandlerArgs &&...args)
Convert text representation of json object/value to type defined by 'Handler'.
Definition: text_to.h:129
bool text_to(Handler *handler, const Container &c)
Convert text representation of json object/value to type defined by 'Handler'.
Definition: text_to.h:55
rapidjson::Document text_to_document(const std::string &str)
Definition: text_to.h:138
Definition: cache.h:33
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
Result
Definition: result.h:34
struct result result
Definition: result.h:34
Definition: result.h:30