| 1 |
/* Copyright (c) 2021, 2026, 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 |
#include "mysql/components/library_mysys/my_hex_tools.h" |
| 25 |
|
| 26 |
#include "json_writer.h" |
| 27 |
|
| 28 |
namespace keyring_common { |
| 29 |
|
| 30 |
namespace json_data { |
| 31 |
|
| 32 |
/** |
| 33 |
Constructor |
| 34 |
|
| 35 |
@param [in] data JSON data |
| 36 |
@param [in] version Version information |
| 37 |
@param [in] version_key Writer version key |
| 38 |
@param [in] array_key Key to array that stores all elements |
| 39 |
*/ |
| 40 |
Json_writer::Json_writer(const std::string data /* = {} */, |
| 41 |
const std::string version /* = "1.0" */, |
| 42 |
const std::string version_key /* = "version" */, |
| 43 |
const std::string array_key /* = "elements" */) |
| 44 |
: document_(), |
| 45 |
version_key_(version_key), |
| 46 |
array_key_(array_key), |
| 47 |
valid_(version.length() > 0 && version_key.length() > 0 && |
| 48 |
array_key.length() > 0) { |
| 49 |
if (valid_ && data.length() == 0) { |
| 50 |
document_.SetObject(); |
| 51 |
rapidjson::Document::AllocatorType &allocator = document_.GetAllocator(); |
| 52 |
rapidjson::Value version_value(rapidjson::kObjectType); |
| 53 |
version_value.SetString(version.c_str(), |
| 54 |
static_cast<rapidjson::SizeType>(version.length()), |
| 55 |
allocator); |
| 56 |
document_.AddMember(rapidjson::StringRef(version_key_.c_str()), |
| 57 |
version_value, allocator); |
| 58 |
rapidjson::Value elements(rapidjson::kArrayType); |
| 59 |
document_.AddMember(rapidjson::StringRef(array_key_.c_str()), elements, |
| 60 |
allocator); |
| 61 |
} else { |
| 62 |
valid_ &= !document_.Parse(data).HasParseError(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
bool Json_writer::set_data(const std::string data) { |
| 67 |
valid_ = !document_.Parse(data).HasParseError(); |
| 68 |
return valid_; |
| 69 |
} |
| 70 |
|
| 71 |
/** Get string representation of the JSON document */ |
| 72 |
const std::string Json_writer::to_string() const { |
| 73 |
if (!valid_) return std::string{}; |
| 74 |
rapidjson::StringBuffer string_buffer; |
| 75 |
string_buffer.Clear(); |
| 76 |
rapidjson::Writer<rapidjson::StringBuffer> string_writer(string_buffer); |
| 77 |
document_.Accept(string_writer); |
| 78 |
return std::string(string_buffer.GetString(), string_buffer.GetSize()); |
| 79 |
} |
| 80 |
|
| 81 |
bool Json_writer::add_element(const meta::Metadata &metadata, |
| 82 |
const data::Data &data, Json_data_extension &) { |
| 83 |
rapidjson::Document::AllocatorType &allocator = document_.GetAllocator(); |
| 84 |
rapidjson::Value array_element(rapidjson::kObjectType); |
| 85 |
rapidjson::Value element_member(rapidjson::kObjectType); |
| 86 |
|
| 87 |
/* Add user */ |
| 88 |
element_member.SetString( |
| 89 |
metadata.owner_id().c_str(), |
| 90 |
static_cast<rapidjson::SizeType>(metadata.owner_id().length()), |
| 91 |
allocator); |
| 92 |
array_element.AddMember("user", element_member, allocator); |
| 93 |
|
| 94 |
/* Add data id */ |
| 95 |
element_member.SetString( |
| 96 |
metadata.key_id().c_str(), |
| 97 |
static_cast<rapidjson::SizeType>(metadata.key_id().length()), allocator); |
| 98 |
array_element.AddMember("data_id", element_member, allocator); |
| 99 |
|
| 100 |
/* Add data type */ |
| 101 |
element_member.SetString( |
| 102 |
data.type().c_str(), |
| 103 |
static_cast<rapidjson::SizeType>(data.type().length()), allocator); |
| 104 |
array_element.AddMember("data_type", element_member, allocator); |
| 105 |
|
| 106 |
/* Add data */ |
| 107 |
std::string hex_data(data.data().size() * 2, '\0'); |
| 108 |
(void)hex_string(&(hex_data[0]), data.data().c_str(), data.data().size()); |
| 109 |
hex_data.shrink_to_fit(); |
| 110 |
element_member.SetString(hex_data.c_str(), |
| 111 |
static_cast<rapidjson::SizeType>(hex_data.size()), |
| 112 |
allocator); |
| 113 |
array_element.AddMember("data", element_member, allocator); |
| 114 |
|
| 115 |
/* Add extension */ |
| 116 |
rapidjson::Value extension_array(rapidjson::kArrayType); |
| 117 |
array_element.AddMember("extension", extension_array, allocator); |
| 118 |
|
| 119 |
/* Now add the newly constructed object to the array */ |
| 120 |
document_[array_key_].PushBack(array_element, allocator); |
| 121 |
|
| 122 |
return document_.HasParseError(); |
| 123 |
} |
| 124 |
|
| 125 |
bool Json_writer::remove_element(const meta::Metadata &metadata, |
| 126 |
const Json_data_extension &) { |
| 127 |
bool retval = true; |
| 128 |
if (!valid_) return retval; |
| 129 |
|
| 130 |
rapidjson::Value &elements = document_[array_key_.c_str()]; |
| 131 |
if (!elements.IsArray()) return retval; |
| 132 |
|
| 133 |
for (rapidjson::Value::ConstValueIterator it = elements.Begin(); |
| 134 |
it != elements.End();) { |
| 135 |
meta::Metadata current_metadata((*it)["data_id"].Get<std::string>(), |
| 136 |
(*it)["user"].Get<std::string>()); |
| 137 |
if (metadata == current_metadata) { |
| 138 |
it = elements.Erase(it); // Erase will move iterator to next position |
| 139 |
retval = false; |
| 140 |
} else |
| 141 |
++it; |
| 142 |
} |
| 143 |
return retval; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
Get number of elements in the document |
| 148 |
|
| 149 |
@returns number elements in the document |
| 150 |
*/ |
| 151 |
size_t Json_writer::num_elements() const { |
| 152 |
if (!valid_) return 0; |
| 153 |
return static_cast<size_t>(document_[array_key_.c_str()].Size()); |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace json_data |
| 157 |
} // namespace keyring_common |