| 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 |
#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 |
|
| 35 |
#include "components/keyrings/common/data/data.h" |
| 36 |
#include "components/keyrings/common/data/meta.h" |
| 37 |
|
| 38 |
#include "json_ds.h" |
| 39 |
|
| 40 |
namespace keyring_common { |
| 41 |
namespace json_data { |
| 42 |
|
| 43 |
class Json_writer { |
| 44 |
public: |
| 45 |
Json_writer(const std::string data = {}, const std::string version = "1.0", |
| 46 |
const std::string version_key = "version", |
| 47 |
const std::string array_key = "elements"); |
| 48 |
|
| 49 |
/** Destructor */ |
| 50 |
virtual ~Json_writer() = default; |
| 51 |
|
| 52 |
bool set_data(const std::string data); |
| 53 |
|
| 54 |
const std::string to_string() const; |
| 55 |
|
| 56 |
/** |
| 57 |
Add an element |
| 58 |
|
| 59 |
@param [in] metadata Data identifier |
| 60 |
@param [in] data Data |
| 61 |
|
| 62 |
@returns status of insertion |
| 63 |
@retval false Success |
| 64 |
@retval true Failure |
| 65 |
*/ |
| 66 |
virtual bool add_element(const meta::Metadata &metadata, |
| 67 |
const data::Data &data, Json_data_extension &); |
| 68 |
|
| 69 |
/** |
| 70 |
Remove an element |
| 71 |
|
| 72 |
@param [in] metadata Data identifier |
| 73 |
|
| 74 |
@returns status of removal |
| 75 |
@retval false Success |
| 76 |
@retval true Failure |
| 77 |
*/ |
| 78 |
virtual bool remove_element(const meta::Metadata &metadata, |
| 79 |
const Json_data_extension &); |
| 80 |
|
| 81 |
/** Number of elements stored */ |
| 82 |
size_t num_elements() const; |
| 83 |
|
| 84 |
/** Validity of the document */ |
| 85 |
bool valid() const { return valid_; } |
| 86 |
|
| 87 |
private: |
| 88 |
/** Data in JSON DOM format */ |
| 89 |
rapidjson::Document document_; |
| 90 |
/** Version information */ |
| 91 |
const std::string version_key_; |
| 92 |
/** Elements array name */ |
| 93 |
const std::string array_key_; |
| 94 |
/** Document validity */ |
| 95 |
bool valid_; |
| 96 |
}; |
| 97 |
|
| 98 |
} // namespace json_data |
| 99 |
} // namespace keyring_common |
| 100 |
|
| 101 |
#endif // !JSON_WRITER_INCLUDED |