MySQL 9.1.0
Source Code Documentation
json_writer.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef JSON_WRITER_INCLUDED
25#define JSON_WRITER_INCLUDED
26
27#include <string>
28
29#include "my_rapidjson_size_t.h"
30
31#include <rapidjson/document.h>
32#include <rapidjson/stringbuffer.h>
33#include <rapidjson/writer.h>
34
37
38#include "json_ds.h"
39
41
43 public:
44 Json_writer(const std::string &data = {}, const std::string &version = "1.0",
45 const std::string &version_key = "version",
46 const std::string &array_key = "elements");
47
48 /** Destructor */
49 virtual ~Json_writer() = default;
50
51 bool set_data(const std::string &data);
52
53 std::string to_string() const;
54
55 /**
56 Add an element
57
58 @param [in] metadata Data identifier
59 @param [in] data Data
60
61 @returns status of insertion
62 @retval false Success
63 @retval true Failure
64 */
65 virtual bool add_element(const meta::Metadata &metadata,
66 const data::Data &data, Json_data_extension &);
67
68 /**
69 Remove an element
70
71 @param [in] metadata Data identifier
72
73 @returns status of removal
74 @retval false Success
75 @retval true Failure
76 */
77 virtual bool remove_element(const meta::Metadata &metadata,
78 const Json_data_extension &);
79
80 /** Number of elements stored */
81 size_t num_elements() const;
82
83 /** Validity of the document */
84 bool valid() const { return valid_; }
85
86 protected:
87 /**
88 Set a property in the main JSON object. It is intended to be used by derived
89 classes, so it is possible to store some specific, data file-wide
90 properties.
91
92 @param [in] property_key key (name) of the property
93 @param [in] property value of the property
94
95 @returns status
96 @retval false Success
97 @retval true Failure
98 */
99 virtual bool set_property(const std::string &property_key,
100 const std::string &property);
101
102 private:
103 /** Data in JSON DOM format */
104 rapidjson::Document document_;
105 /** Version information */
106 const std::string version_key_;
107 /** Elements array name */
108 const std::string array_key_;
109 /** Document validity */
110 bool valid_;
111};
112
113} // namespace keyring_common::json_data
114
115#endif // !JSON_WRITER_INCLUDED
Sensitive data storage.
Definition: data.h:39
Definition: json_writer.h:42
const std::string version_key_
Version information.
Definition: json_writer.h:106
virtual bool set_property(const std::string &property_key, const std::string &property)
Set a property in the main JSON object.
Definition: json_writer.cc:153
bool valid() const
Validity of the document.
Definition: json_writer.h:84
bool set_data(const std::string &data)
Definition: json_writer.cc:63
virtual ~Json_writer()=default
Destructor.
const std::string array_key_
Elements array name.
Definition: json_writer.h:108
rapidjson::Document document_
Data in JSON DOM format.
Definition: json_writer.h:104
size_t num_elements() const
Number of elements stored.
Definition: json_writer.cc:148
virtual bool add_element(const meta::Metadata &metadata, const data::Data &data, Json_data_extension &)
Add an element.
Definition: json_writer.cc:78
std::string to_string() const
Get string representation of the JSON document.
Definition: json_writer.cc:69
bool valid_
Document validity.
Definition: json_writer.h:110
Json_writer(const std::string &data={}, const std::string &version="1.0", const std::string &version_key="version", const std::string &array_key="elements")
Constructor.
Definition: json_writer.cc:38
virtual bool remove_element(const meta::Metadata &metadata, const Json_data_extension &)
Remove an element.
Definition: json_writer.cc:122
Common metadata.
Definition: meta.h:38
Define rapidjson::SizeType to be std::uint64_t.
Definition: json_ds.h:29
required uint64 version
Definition: replication_group_member_actions.proto:41