| 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_READER_INCLUDED |
| 25 |
#define JSON_READER_INCLUDED |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "my_rapidjson_size_t.h" |
| 32 |
|
| 33 |
#include <rapidjson/document.h> |
| 34 |
#include <rapidjson/schema.h> |
| 35 |
|
| 36 |
#include "components/keyrings/common/data/data.h" |
| 37 |
#include "components/keyrings/common/data/meta.h" |
| 38 |
|
| 39 |
#include "json_ds.h" |
| 40 |
|
| 41 |
namespace keyring_common { |
| 42 |
namespace json_data { |
| 43 |
|
| 44 |
using output_vector = |
| 45 |
std::vector<std::pair<std::pair<meta::Metadata, data::Data>, |
| 46 |
std::unique_ptr<Json_data_extension>>>; |
| 47 |
|
| 48 |
/** |
| 49 |
Base Json_reader |
| 50 |
|
| 51 |
Expected format for version 1.0 |
| 52 |
{ |
| 53 |
"version": "1.0", |
| 54 |
"elements": [ |
| 55 |
{ |
| 56 |
"user": "<user_name>", |
| 57 |
"data_id": "<name>", |
| 58 |
"data_type": "<data_type>", |
| 59 |
"data": "<hex_of_data>", |
| 60 |
"extension": [ |
| 61 |
] |
| 62 |
}, |
| 63 |
... |
| 64 |
... |
| 65 |
] |
| 66 |
} |
| 67 |
*/ |
| 68 |
|
| 69 |
class Json_reader { |
| 70 |
public: |
| 71 |
Json_reader(const std::string schema, const std::string data, |
| 72 |
const std::string version_key = "version", |
| 73 |
const std::string array_key = "elements"); |
| 74 |
|
| 75 |
Json_reader(const std::string data); |
| 76 |
|
| 77 |
Json_reader(); |
| 78 |
|
| 79 |
/** Destructor */ |
| 80 |
virtual ~Json_reader() = default; |
| 81 |
|
| 82 |
std::string version() const; |
| 83 |
|
| 84 |
/** |
| 85 |
Get number of elements in the document |
| 86 |
|
| 87 |
@returns number elements in the document |
| 88 |
*/ |
| 89 |
size_t num_elements() const; |
| 90 |
|
| 91 |
/** |
| 92 |
Fetch element from given position |
| 93 |
|
| 94 |
@param [in] index Element position |
| 95 |
@param [out] metadata Data identifier |
| 96 |
@param [out] data Data |
| 97 |
@param [out] json_data_extension Backend specific extension |
| 98 |
|
| 99 |
@return status of operation |
| 100 |
@retval false Success |
| 101 |
@retval true Failure |
| 102 |
*/ |
| 103 |
virtual bool get_element( |
| 104 |
size_t index, meta::Metadata &metadata, data::Data &data, |
| 105 |
std::unique_ptr<Json_data_extension> &json_data_extension) const; |
| 106 |
|
| 107 |
/** |
| 108 |
Get all elements |
| 109 |
|
| 110 |
@param [out] output Output vector |
| 111 |
|
| 112 |
@returns status of extracting elements |
| 113 |
@retval false Success |
| 114 |
@retval true Failure |
| 115 |
*/ |
| 116 |
virtual bool get_elements(output_vector &output) const; |
| 117 |
|
| 118 |
bool valid() const { return valid_; } |
| 119 |
|
| 120 |
private: |
| 121 |
/** Data in JSON DOM format */ |
| 122 |
rapidjson::Document document_; |
| 123 |
/** Version key */ |
| 124 |
const std::string version_key_; |
| 125 |
/** user specific elements array key */ |
| 126 |
const std::string array_key_; |
| 127 |
/** Validity of the data */ |
| 128 |
bool valid_; |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace json_data |
| 132 |
|
| 133 |
} // namespace keyring_common |
| 134 |
|
| 135 |
#endif // !JSON_READER_INCLUDED |