MySQL 9.7.0
Source Code Documentation
text_to.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2026, 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 <optional>
30#include <type_traits>
31
32#include <my_rapidjson_size_t.h>
33#include <rapidjson/document.h>
34#include <rapidjson/memorystream.h>
35#include <rapidjson/reader.h>
36
38
39namespace helper {
40namespace json {
41
42/**
43 * Convert text representation of json object/value to type defined by
44 * 'Handler'.
45 *
46 * The 'Handler' must be a child class of `rapidjson::BaseReaderHandler`. The
47 * child class must contain type/alias definition of returned result, under
48 * "Result" alias and `get_result` method. For example:
49 *
50 * class HandlerSomeExample
51 * : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,HandlerSomeExample>
52 * { public: using Result = std::string;
53 *
54 * ...
55 * };
56 */
57template <typename Handler, typename Container>
58rapidjson::ParseResult parse(Handler *handler, const Container &c) {
59 static_assert(
62 if (c.size() == 0) {
63 return {rapidjson::kParseErrorDocumentEmpty, 0};
64 }
65 rapidjson::MemoryStream memory_stream{
66 reinterpret_cast<const char *>(&*c.begin()), c.size()};
67 rapidjson::Reader read;
68
69 return read.Parse<Handler::k_parse_flags>(memory_stream, *handler);
70}
71
72template <typename Container>
73bool text_to(rapidjson::Document *doc, const Container &c) {
74 static_assert(
77 if (c.size() == 0) {
78 return false;
79 }
80 doc->Parse(reinterpret_cast<const char *>(&*c.begin()), c.size());
81 return !doc->HasParseError();
82}
83
84inline bool text_to(rapidjson::Document *doc, const std::string &str) {
85 doc->Parse(str.c_str());
86 return !doc->HasParseError();
87}
88
89inline bool text_to(rapidjson::Document *doc, const char *str) {
90 doc->Parse(str);
91 return !doc->HasParseError();
92}
93
94inline bool text_to(rapidjson::Document::Object *obj, const std::string &str) {
95 rapidjson::Document doc;
96
97 if (!text_to(&doc, str)) return false;
98 if (!doc.IsObject()) return false;
99
100 *obj = doc.GetObject();
101
102 return true;
103}
104
105inline bool text_to(rapidjson::Value *val, const std::string &str) {
106 rapidjson::Document doc;
107
108 if (!text_to(&doc, str)) return false;
109 if (!doc.IsObject()) return false;
110
111 *val = doc.GetObject();
112
113 return true;
114}
115
116/**
117 * Convert text representation of json object/value to type defined by
118 * 'Handler'.
119 *
120 * The 'Handler' must be a child class of `rapidjson::BaseReaderHandler`. The
121 * child class must contain type/alias definition of returned result, under
122 * "Result" alias and `get_result` method. For example:
123 *
124 * class HandlerSomeExample
125 * : rapidjson::BaseReaderHandler<rapidjson::UTF8<>,HandlerSomeExample>
126 * { public: using Result = std::string;
127 *
128 * ...
129 * };
130 */
131template <typename Handler, typename Container, typename... HandlerArgs>
133text_to_handler(const Container &c, HandlerArgs &&...args) {
134 Handler handler(std::forward<HandlerArgs>(args)...);
135
136 if (const auto parse_result = parse<Handler, Container>(&handler, c);
137 parse_result.IsError())
138 return stdx::unexpected(parse_result.Code());
139
140 return handler.get_result();
141}
142
143inline rapidjson::Document text_to_document(const std::string &str) {
144 rapidjson::Document result;
145 text_to(&result, str);
146 return result;
147}
148
149} // namespace json
150} // namespace helper
151
152#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:4752
Definition: expected.h:286
Define rapidjson::SizeType to be std::uint64_t.
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1077
MysqlCacheManager::Object Object
Definition: mysql_cache_manager.cc:110
stdx::expected< typename Handler::Result, rapidjson::ParseErrorCode > text_to_handler(const Container &c, HandlerArgs &&...args)
Convert text representation of json object/value to type defined by 'Handler'.
Definition: text_to.h:133
rapidjson::ParseResult parse(Handler *handler, const Container &c)
Convert text representation of json object/value to type defined by 'Handler'.
Definition: text_to.h:58
bool text_to(rapidjson::Document *doc, const Container &c)
Definition: text_to.h:73
rapidjson::Document text_to_document(const std::string &str)
Definition: text_to.h:143
Definition: any_of.h:31
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
unexpected(E) -> unexpected< E >
struct result result
Definition: result.h:34
Definition: result.h:30