OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 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 #include "JsonUtils.h"
18 
19 #include <fstream>
20 
21 #include <rapidjson/istreamwrapper.h>
22 #include <rapidjson/ostreamwrapper.h>
23 #include <rapidjson/writer.h>
24 
25 #include <rapidjson/stringbuffer.h>
26 
27 namespace json_utils {
28 
29 std::string get_type_as_string(const rapidjson::Value& object) {
30  if (object.IsArray()) {
31  return "array";
32  } else if (object.IsBool()) {
33  return "bool";
34  } else if (object.IsDouble()) {
35  return "double";
36  } else if (object.IsFloat()) {
37  return "float";
38  } else if (object.IsInt64()) {
39  return "int64";
40  } else if (object.IsInt()) {
41  return "int";
42  } else if (object.IsNull()) {
43  return "null";
44  } else if (object.IsNumber()) {
45  return "number";
46  } else if (object.IsObject()) {
47  return "object";
48  } else if (object.IsString()) {
49  return "string";
50  } else if (object.IsUint64()) {
51  return "uint64";
52  } else if (object.IsUint()) {
53  return "uint";
54  }
55  return "unknown";
56 }
57 
58 // Basic types (more can be added as required)
59 void set_value(rapidjson::Value& json_val,
60  const size_t& value,
61  rapidjson::Document::AllocatorType& allocator) {
62  json_val.SetUint64(value);
63 }
64 void get_value(const rapidjson::Value& json_val, size_t& value) {
65  CHECK(json_val.IsUint64());
66  value = json_val.GetUint64();
67 }
68 
69 void set_value(rapidjson::Value& json_val,
70  const int& value,
71  rapidjson::Document::AllocatorType& allocator) {
72  json_val.SetInt(value);
73 }
74 
75 void get_value(const rapidjson::Value& json_val, int& value) {
76  CHECK(json_val.IsInt());
77  value = json_val.GetInt();
78 }
79 
80 void set_value(rapidjson::Value& json_val,
81  const std::string& value,
82  rapidjson::Document::AllocatorType& allocator) {
83  json_val.SetString(value, allocator);
84 }
85 
86 // int64_t
87 void get_value(const rapidjson::Value& json_val, std::string& value) {
88  CHECK(json_val.IsString());
89  value = json_val.GetString();
90 }
91 
92 void set_value(rapidjson::Value& json_val,
93  const int64_t& value,
94  rapidjson::Document::AllocatorType& allocator) {
95  json_val.SetInt64(value);
96 }
97 
98 void get_value(const rapidjson::Value& json_val, int64_t& value) {
99  CHECK(json_val.IsInt64());
100  value = json_val.GetInt64();
101 }
102 
103 // Boolean
104 void set_value(rapidjson::Value& json_val,
105  const bool& value,
106  rapidjson::Document::AllocatorType& allocator) {
107  json_val.SetBool(value);
108 }
109 
110 void get_value(const rapidjson::Value& json_val, bool& value) {
111  CHECK(json_val.IsBool());
112  value = json_val.GetBool();
113 }
114 
115 // SQLTypes
116 void set_value(rapidjson::Value& json_val,
117  const SQLTypes& value,
118  rapidjson::Document::AllocatorType& allocator) {
119  json_val.SetInt64(static_cast<int64_t>(value));
120 }
121 
122 void get_value(const rapidjson::Value& json_val, SQLTypes& value) {
123  CHECK(json_val.IsInt64());
124  value = static_cast<SQLTypes>(json_val.GetInt64());
125 }
126 
127 // EncodingType
128 void set_value(rapidjson::Value& json_val,
129  const EncodingType& value,
130  rapidjson::Document::AllocatorType& allocator) {
131  json_val.SetInt64(static_cast<int64_t>(value));
132 }
133 
134 void get_value(const rapidjson::Value& json_val, EncodingType& value) {
135  CHECK(json_val.IsInt64());
136  value = static_cast<EncodingType>(json_val.GetInt64());
137 }
138 
139 // StringDictKey
140 void set_value(rapidjson::Value& json_obj,
141  const shared::StringDictKey& dict_key,
142  rapidjson::Document::AllocatorType& allocator) {
143  json_obj.SetObject();
144  add_value_to_object(json_obj, dict_key.db_id, "db_id", allocator);
145  add_value_to_object(json_obj, dict_key.dict_id, "dict_id", allocator);
146 }
147 
148 void get_value(const rapidjson::Value& json_obj, shared::StringDictKey& dict_key) {
149  CHECK(json_obj.IsObject());
150  get_value_from_object(json_obj, dict_key.db_id, "db_id");
151  get_value_from_object(json_obj, dict_key.dict_id, "dict_id");
152 }
153 
154 // SQLTypeInfo
155 void set_value(rapidjson::Value& json_val,
156  const SQLTypeInfo& type_info,
157  rapidjson::Document::AllocatorType& allocator) {
158  json_val.SetObject();
159  add_value_to_object(json_val, type_info.get_type(), "type", allocator);
160  add_value_to_object(json_val, type_info.get_subtype(), "sub_type", allocator);
161  add_value_to_object(json_val, type_info.get_dimension(), "dimension", allocator);
162  add_value_to_object(json_val, type_info.get_scale(), "scale", allocator);
163  add_value_to_object(json_val, type_info.get_notnull(), "notnull", allocator);
164  add_value_to_object(json_val, type_info.get_compression(), "compression", allocator);
165  add_value_to_object(json_val, type_info.get_comp_param(), "comp_param", allocator);
166  add_value_to_object(json_val, type_info.get_size(), "size", allocator);
167  add_value_to_object(json_val,
168  // Serialize StringDictKey without any checks, since the string
169  // dictionary ID may not be set at this point.
171  "string_dict_key",
172  allocator);
173 }
174 
175 void get_value(const rapidjson::Value& json_val, SQLTypeInfo& type_info) {
176  CHECK(json_val.IsObject());
177  get_value_from_object<SQLTypes>(
178  json_val, std::mem_fn(&SQLTypeInfo::set_type), type_info, "type");
179  get_value_from_object<SQLTypes>(
180  json_val, std::mem_fn(&SQLTypeInfo::set_subtype), type_info, "sub_type");
181  get_value_from_object<int>(
182  json_val, std::mem_fn(&SQLTypeInfo::set_dimension), type_info, "dimension");
183  get_value_from_object<int>(
184  json_val, std::mem_fn(&SQLTypeInfo::set_scale), type_info, "scale");
185  get_value_from_object<bool>(
186  json_val, std::mem_fn(&SQLTypeInfo::set_notnull), type_info, "notnull");
187  get_value_from_object<EncodingType>(
188  json_val, std::mem_fn(&SQLTypeInfo::set_compression), type_info, "compression");
189  get_value_from_object<int>(
190  json_val, std::mem_fn(&SQLTypeInfo::set_comp_param), type_info, "comp_param");
191  get_value_from_object<int>(
192  json_val, std::mem_fn(&SQLTypeInfo::set_size), type_info, "size");
193  get_value_from_object<shared::StringDictKey>(
194  json_val,
195  // Deserialize StringDictKey without any checks.
197  type_info,
198  "string_dict_key");
199 }
200 
201 rapidjson::Document read_from_file(const std::string& file_path) {
202  std::ifstream ifs(file_path);
203  if (!ifs) {
204  throw std::runtime_error{"Error trying to open file \"" + file_path +
205  "\". The error was: " + std::strerror(errno)};
206  }
207 
208  rapidjson::IStreamWrapper isw(ifs);
209  rapidjson::Document d;
210  d.ParseStream(isw);
211  return d;
212 }
213 
214 void write_to_file(const rapidjson::Document& document, const std::string& filepath) {
215  std::ofstream ofs(filepath);
216  if (!ofs) {
217  throw std::runtime_error{"Error trying to create file \"" + filepath +
218  "\". The error was: " + std::strerror(errno)};
219  }
220  rapidjson::OStreamWrapper osw(ofs);
221  rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
222  document.Accept(writer);
223 }
224 
225 std::string write_to_string(const rapidjson::Document& document) {
226  rapidjson::StringBuffer buffer;
227  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
228  document.Accept(writer);
229  return buffer.GetString();
230 }
231 
232 std::optional<std::string> get_optional_string_value_from_object(
233  const rapidjson::Value& object,
234  const std::string& key) {
235  if (object.IsObject() && object.HasMember(key) && object[key].IsString()) {
236  return object[key].GetString();
237  }
238  return {};
239 }
240 } // namespace json_utils
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:392
void set_compression(EncodingType c)
Definition: sqltypes.h:481
void set_size(int s)
Definition: sqltypes.h:478
HOST DEVICE int get_size() const
Definition: sqltypes.h:403
SQLTypes
Definition: sqltypes.h:65
void set_value(rapidjson::Value &json_val, const ColumnDescriptor &column_desc, rapidjson::Document::AllocatorType &allocator)
const shared::StringDictKey & getStringDictKeySkipCompParamCheck() const
Definition: sqltypes.h:1070
void write_to_file(const rapidjson::Document &document, const std::string &filepath)
Definition: JsonUtils.cpp:214
HOST DEVICE int get_scale() const
Definition: sqltypes.h:396
void get_value_from_object(const rapidjson::Value &object, T &value, const std::string &name)
Definition: JsonUtils.h:270
HOST DEVICE void set_subtype(SQLTypes st)
Definition: sqltypes.h:471
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
void setStringDictKeySkipCompParamCheck(const shared::StringDictKey &dict_key)
Definition: sqltypes.h:1074
rapidjson::Document read_from_file(const std::string &file_path)
Definition: JsonUtils.cpp:201
EncodingType
Definition: sqltypes.h:240
void set_scale(int s)
Definition: sqltypes.h:475
std::optional< std::string > get_optional_string_value_from_object(const rapidjson::Value &object, const std::string &key)
Definition: JsonUtils.cpp:232
std::string write_to_string(const rapidjson::Document &document)
Definition: JsonUtils.cpp:225
void set_comp_param(int p)
Definition: sqltypes.h:482
std::string get_type_as_string(const rapidjson::Value &object)
Definition: JsonUtils.cpp:29
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:399
void get_value(const rapidjson::Value &json_val, ColumnDescriptor &column_desc)
void set_dimension(int d)
Definition: sqltypes.h:472
void add_value_to_object(rapidjson::Value &object, const T &value, const std::string &name, rapidjson::Document::AllocatorType &allocator)
Definition: JsonUtils.h:255
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:393
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:402
void set_notnull(bool n)
Definition: sqltypes.h:477
#define CHECK(condition)
Definition: Logger.h:291
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
HOST DEVICE void set_type(SQLTypes t)
Definition: sqltypes.h:470