ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
IJsonConvertible.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 // LOCAL
9 #include "IJsonConvertible.h"
10 
11 #include "Logging.h"
12 
13 // JSON_LIB
14 #include <json/json.h>
15 
16 #include <string>
17 
18 namespace cloudViewer {
19 namespace utility {
20 
21 Json::Value StringToJson(const std::string &json_str) {
22  Json::Value json;
23  std::string err;
24  Json::CharReaderBuilder builder;
25  const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
26  if (!reader->parse(json_str.c_str(), json_str.c_str() + json_str.length(),
27  &json, &err)) {
28  utility::LogError("Failed to parse string to json, error: {}", err);
29  }
30  return json;
31 }
32 
33 std::string JsonToString(const Json::Value &json) {
34  return Json::writeString(Json::StreamWriterBuilder(), json);
35 }
36 
37 std::string IJsonConvertible::ToString() const {
38  Json::Value val;
39  ConvertToJsonValue(val);
40  return val.toStyledString();
41 }
42 
44  const Json::Value &value) {
45  if (value.size() != 3) {
46  return false;
47  } else {
48  vec(0) = value[0].asDouble();
49  vec(1) = value[1].asDouble();
50  vec(2) = value[2].asDouble();
51  return true;
52  }
53 }
54 
55 bool IJsonConvertible::EigenVector3dToJsonArray(const Eigen::Vector3d &vec,
56  Json::Value &value) {
57  value.clear();
58  value.append(vec(0));
59  value.append(vec(1));
60  value.append(vec(2));
61  return true;
62 }
63 
65  const Json::Value &value) {
66  if (value.size() != 4) {
67  return false;
68  } else {
69  vec(0) = value[0].asDouble();
70  vec(1) = value[1].asDouble();
71  vec(2) = value[2].asDouble();
72  vec(3) = value[3].asDouble();
73  return true;
74  }
75 }
76 
77 bool IJsonConvertible::EigenVector4dToJsonArray(const Eigen::Vector4d &vec,
78  Json::Value &value) {
79  value.clear();
80  value.append(vec(0));
81  value.append(vec(1));
82  value.append(vec(2));
83  value.append(vec(3));
84  return true;
85 }
86 
88  const Json::Value &value) {
89  if (value.size() != 9) {
90  return false;
91  } else {
92  for (int i = 0; i < 9; i++) {
93  mat.coeffRef(i) = value[i].asDouble();
94  }
95  return true;
96  }
97 }
98 
99 bool IJsonConvertible::EigenMatrix3dToJsonArray(const Eigen::Matrix3d &mat,
100  Json::Value &value) {
101  value.clear();
102  for (int i = 0; i < 9; i++) {
103  value.append(mat.coeffRef(i));
104  }
105  return true;
106 }
107 
109  const Json::Value &value) {
110  if (value.size() != 16) {
111  return false;
112  } else {
113  for (int i = 0; i < 16; i++) {
114  mat.coeffRef(i) = value[i].asDouble();
115  }
116  return true;
117  }
118 }
119 
120 bool IJsonConvertible::EigenMatrix4dToJsonArray(const Eigen::Matrix4d &mat,
121  Json::Value &value) {
122  value.clear();
123  for (int i = 0; i < 16; i++) {
124  value.append(mat.coeffRef(i));
125  }
126  return true;
127 }
128 
130  const Json::Value &value) {
131  if (value.size() != 16) {
132  return false;
133  } else {
134  for (int i = 0; i < 16; i++) {
135  mat.coeffRef(i) = value[i].asDouble();
136  }
137  return true;
138  }
139 }
140 
142  Json::Value &value) {
143  value.clear();
144  for (int i = 0; i < 16; i++) {
145  value.append(mat.coeffRef(i));
146  }
147  return true;
148 }
149 
151  const Json::Value &value) {
152  if (value.size() != 36) {
153  return false;
154  } else {
155  for (int i = 0; i < 36; i++) {
156  mat.coeffRef(i) = value[i].asDouble();
157  }
158  return true;
159  }
160 }
161 
163  Json::Value &value) {
164  value.clear();
165  for (int i = 0; i < 36; i++) {
166  value.append(mat.coeffRef(i));
167  }
168  return true;
169 }
170 
172  const Json::Value &value) {
173  if (value.size() != 36) {
174  return false;
175  } else {
176  for (int i = 0; i < 36; i++) {
177  mat.coeffRef(i) = value[i].asDouble();
178  }
179  return true;
180  }
181 }
182 
184  Json::Value &value) {
185  value.clear();
186  for (int i = 0; i < 36; i++) {
187  value.append(mat.coeffRef(i));
188  }
189  return true;
190 }
191 
192 } // namespace utility
193 } // namespace cloudViewer
static bool EigenMatrix3dFromJsonArray(Eigen::Matrix3d &mat, const Json::Value &value)
static bool EigenVector3dFromJsonArray(Eigen::Vector3d &vec, const Json::Value &value)
static bool EigenVector3dToJsonArray(const Eigen::Vector3d &vec, Json::Value &value)
virtual bool ConvertToJsonValue(Json::Value &value) const =0
static bool EigenVector4dToJsonArray(const Eigen::Vector4d &vec, Json::Value &value)
static bool EigenMatrix6dFromJsonArray(Eigen::Matrix6d &mat, const Json::Value &value)
static bool EigenMatrix3dToJsonArray(const Eigen::Matrix3d &mat, Json::Value &value)
static bool EigenVector4dFromJsonArray(Eigen::Vector4d &vec, const Json::Value &value)
static bool EigenMatrix6dToJsonArray(const Eigen::Matrix6d &mat, Json::Value &value)
virtual std::string ToString() const
Convert to a styled string representation of JSON data for display.
static bool EigenMatrix4dFromJsonArray(Eigen::Matrix4d &mat, const Json::Value &value)
static bool EigenMatrix4dToJsonArray(const Eigen::Matrix4d &mat, Json::Value &value)
#define LogError(...)
Definition: Logging.h:60
Eigen::Matrix< double, 6, 6, Eigen::DontAlign > Matrix6d_u
Definition: Eigen.h:112
Eigen::Matrix< double, 4, 4, Eigen::DontAlign > Matrix4d_u
Definition: Eigen.h:113
Eigen::Matrix< double, 6, 6 > Matrix6d
Extending Eigen namespace by adding frequently used matrix type.
Definition: Eigen.h:106
Json::Value StringToJson(const std::string &json_str)
Parse string and conver to Json::value. Throws exception if the conversion is invalid.
std::string JsonToString(const Json::Value &json)
Serialize a Json::Value to a string.
Generic file read and write utility for python interface.