ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
IJsonConvertibleIO.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "IJsonConvertibleIO.h"
9 
10 // CV_CORE_LIB
11 #include <FileSystem.h>
12 #include <Logging.h>
13 
14 // JSON_LIB
15 #include <json/json.h>
16 
17 // SYSTEM
18 #include <fstream>
19 #include <sstream>
20 #include <unordered_map>
21 
22 namespace cloudViewer {
23 
24 namespace {
25 using namespace io;
26 
27 static const std::unordered_map<
28  std::string,
29  std::function<bool(const std::string &,
31  file_extension_to_ijsonconvertible_read_function{
33  };
34 
35 static const std::unordered_map<
36  std::string,
37  std::function<bool(const std::string &,
39  file_extension_to_ijsonconvertible_write_function{
41  };
42 
43 bool ReadIJsonConvertibleFromJSONStream(
44  std::istream &json_stream,
46  Json::Value root_object;
47  Json::CharReaderBuilder builder;
48  builder["collectComments"] = false;
49  Json::String errs;
50  bool is_parse_successful =
51  parseFromStream(builder, json_stream, &root_object, &errs);
52  if (!is_parse_successful) {
53  utility::LogWarning("Read JSON failed: {}.", errs);
54  return false;
55  }
56  return object.ConvertFromJsonValue(root_object);
57 }
58 
59 bool WriteIJsonConvertibleToJSONStream(
60  std::ostream &json_stream,
62  Json::Value root_object;
63  if (object.ConvertToJsonValue(root_object) == false) {
64  return false;
65  }
66  Json::StreamWriterBuilder builder;
67  builder["commentStyle"] = "None";
68  builder["indentation"] = "\t";
69  auto writer = builder.newStreamWriter();
70  writer->write(root_object, &json_stream);
71  return true;
72 }
73 
74 } // unnamed namespace
75 
76 namespace io {
77 
79  const std::string &filename,
81  std::ifstream file_in(filename);
82  if (file_in.is_open() == false) {
83  utility::LogWarning("Read JSON failed: unable to open file: {}",
84  filename);
85  return false;
86  }
87  bool success = ReadIJsonConvertibleFromJSONStream(file_in, object);
88  file_in.close();
89  return success;
90 }
91 
93  const std::string &filename,
95  std::ofstream file_out(filename);
96  if (file_out.is_open() == false) {
97  utility::LogWarning("Write JSON failed: unable to open file: {}",
98  filename);
99  return false;
100  }
101  bool success = WriteIJsonConvertibleToJSONStream(file_out, object);
102  file_out.close();
103  return success;
104 }
105 
107  const std::string &json_string,
109  std::istringstream iss(json_string);
110  return ReadIJsonConvertibleFromJSONStream(iss, object);
111 }
112 
114  std::string &json_string,
116  std::ostringstream oss;
117  bool success = WriteIJsonConvertibleToJSONStream(oss, object);
118  json_string = oss.str();
119  return success;
120 }
121 
122 bool ReadIJsonConvertible(const std::string &filename,
124  std::string filename_ext =
126  filename);
127  if (filename_ext.empty()) {
129  "Read utility::IJsonConvertible failed: unknown file "
130  "extension.");
131  return false;
132  }
133  auto map_itr =
134  file_extension_to_ijsonconvertible_read_function.find(filename_ext);
135  if (map_itr == file_extension_to_ijsonconvertible_read_function.end()) {
137  "Read utility::IJsonConvertible failed: unknown file "
138  "extension.");
139  return false;
140  }
141  return map_itr->second(filename, object);
142 }
143 
145  const std::string &filename,
147  std::string filename_ext =
149  filename);
150  if (filename_ext.empty()) {
152  "Write utility::IJsonConvertible failed: unknown file "
153  "extension.");
154  return false;
155  }
156  auto map_itr = file_extension_to_ijsonconvertible_write_function.find(
157  filename_ext);
158  if (map_itr == file_extension_to_ijsonconvertible_write_function.end()) {
160  "Write utility::IJsonConvertible failed: unknown file "
161  "extension.");
162  return false;
163  }
164  return map_itr->second(filename, object);
165 }
166 } // namespace io
167 
168 } // namespace cloudViewer
std::string filename
#define LogWarning(...)
Definition: Logging.h:72
bool ReadIJsonConvertibleFromJSONString(const std::string &json_string, cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertible(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertibleToJSONString(std::string &json_string, const cloudViewer::utility::IJsonConvertible &object)
bool WriteIJsonConvertibleToJSON(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool ReadIJsonConvertibleFromJSON(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
bool ReadIJsonConvertible(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:281
Generic file read and write utility for python interface.
static vtkPVTrivialProducerStaticInternal Value