OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #include <rapidjson/stringbuffer.h>
26 
27 namespace foreign_storage {
28 namespace json_utils {
29 
30 std::string get_type_as_string(const rapidjson::Value& object) {
31  if (object.IsArray()) {
32  return "array";
33  } else if (object.IsBool()) {
34  return "bool";
35  } else if (object.IsDouble()) {
36  return "double";
37  } else if (object.IsFloat()) {
38  return "float";
39  } else if (object.IsInt64()) {
40  return "int64";
41  } else if (object.IsInt()) {
42  return "int";
43  } else if (object.IsNull()) {
44  return "null";
45  } else if (object.IsNumber()) {
46  return "number";
47  } else if (object.IsObject()) {
48  return "onject";
49  } else if (object.IsString()) {
50  return "string";
51  } else if (object.IsUint64()) {
52  return "unit64";
53  } else if (object.IsUint()) {
54  return "unit";
55  }
56  return "unknown";
57 }
58 
59 // Basic types (more can be added as required)
60 void set_value(rapidjson::Value& json_val,
61  const size_t& value,
62  rapidjson::Document::AllocatorType& allocator) {
63  json_val.SetUint64(value);
64 }
65 void get_value(const rapidjson::Value& json_val, size_t& value) {
66  CHECK(json_val.IsUint64());
67  value = json_val.GetUint64();
68 }
69 
70 void set_value(rapidjson::Value& json_val,
71  const int& value,
72  rapidjson::Document::AllocatorType& allocator) {
73  json_val.SetInt(value);
74 }
75 
76 void get_value(const rapidjson::Value& json_val, int& value) {
77  CHECK(json_val.IsInt());
78  value = json_val.GetInt();
79 }
80 
81 void set_value(rapidjson::Value& json_val,
82  const std::string& value,
83  rapidjson::Document::AllocatorType& allocator) {
84  json_val.SetString(value, allocator);
85 }
86 
87 // int64_t
88 void get_value(const rapidjson::Value& json_val, std::string& value) {
89  CHECK(json_val.IsString());
90  value = json_val.GetString();
91 }
92 
93 void set_value(rapidjson::Value& json_val,
94  const int64_t& value,
95  rapidjson::Document::AllocatorType& allocator) {
96  json_val.SetInt64(value);
97 }
98 
99 void get_value(const rapidjson::Value& json_val, int64_t& value) {
100  CHECK(json_val.IsInt64());
101  value = json_val.GetInt64();
102 }
103 
104 rapidjson::Document read_from_file(const std::string& file_path) {
105  std::ifstream ifs(file_path);
106  if (!ifs) {
107  throw std::runtime_error{"Error trying to open file \"" + file_path +
108  "\". The error was: " + std::strerror(errno)};
109  }
110 
111  rapidjson::IStreamWrapper isw(ifs);
112  rapidjson::Document d;
113  d.ParseStream(isw);
114  return d;
115 }
116 
117 void write_to_file(const rapidjson::Document& document, const std::string& filepath) {
118  std::ofstream ofs(filepath);
119  if (!ofs) {
120  throw std::runtime_error{"Error trying to create file \"" + filepath +
121  "\". The error was: " + std::strerror(errno)};
122  }
123  rapidjson::OStreamWrapper osw(ofs);
124  rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
125  document.Accept(writer);
126 }
127 
128 std::string write_to_string(const rapidjson::Document& document) {
129  rapidjson::StringBuffer buffer;
130  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
131  document.Accept(writer);
132  return buffer.GetString();
133 }
134 } // namespace json_utils
135 } // 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)
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:209
std::string write_to_string(const rapidjson::Document &document)