19 #include <rapidjson/document.h>
23 namespace foreign_storage {
26 namespace json_utils {
33 const std::string&
name,
34 rapidjson::Document::AllocatorType& allocator);
38 const std::string&
name);
42 void set_value(rapidjson::Value& json_val,
44 rapidjson::Document::AllocatorType& allocator);
45 void get_value(
const rapidjson::Value& json_val,
size_t& value);
47 void set_value(rapidjson::Value& json_val,
49 rapidjson::Document::AllocatorType& allocator);
50 void get_value(
const rapidjson::Value& json_val,
int& value);
52 void set_value(rapidjson::Value& json_val,
53 const std::string& value,
54 rapidjson::Document::AllocatorType& allocator);
55 void get_value(
const rapidjson::Value& json_val, std::string& value);
58 void set_value(rapidjson::Value& json_val,
60 rapidjson::Document::AllocatorType& allocator);
62 void get_value(
const rapidjson::Value& json_val, int64_t& value);
67 const std::vector<T>& vector_value,
68 rapidjson::Document::AllocatorType& allocator) {
70 for (
const auto& value : vector_value) {
71 rapidjson::Value json_obj;
73 json_val.PushBack(json_obj, allocator);
78 void get_value(
const rapidjson::Value& json_val, std::vector<T>& vector_value) {
79 CHECK(json_val.IsArray());
80 CHECK(vector_value.size() == 0);
81 for (
const auto& json_obj : json_val.GetArray()) {
84 vector_value.push_back(val);
89 template <
class T,
class V>
91 const std::vector<std::pair<T, V>>& vector_value,
92 rapidjson::Document::AllocatorType& allocator) {
94 for (
const auto& pair : vector_value) {
95 rapidjson::Value pair_obj;
99 json_val.PushBack(pair_obj, allocator);
103 template <
class T,
class V>
105 std::vector<std::pair<T, V>>& vector_value) {
106 CHECK(json_val.IsArray());
107 CHECK(vector_value.size() == 0);
108 for (
const auto& json_obj : json_val.GetArray()) {
109 CHECK(json_obj.IsObject());
114 vector_value.emplace_back(std::make_pair(key, value));
119 template <
class T,
class V>
121 const std::map<T, V>& map_value,
122 rapidjson::Document::AllocatorType& allocator) {
124 for (
const auto& pair : map_value) {
125 rapidjson::Value pair_obj;
126 pair_obj.SetObject();
129 json_val.PushBack(pair_obj, allocator);
133 template <
class T,
class V>
134 void get_value(
const rapidjson::Value& json_val, std::map<T, V>& map_value) {
135 CHECK(json_val.IsArray());
136 CHECK(map_value.size() == 0);
137 for (
const auto& json_obj : json_val.GetArray()) {
138 CHECK(json_obj.IsObject());
143 map_value[key] = value;
151 const std::string&
name,
152 rapidjson::Document::AllocatorType& allocator) {
153 CHECK(
object.IsObject());
154 CHECK(!
object.HasMember(name));
155 rapidjson::Value json_val;
157 rapidjson::Value json_name;
158 json_name.SetString(name, allocator);
159 object.AddMember(json_name, json_val, allocator);
166 const std::string&
name) {
167 CHECK(
object.IsObject());
168 CHECK(
object.HasMember(name));
176 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::string write_to_string(const rapidjson::Document &document)