OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FsiJsonUtils.cpp
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 #include "FsiJsonUtils.h"
18 
19 #include <fstream>
20 
21 #include <rapidjson/istreamwrapper.h>
22 #include <rapidjson/ostreamwrapper.h>
23 #include <rapidjson/writer.h>
24 
25 namespace foreign_storage {
26 namespace json_utils {
27 
28 // Basic types (more can be added as required)
29 void set_value(rapidjson::Value& json_val,
30  const long unsigned int& value,
31  rapidjson::Document::AllocatorType& allocator) {
32  json_val.SetUint64(value);
33 }
34 void get_value(const rapidjson::Value& json_val, long unsigned int& value) {
35  CHECK(json_val.IsUint64());
36  value = json_val.GetUint64();
37 }
38 
39 void set_value(rapidjson::Value& json_val,
40  const int& value,
41  rapidjson::Document::AllocatorType& allocator) {
42  json_val.SetInt(value);
43 }
44 
45 void get_value(const rapidjson::Value& json_val, int& value) {
46  CHECK(json_val.IsInt());
47  value = json_val.GetInt();
48 }
49 
50 void set_value(rapidjson::Value& json_val,
51  const std::string& value,
52  rapidjson::Document::AllocatorType& allocator) {
53  json_val.SetString(value, allocator);
54 }
55 
56 void get_value(const rapidjson::Value& json_val, std::string& value) {
57  CHECK(json_val.IsString());
58  value = json_val.GetString();
59 }
60 
61 rapidjson::Document read_from_file(const std::string& file_path) {
62  std::ifstream ifs(file_path);
63  if (!ifs) {
64  throw std::runtime_error{"Error trying to open file \"" + file_path +
65  "\". The error was: " + std::strerror(errno)};
66  }
67 
68  rapidjson::IStreamWrapper isw(ifs);
69  rapidjson::Document d;
70  d.ParseStream(isw);
71  return d;
72 }
73 
74 void write_to_file(const rapidjson::Document& document, const std::string& filepath) {
75  std::ofstream ofs(filepath);
76  if (!ofs) {
77  throw std::runtime_error{"Error trying to create file \"" + filepath +
78  "\". The error was: " + std::strerror(errno)};
79  }
80  rapidjson::OStreamWrapper osw(ofs);
81  rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
82  document.Accept(writer);
83 }
84 
85 } // namespace json_utils
86 } // 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)
rapidjson::Document read_from_file(const std::string &file_path)
void write_to_file(const rapidjson::Document &document, const std::string &filepath)
#define CHECK(condition)
Definition: Logger.h:197