OmniSciDB  2e3a973ef4
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 
28 // Forward declare for vector/map functions
29 template <class T>
30 void add_value_to_object(rapidjson::Value& object,
31  const T& value,
32  const std::string& name,
33  rapidjson::Document::AllocatorType& allocator);
34 template <class T>
35 void get_value_from_object(const rapidjson::Value& object,
36  T& value,
37  const std::string& name);
38 
39 // Basic types (more can be added as required) will be defined in source file
40 // int
41 void set_value(rapidjson::Value& json_val,
42  const long unsigned int& value,
43  rapidjson::Document::AllocatorType& allocator);
44 void get_value(const rapidjson::Value& json_val, long unsigned int& value);
45 // unsigned long int / size_t
46 void set_value(rapidjson::Value& json_val,
47  const int& value,
48  rapidjson::Document::AllocatorType& allocator);
49 void get_value(const rapidjson::Value& json_val, int& value);
50 // string
51 void set_value(rapidjson::Value& json_val,
52  const std::string& value,
53  rapidjson::Document::AllocatorType& allocator);
54 void get_value(const rapidjson::Value& json_val, std::string& value);
55 
56 // std::vector
57 template <class T>
58 void set_value(rapidjson::Value& json_val,
59  const std::vector<T>& vector_value,
60  rapidjson::Document::AllocatorType& allocator) {
61  json_val.SetArray();
62  for (const auto& value : vector_value) {
63  rapidjson::Value json_obj;
64  set_value(json_obj, value, allocator);
65  json_val.PushBack(json_obj, allocator);
66  }
67 }
68 
69 template <class T>
70 void get_value(const rapidjson::Value& json_val, std::vector<T>& vector_value) {
71  CHECK(json_val.IsArray());
72  CHECK(vector_value.size() == 0);
73  for (const auto& json_obj : json_val.GetArray()) {
74  T val;
75  get_value(json_obj, val);
76  vector_value.push_back(val);
77  }
78 }
79 
80 // std::map
81 template <class T, class V>
82 void set_value(rapidjson::Value& json_val,
83  const std::map<T, V>& map_value,
84  rapidjson::Document::AllocatorType& allocator) {
85  json_val.SetArray();
86  for (const auto& pair : map_value) {
87  rapidjson::Value pair_obj;
88  pair_obj.SetObject();
89  add_value_to_object(pair_obj, pair.first, "key", allocator);
90  add_value_to_object(pair_obj, pair.second, "value", allocator);
91  json_val.PushBack(pair_obj, allocator);
92  }
93 }
94 
95 template <class T, class V>
96 void get_value(const rapidjson::Value& json_val, std::map<T, V>& map_value) {
97  CHECK(json_val.IsArray());
98  CHECK(map_value.size() == 0);
99  for (const auto& json_obj : json_val.GetArray()) {
100  CHECK(json_obj.IsObject());
101  T key;
102  V value;
103  get_value_from_object(json_obj, key, "key");
104  get_value_from_object(json_obj, value, "value");
105  map_value[key] = value;
106  }
107 }
108 
109 // Serialize value into json object with valid set_value functions
110 template <class T>
111 void add_value_to_object(rapidjson::Value& object,
112  const T& value,
113  const std::string& name,
114  rapidjson::Document::AllocatorType& allocator) {
115  CHECK(object.IsObject());
116  CHECK(!object.HasMember(name));
117  rapidjson::Value json_val;
118  set_value(json_val, value, allocator);
119  rapidjson::Value json_name;
120  json_name.SetString(name, allocator);
121  object.AddMember(json_name, json_val, allocator);
122 }
123 
124 // Deserialize value from json object with valid get_value/set_value functions
125 template <class T>
126 void get_value_from_object(const rapidjson::Value& object,
127  T& value,
128  const std::string& name) {
129  CHECK(object.IsObject());
130  CHECK(object.HasMember(name));
131  get_value(object[name], value);
132 }
133 
134 // Read JSON content from the given file path
135 rapidjson::Document read_from_file(const std::string& file_path);
136 
137 // Write JSON content (encapsulated by the given Document object) to the given file path
138 void write_to_file(const rapidjson::Document& document, const std::string& file_path);
139 
140 } // namespace json_utils
141 } // namespace foreign_storage
void set_value(rapidjson::Value &json_val, const long unsigned int &value, rapidjson::Document::AllocatorType &allocator)
void get_value(const rapidjson::Value &json_val, long unsigned int &value)
void get_value_from_object(const rapidjson::Value &object, T &value, const std::string &name)
Definition: FsiJsonUtils.h:126
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:111
void write_to_file(const rapidjson::Document &document, const std::string &filepath)
#define CHECK(condition)
Definition: Logger.h:197