21 #include <rapidjson/istreamwrapper.h>
22 #include <rapidjson/ostreamwrapper.h>
23 #include <rapidjson/writer.h>
25 #include <rapidjson/stringbuffer.h>
27 namespace foreign_storage {
28 namespace json_utils {
31 if (
object.IsArray()) {
33 }
else if (
object.IsBool()) {
35 }
else if (
object.IsDouble()) {
37 }
else if (
object.IsFloat()) {
39 }
else if (
object.IsInt64()) {
41 }
else if (
object.IsInt()) {
43 }
else if (
object.IsNull()) {
45 }
else if (
object.IsNumber()) {
47 }
else if (
object.IsObject()) {
49 }
else if (
object.IsString()) {
51 }
else if (
object.IsUint64()) {
53 }
else if (
object.IsUint()) {
62 rapidjson::Document::AllocatorType& allocator) {
63 json_val.SetUint64(value);
65 void get_value(
const rapidjson::Value& json_val,
size_t& value) {
66 CHECK(json_val.IsUint64());
67 value = json_val.GetUint64();
72 rapidjson::Document::AllocatorType& allocator) {
73 json_val.SetInt(value);
76 void get_value(
const rapidjson::Value& json_val,
int& value) {
77 CHECK(json_val.IsInt());
78 value = json_val.GetInt();
82 const std::string& value,
83 rapidjson::Document::AllocatorType& allocator) {
84 json_val.SetString(value, allocator);
88 void get_value(
const rapidjson::Value& json_val, std::string& value) {
89 CHECK(json_val.IsString());
90 value = json_val.GetString();
95 rapidjson::Document::AllocatorType& allocator) {
96 json_val.SetInt64(value);
99 void get_value(
const rapidjson::Value& json_val, int64_t& value) {
100 CHECK(json_val.IsInt64());
101 value = json_val.GetInt64();
105 std::ifstream ifs(file_path);
107 throw std::runtime_error{
"Error trying to open file \"" + file_path +
108 "\". The error was: " + std::strerror(errno)};
111 rapidjson::IStreamWrapper isw(ifs);
112 rapidjson::Document d;
117 void write_to_file(
const rapidjson::Document& document,
const std::string& filepath) {
118 std::ofstream ofs(filepath);
120 throw std::runtime_error{
"Error trying to create file \"" + filepath +
121 "\". The error was: " + std::strerror(errno)};
123 rapidjson::OStreamWrapper osw(ofs);
124 rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
125 document.Accept(writer);
129 rapidjson::StringBuffer buffer;
130 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
131 document.Accept(writer);
132 return buffer.GetString();
136 const rapidjson::Value&
object,
137 const std::string& key) {
138 if (
object.IsObject() &&
object.HasMember(key) &&
object[key].IsString()) {
139 return object[key].GetString();
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)
std::optional< std::string > get_optional_string_value_from_object(const rapidjson::Value &object, const std::string &key)
std::string write_to_string(const rapidjson::Document &document)