OmniSciDB  471d68cefb
 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 2020 OmniSci, 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 <rapidjson/document.h>
20 #include <map>
21 #include <vector>
22 #include "Logger/Logger.h"
23 namespace foreign_storage {
24 
25 // helper functions for serializing/deserializing objects to rapidjson value
26 namespace json_utils {
27 std::string get_type_as_string(const rapidjson::Value& object);
28 
29 // Forward declare for vector/map functions
30 template <class T>
31 void add_value_to_object(rapidjson::Value& object,
32  const T& value,
33  const std::string& name,
34  rapidjson::Document::AllocatorType& allocator);
35 template <class T>
36 void get_value_from_object(const rapidjson::Value& object,
37  T& value,
38  const std::string& name);
39 
40 // Basic types (more can be added as required) will be defined in source file
41 // int
42 void set_value(rapidjson::Value& json_val,
43  const size_t& value,
44  rapidjson::Document::AllocatorType& allocator);
45 void get_value(const rapidjson::Value& json_val, size_t& value);
46 // unsigned long int / size_t
47 void set_value(rapidjson::Value& json_val,
48  const int& value,
49  rapidjson::Document::AllocatorType& allocator);
50 void get_value(const rapidjson::Value& json_val, int& value);
51 // string
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);
56 
57 // int64
58 void set_value(rapidjson::Value& json_val,
59  const int64_t& value,
60  rapidjson::Document::AllocatorType& allocator);
61 
62 void get_value(const rapidjson::Value& json_val, int64_t& value);
63 
64 // std::vector
65 template <class T>
66 void set_value(rapidjson::Value& json_val,
67  const std::vector<T>& vector_value,
68  rapidjson::Document::AllocatorType& allocator) {
69  json_val.SetArray();
70  for (const auto& value : vector_value) {
71  rapidjson::Value json_obj;
72  set_value(json_obj, value, allocator);
73  json_val.PushBack(json_obj, allocator);
74  }
75 }
76 
77 template <class T>
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()) {
82  T val;
83  get_value(json_obj, val);
84  vector_value.push_back(val);
85  }
86 }
87 
88 // std::vector<std::pair>
89 template <class T, class V>
90 void set_value(rapidjson::Value& json_val,
91  const std::vector<std::pair<T, V>>& vector_value,
92  rapidjson::Document::AllocatorType& allocator) {
93  json_val.SetArray();
94  for (const auto& pair : vector_value) {
95  rapidjson::Value pair_obj;
96  pair_obj.SetObject();
97  add_value_to_object(pair_obj, pair.first, "key", allocator);
98  add_value_to_object(pair_obj, pair.second, "value", allocator);
99  json_val.PushBack(pair_obj, allocator);
100  }
101 }
102 
103 template <class T, class V>
104 void get_value(const rapidjson::Value& json_val,
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());
110  T key;
111  V value;
112  get_value_from_object(json_obj, key, "key");
113  get_value_from_object(json_obj, value, "value");
114  vector_value.emplace_back(std::make_pair(key, value));
115  }
116 }
117 
118 // std::map
119 template <class T, class V>
120 void set_value(rapidjson::Value& json_val,
121  const std::map<T, V>& map_value,
122  rapidjson::Document::AllocatorType& allocator) {
123  json_val.SetArray();
124  for (const auto& pair : map_value) {
125  rapidjson::Value pair_obj;
126  pair_obj.SetObject();
127  add_value_to_object(pair_obj, pair.first, "key", allocator);
128  add_value_to_object(pair_obj, pair.second, "value", allocator);
129  json_val.PushBack(pair_obj, allocator);
130  }
131 }
132 
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());
139  T key;
140  V value;
141  get_value_from_object(json_obj, key, "key");
142  get_value_from_object(json_obj, value, "value");
143  map_value[key] = value;
144  }
145 }
146 
147 // Serialize value into json object with valid set_value functions
148 template <class T>
149 void add_value_to_object(rapidjson::Value& object,
150  const T& 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;
156  set_value(json_val, value, allocator);
157  rapidjson::Value json_name;
158  json_name.SetString(name, allocator);
159  object.AddMember(json_name, json_val, allocator);
160 }
161 
162 // Deserialize value from json object with valid get_value/set_value functions
163 template <class T>
164 void get_value_from_object(const rapidjson::Value& object,
165  T& value,
166  const std::string& name) {
167  CHECK(object.IsObject());
168  CHECK(object.HasMember(name));
169  get_value(object[name], value);
170 }
171 
172 // Read JSON content from the given file path
173 rapidjson::Document read_from_file(const std::string& file_path);
174 
175 // Write JSON content (encapsulated by the given Document object) to the given file path
176 void write_to_file(const rapidjson::Document& document, const std::string& file_path);
177 
178 std::string write_to_string(const rapidjson::Document& document);
179 
180 } // namespace json_utils
181 } // 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)
string name
Definition: setup.in.py:72
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:164
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:149
void write_to_file(const rapidjson::Document &document, const std::string &filepath)
#define CHECK(condition)
Definition: Logger.h:209
std::string write_to_string(const rapidjson::Document &document)