OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FsiJsonUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <map>
20 #include <optional>
21 #include <vector>
22 
23 #include <rapidjson/document.h>
24 
25 #include "Logger/Logger.h"
26 
27 namespace foreign_storage {
28 
29 // helper functions for serializing/deserializing objects to rapidjson value
30 namespace json_utils {
31 std::string get_type_as_string(const rapidjson::Value& object);
32 
33 // Forward declare for vector/map functions
34 template <class T>
35 void add_value_to_object(rapidjson::Value& object,
36  const T& value,
37  const std::string& name,
38  rapidjson::Document::AllocatorType& allocator);
39 template <class T>
40 void get_value_from_object(const rapidjson::Value& object,
41  T& value,
42  const std::string& name);
43 
44 std::optional<std::string> get_optional_string_value_from_object(
45  const rapidjson::Value& object,
46  const std::string& name);
47 
48 // Basic types (more can be added as required) will be defined in source file
49 // int
50 void set_value(rapidjson::Value& json_val,
51  const size_t& value,
52  rapidjson::Document::AllocatorType& allocator);
53 void get_value(const rapidjson::Value& json_val, size_t& value);
54 // unsigned long int / size_t
55 void set_value(rapidjson::Value& json_val,
56  const int& value,
57  rapidjson::Document::AllocatorType& allocator);
58 void get_value(const rapidjson::Value& json_val, int& value);
59 // string
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);
64 
65 // int64
66 void set_value(rapidjson::Value& json_val,
67  const int64_t& value,
68  rapidjson::Document::AllocatorType& allocator);
69 
70 void get_value(const rapidjson::Value& json_val, int64_t& value);
71 
72 // std::vector
73 template <class T>
74 void set_value(rapidjson::Value& json_val,
75  const std::vector<T>& vector_value,
76  rapidjson::Document::AllocatorType& allocator) {
77  json_val.SetArray();
78  for (const auto& value : vector_value) {
79  rapidjson::Value json_obj;
80  set_value(json_obj, value, allocator);
81  json_val.PushBack(json_obj, allocator);
82  }
83 }
84 
85 template <class T>
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()) {
90  T val;
91  get_value(json_obj, val);
92  vector_value.push_back(val);
93  }
94 }
95 
96 // std::vector<std::pair>
97 template <class T, class V>
98 void set_value(rapidjson::Value& json_val,
99  const std::vector<std::pair<T, V>>& vector_value,
100  rapidjson::Document::AllocatorType& allocator) {
101  json_val.SetArray();
102  for (const auto& pair : vector_value) {
103  rapidjson::Value pair_obj;
104  pair_obj.SetObject();
105  add_value_to_object(pair_obj, pair.first, "key", allocator);
106  add_value_to_object(pair_obj, pair.second, "value", allocator);
107  json_val.PushBack(pair_obj, allocator);
108  }
109 }
110 
111 template <class T, class V>
112 void get_value(const rapidjson::Value& json_val,
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());
118  T key;
119  V value;
120  get_value_from_object(json_obj, key, "key");
121  get_value_from_object(json_obj, value, "value");
122  vector_value.emplace_back(std::make_pair(key, value));
123  }
124 }
125 
126 // std::map
127 template <class T, class V>
128 void set_value(rapidjson::Value& json_val,
129  const std::map<T, V>& map_value,
130  rapidjson::Document::AllocatorType& allocator) {
131  json_val.SetArray();
132  for (const auto& pair : map_value) {
133  rapidjson::Value pair_obj;
134  pair_obj.SetObject();
135  add_value_to_object(pair_obj, pair.first, "key", allocator);
136  add_value_to_object(pair_obj, pair.second, "value", allocator);
137  json_val.PushBack(pair_obj, allocator);
138  }
139 }
140 
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());
147  T key;
148  V value;
149  get_value_from_object(json_obj, key, "key");
150  get_value_from_object(json_obj, value, "value");
151  map_value[key] = value;
152  }
153 }
154 
155 // Serialize value into json object with valid set_value functions
156 template <class T>
157 void add_value_to_object(rapidjson::Value& object,
158  const T& 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;
164  set_value(json_val, value, allocator);
165  rapidjson::Value json_name;
166  json_name.SetString(name, allocator);
167  object.AddMember(json_name, json_val, allocator);
168 }
169 
170 // Deserialize value from json object with valid get_value/set_value functions
171 template <class T>
172 void get_value_from_object(const rapidjson::Value& object,
173  T& value,
174  const std::string& name) {
175  CHECK(object.IsObject());
176  CHECK(object.HasMember(name)) << "Could not find member: " << name;
177  get_value(object[name], value);
178 }
179 
180 // Read JSON content from the given file path
181 rapidjson::Document read_from_file(const std::string& file_path);
182 
183 // Write JSON content (encapsulated by the given Document object) to the given file path
184 void write_to_file(const rapidjson::Document& document, const std::string& file_path);
185 
186 std::string write_to_string(const rapidjson::Document& document);
187 
188 } // namespace json_utils
189 } // namespace foreign_storage
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)
Definition: FsiJsonUtils.h:172
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)
Definition: FsiJsonUtils.h:157
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)
#define CHECK(condition)
Definition: Logger.h:289
std::string write_to_string(const rapidjson::Document &document)
string name
Definition: setup.in.py:72