MySQL 9.1.0
Source Code Documentation
json_reader.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_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
38
39#include "json_ds.h"
40
42
44 std::vector<std::pair<std::pair<meta::Metadata, data::Data>,
45 std::unique_ptr<Json_data_extension>>>;
46
47/**
48 Base Json_reader
49
50 Expected format for version 1.0
51 {
52 "version": "1.0",
53 "elements": [
54 {
55 "user": "<user_name>",
56 "data_id": "<name>",
57 "data_type": "<data_type>",
58 "data": "<hex_of_data>",
59 "extension": [
60 ]
61 },
62 ...
63 ...
64 ]
65 }
66*/
67
69 public:
70 Json_reader(const std::string &schema, const std::string &data,
71 std::string version_key = "version",
72 std::string array_key = "elements");
73
74 Json_reader(const std::string &data);
75
77
78 /** Destructor */
79 virtual ~Json_reader() = default;
80
81 /**
82 Get version info
83
84 @returns version string in case same is present, empty string otherwise.
85 */
86 std::string version() const { return property(version_key_); }
87
88 /**
89 Get number of elements in the document
90
91 @returns number elements in the document
92 */
93 size_t num_elements() const;
94
95 /**
96 Fetch element from given position
97
98 @param [in] index Element position
99 @param [out] metadata Data identifier
100 @param [out] data Data
101 @param [out] json_data_extension Backend specific extension
102
103 @return status of operation
104 @retval false Success
105 @retval true Failure
106 */
107 virtual bool get_element(
108 size_t index, meta::Metadata &metadata, data::Data &data,
109 std::unique_ptr<Json_data_extension> &json_data_extension) const;
110
111 /**
112 Get all elements
113
114 @param [out] output Output vector
115
116 @returns status of extracting elements
117 @retval false Success
118 @retval true Failure
119 */
120 virtual bool get_elements(output_vector &output) const;
121
122 bool valid() const { return valid_; }
123
124 /**
125 Get property from the main JSON object. It is intended to be used for some
126 specific, data file-wide properties.
127
128 @returns value of the property
129 */
130 std::string property(const std::string property_key) const;
131
132 private:
133 /** Data in JSON DOM format */
134 rapidjson::Document document_;
135 /** Version key */
136 const std::string version_key_;
137 /** user specific elements array key */
138 const std::string array_key_;
139 /** Validity of the data */
140 bool valid_;
141};
142
143} // namespace keyring_common::json_data
144
145#endif // !JSON_READER_INCLUDED
Sensitive data storage.
Definition: data.h:39
Base Json_reader.
Definition: json_reader.h:68
virtual ~Json_reader()=default
Destructor.
std::string version() const
Get version info.
Definition: json_reader.h:86
bool valid() const
Definition: json_reader.h:122
virtual bool get_element(size_t index, meta::Metadata &metadata, data::Data &data, std::unique_ptr< Json_data_extension > &json_data_extension) const
Fetch element from given position.
Definition: json_reader.cc:132
virtual bool get_elements(output_vector &output) const
Get all elements.
Definition: json_reader.cc:150
size_t num_elements() const
Get number of elements in the document.
Definition: json_reader.cc:127
rapidjson::Document document_
Data in JSON DOM format.
Definition: json_reader.h:134
const std::string version_key_
Version key.
Definition: json_reader.h:136
Json_reader()
Definition: json_reader.cc:107
const std::string array_key_
user specific elements array key
Definition: json_reader.h:138
bool valid_
Validity of the data.
Definition: json_reader.h:140
std::string property(const std::string property_key) const
Get property from the main JSON object.
Definition: json_reader.cc:117
Common metadata.
Definition: meta.h:38
Define rapidjson::SizeType to be std::uint64_t.
Definition: json_ds.h:29
std::vector< std::pair< std::pair< meta::Metadata, data::Data >, std::unique_ptr< Json_data_extension > > > output_vector
Definition: json_reader.h:45