23 #include <rapidjson/document.h>
27 namespace foreign_storage {
30 namespace json_utils {
37 const std::string&
name,
38 rapidjson::Document::AllocatorType& allocator);
42 const std::string&
name);
45 const rapidjson::Value&
object,
46 const std::string&
name);
50 void set_value(rapidjson::Value& json_val,
52 rapidjson::Document::AllocatorType& allocator);
53 void get_value(
const rapidjson::Value& json_val,
size_t& value);
55 void set_value(rapidjson::Value& json_val,
57 rapidjson::Document::AllocatorType& allocator);
58 void get_value(
const rapidjson::Value& json_val,
int& value);
60 void set_value(rapidjson::Value& json_val,
61 const std::string& value,
62 rapidjson::Document::AllocatorType& allocator);
63 void get_value(
const rapidjson::Value& json_val, std::string& value);
66 void set_value(rapidjson::Value& json_val,
68 rapidjson::Document::AllocatorType& allocator);
70 void get_value(
const rapidjson::Value& json_val, int64_t& value);
75 const std::vector<T>& vector_value,
76 rapidjson::Document::AllocatorType& allocator) {
78 for (
const auto& value : vector_value) {
79 rapidjson::Value json_obj;
81 json_val.PushBack(json_obj, allocator);
86 void get_value(
const rapidjson::Value& json_val, std::vector<T>& vector_value) {
87 CHECK(json_val.IsArray());
88 CHECK(vector_value.size() == 0);
89 for (
const auto& json_obj : json_val.GetArray()) {
92 vector_value.push_back(val);
97 template <
class T,
class V>
99 const std::vector<std::pair<T, V>>& vector_value,
100 rapidjson::Document::AllocatorType& allocator) {
102 for (
const auto& pair : vector_value) {
103 rapidjson::Value pair_obj;
104 pair_obj.SetObject();
107 json_val.PushBack(pair_obj, allocator);
111 template <
class T,
class V>
113 std::vector<std::pair<T, V>>& vector_value) {
114 CHECK(json_val.IsArray());
115 CHECK(vector_value.size() == 0);
116 for (
const auto& json_obj : json_val.GetArray()) {
117 CHECK(json_obj.IsObject());
122 vector_value.emplace_back(std::make_pair(key, value));
127 template <
class T,
class V>
129 const std::map<T, V>& map_value,
130 rapidjson::Document::AllocatorType& allocator) {
132 for (
const auto& pair : map_value) {
133 rapidjson::Value pair_obj;
134 pair_obj.SetObject();
137 json_val.PushBack(pair_obj, allocator);
141 template <
class T,
class V>
142 void get_value(
const rapidjson::Value& json_val, std::map<T, V>& map_value) {
143 CHECK(json_val.IsArray());
144 CHECK(map_value.size() == 0);
145 for (
const auto& json_obj : json_val.GetArray()) {
146 CHECK(json_obj.IsObject());
151 map_value[key] = value;
159 const std::string&
name,
160 rapidjson::Document::AllocatorType& allocator) {
161 CHECK(
object.IsObject());
162 CHECK(!
object.HasMember(name)) <<
"Found unexpected member: " <<
name;
163 rapidjson::Value json_val;
165 rapidjson::Value json_name;
166 json_name.SetString(name, allocator);
167 object.AddMember(json_name, json_val, allocator);
174 const std::string&
name) {
175 CHECK(
object.IsObject());
176 CHECK(
object.HasMember(name)) <<
"Could not find member: " <<
name;
184 void write_to_file(
const rapidjson::Document& document,
const std::string& file_path);
void set_value(rapidjson::Value &json_val, const size_t &value, rapidjson::Document::AllocatorType &allocator)
std::string get_type_as_string(const rapidjson::Value &object)
void get_value(const rapidjson::Value &json_val, size_t &value)
void get_value_from_object(const rapidjson::Value &object, T &value, const std::string &name)
rapidjson::Document read_from_file(const std::string &file_path)
void add_value_to_object(rapidjson::Value &object, const T &value, const std::string &name, rapidjson::Document::AllocatorType &allocator)
void write_to_file(const rapidjson::Document &document, const std::string &filepath)
std::optional< std::string > get_optional_string_value_from_object(const rapidjson::Value &object, const std::string &key)
std::string write_to_string(const rapidjson::Document &document)