NlohmannJSONReader.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Peller (fabian dot peller at kit dot edu)
17 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18 * GNU General Public License
19 */
20 
21 // STD/STL
22 #include <memory>
23 #include <numeric>
24 
25 // Header
26 #include "NlohmannJSONReader.h"
27 
28 // ArmarX
31 
32 #include "../../json/Data.h"
33 
35 {
36  namespace
37  {
38  /// Throw an exception if the type is other than expected
39  void
40  getAronMetaInformationForType(const nlohmann::json& input,
41  const std::string& expectedType,
42  Path& p)
43  {
44  if (input[rw::json::constantes::TYPE_SLUG] != expectedType)
45  {
46  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
47  "Wrong type in json encountered.",
49  expectedType);
50  }
51 
52  // get Path
53  std::vector<std::string> pathElements = input[rw::json::constantes::PATH_SLUG];
54  p = Path(pathElements);
55  }
56  } // namespace
57 
60  {
62  }
63 
64  void
65  NlohmannJSONReader::readObject(const nlohmann::json& input,
66  std::string& name,
67  std::vector<std::string>& templates,
68  std::vector<std::string>& templateInstantiations,
69  std::map<std::string, nlohmann::json>& memberTypes,
70  type::Maybe& maybe,
71  Path& p)
72  {
73  getAronMetaInformationForType(input, rw::json::constantes::OBJECT_TYPENAME_SLUG, p);
74 
77  {
78  templates = input[rw::json::constantes::TEMPLATES_SLUG].get<std::vector<std::string>>();
79  }
81  {
83  .get<std::vector<std::string>>();
84  }
85 
87 
88  memberTypes =
89  input[rw::json::constantes::MEMBERS_SLUG].get<std::map<std::string, nlohmann::json>>();
90  }
91 
92  void
93  NlohmannJSONReader::readList(const nlohmann::json& input,
94  nlohmann::json& acceptedType,
95  type::Maybe& maybe,
96  Path& p)
97  {
98  getAronMetaInformationForType(input, rw::json::constantes::LIST_TYPENAME_SLUG, p);
99 
102  }
103 
104  void
105  NlohmannJSONReader::readDict(const nlohmann::json& input,
106  nlohmann::json& acceptedType,
107  type::Maybe& maybe,
108  Path& p)
109  {
110  getAronMetaInformationForType(input, rw::json::constantes::DICT_TYPENAME_SLUG, p);
111 
114  }
115 
116  void
117  NlohmannJSONReader::readTuple(const nlohmann::json& input,
118  std::vector<nlohmann::json>& acceptedTypes,
119  type::Maybe& maybe,
120  Path& p)
121  {
122  getAronMetaInformationForType(input, rw::json::constantes::TUPLE_TYPENAME_SLUG, p);
123 
125  acceptedTypes =
126  input[rw::json::constantes::ACCEPTED_TYPE_SLUG].get<std::vector<nlohmann::json>>();
127  }
128 
129  void
130  NlohmannJSONReader::readPair(const nlohmann::json& input,
131  nlohmann::json& acceptedType1,
132  nlohmann::json& acceptedType2,
133  type::Maybe& maybe,
134  Path& p)
135  {
136  getAronMetaInformationForType(input, rw::json::constantes::PAIR_TYPENAME_SLUG, p);
137 
139  auto list =
140  input[rw::json::constantes::ACCEPTED_TYPE_SLUG].get<std::vector<nlohmann::json>>();
141  acceptedType1 = list[0];
142  acceptedType2 = list[1];
143  }
144 
145  void
146  NlohmannJSONReader::readNDArray(const nlohmann::json& input,
147  int& ndim,
149  std::string& defaultValue,
150  type::Maybe& maybe,
151  Path& p)
152  {
153  getAronMetaInformationForType(input, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
154 
157 
161  }
162 
163  void
164  NlohmannJSONReader::readMatrix(const nlohmann::json& input,
165  int& rows,
166  int& cols,
168  std::string& defaultValue,
169  type::Maybe& maybe,
170  Path& p)
171  {
172  getAronMetaInformationForType(input, rw::json::constantes::MATRIX_TYPENAME_SLUG, p);
173 
175  auto list = input[rw::json::constantes::DIMENSIONS_SLUG].get<std::vector<nlohmann::json>>();
176  rows = list[0];
177  cols = list[1];
178 
182  }
183 
184  void
187  std::string& defaultValue,
188  type::Maybe& maybe,
189  Path& p)
190  {
191  getAronMetaInformationForType(input, rw::json::constantes::QUATERNION_TYPENAME_SLUG, p);
192 
194 
198  }
199 
200  void
202  type::pointcloud::VoxelType& type,
203  std::string& defaultValue,
204  type::Maybe& maybe,
205  Path& p)
206  {
207  getAronMetaInformationForType(input, rw::json::constantes::POINT_CLOUD_TYPENAME_SLUG, p);
208 
210 
214  }
215 
216  void
217  NlohmannJSONReader::readImage(const nlohmann::json& input,
218  type::image::PixelType& type,
219  std::string& defaultValue,
220  type::Maybe& maybe,
221  Path& p)
222  {
223  getAronMetaInformationForType(input, rw::json::constantes::IMAGE_TYPENAME_SLUG, p);
224 
226 
230  }
231 
232  void
233  NlohmannJSONReader::readIntEnum(const nlohmann::json& input,
234  std::string& name,
235  std::map<std::string, int>& acceptedValues,
236  std::string& defaultValue,
237  type::Maybe& maybe,
238  Path& p)
239  {
240  getAronMetaInformationForType(input, rw::json::constantes::INT_ENUM_TYPENAME_SLUG, p);
241 
244 
245  acceptedValues =
246  input[rw::json::constantes::ELEMENTS_SLUG].get<std::map<std::string, int>>();
247 
249  }
250 
251  void
252  NlohmannJSONReader::readInt(const nlohmann::json& input,
253  std::optional<int>& defaultValue,
254  type::Maybe& maybe,
255  Path& p)
256  {
257  getAronMetaInformationForType(input, rw::json::constantes::INT_TYPENAME_SLUG, p);
258 
260 
262  {
263  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
264  {
265  defaultValue = std::stoi(d);
266  }
267  }
268  }
269 
270  void
271  NlohmannJSONReader::readLong(const nlohmann::json& input,
272  std::optional<long>& defaultValue,
273  type::Maybe& maybe,
274  Path& p)
275  {
276  getAronMetaInformationForType(input, rw::json::constantes::LONG_TYPENAME_SLUG, p);
277 
279 
281  {
282  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
283  {
284  defaultValue = std::stol(d);
285  }
286  }
287  }
288 
289  void
290  NlohmannJSONReader::readFloat(const nlohmann::json& input,
291  std::optional<float>& defaultValue,
292  type::Maybe& maybe,
293  Path& p)
294  {
295  getAronMetaInformationForType(input, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
296 
298 
300  {
301  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
302  {
303  defaultValue = std::stof(d);
304  }
305  }
306  }
307 
308  void
309  NlohmannJSONReader::readDouble(const nlohmann::json& input,
310  std::optional<double>& defaultValue,
311  type::Maybe& maybe,
312  Path& p)
313  {
314  getAronMetaInformationForType(input, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
315 
317 
319  {
320  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
321  {
322  defaultValue = std::stod(d);
323  }
324  }
325  }
326 
327  void
328  NlohmannJSONReader::readString(const nlohmann::json& input,
329  std::optional<std::string>& defaultValue,
330  type::Maybe& maybe,
331  Path& p)
332  {
333  getAronMetaInformationForType(input, rw::json::constantes::STRING_TYPENAME_SLUG, p);
334 
336 
338  {
339  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
340  {
341  defaultValue = d;
342  }
343  }
344  }
345 
346  void
347  NlohmannJSONReader::readBool(const nlohmann::json& input,
348  std::optional<bool>& defaultValue,
349  type::Maybe& maybe,
350  Path& p)
351  {
352  getAronMetaInformationForType(input, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
353 
355 
357  {
358  if (std::string d = input[rw::json::constantes::DEFAULT_SLUG]; not d.empty())
359  {
360  defaultValue = std::stoi(d);
361  }
362  }
363  }
364 
365  void
366  NlohmannJSONReader::readAnyObject(const nlohmann::json& input, type::Maybe& maybe, Path& p)
367  {
368  getAronMetaInformationForType(input, rw::json::constantes::ANY_OBJECT_TYPENAME_SLUG, p);
369 
371  }
372 } // namespace armarx::aron::type::reader
armarx::aron::type::reader
Definition: NlohmannJSONReader.cpp:34
armarx::aron::type::rw::json::constantes::DICT_TYPENAME_SLUG
const constexpr auto DICT_TYPENAME_SLUG
Definition: Data.h:58
armarx::aron::type::rw::json::constantes::DOUBLE_TYPENAME_SLUG
const constexpr auto DOUBLE_TYPENAME_SLUG
Definition: Data.h:71
armarx::aron::type::reader::NlohmannJSONReader::readPointCloud
void readPointCloud(InputType &input, type::pointcloud::VoxelType &type, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:201
armarx::aron::type::reader::NlohmannJSONReader::readMatrix
void readMatrix(InputType &input, int &rows, int &cols, type::matrix::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:164
armarx::aron::type::reader::NlohmannJSONReader::readDouble
void readDouble(InputType &input, std::optional< double > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
list
list(APPEND SOURCES ${QT_RESOURCES}) set(COMPONENT_LIBS ArmarXGui ArmarXCoreObservers ArmarXCoreEigen3Variants PlotterController $
Definition: CMakeLists.txt:49
armarx::aron::type::rw::json::constantes::DIMENSIONS_SLUG
const constexpr auto DIMENSIONS_SLUG
Definition: Data.h:50
armarx::aron::type::reader::NlohmannJSONReader::readPair
void readPair(InputType &input, InputTypeNonConst &acceptedType1, InputTypeNonConst &acceptedType2, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:130
armarx::aron::type::rw::json::conversion::String2NDArrayType
const auto String2NDArrayType
Definition: Data.h:118
armarx::aron::type::ConstNlohmannJSONVisitor::GetDescriptor
static type::Descriptor GetDescriptor(Input &n)
Definition: NlohmannJSONVisitor.cpp:29
armarx::aron::type::reader::NlohmannJSONReader::readIntEnum
void readIntEnum(InputType &input, std::string &name, std::map< std::string, int > &acceptedValues, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:233
armarx::aron::type::reader::NlohmannJSONReader::readLong
void readLong(InputType &input, std::optional< long > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::type::ReaderInterface::InputType
I InputType
Definition: Reader.h:41
armarx::aron::type::rw::json::constantes::INT_TYPENAME_SLUG
const constexpr auto INT_TYPENAME_SLUG
Definition: Data.h:68
armarx::aron::type::rw::json::constantes::ANY_OBJECT_TYPENAME_SLUG
const constexpr auto ANY_OBJECT_TYPENAME_SLUG
Definition: Data.h:75
armarx::aron::type::rw::json::constantes::NAME_SLUG
const constexpr auto NAME_SLUG
Definition: Data.h:47
armarx::aron::type::rw::json::constantes::QUATERNION_TYPENAME_SLUG
const constexpr auto QUATERNION_TYPENAME_SLUG
Definition: Data.h:65
armarx::aron::type::reader::NlohmannJSONReader::readTuple
void readTuple(InputType &input, std::vector< InputTypeNonConst > &acceptedTypes, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:117
armarx::aron::type::rw::json::constantes::PATH_SLUG
const constexpr auto PATH_SLUG
Definition: Data.h:39
armarx::aron::type::rw::json::constantes::MATRIX_TYPENAME_SLUG
const constexpr auto MATRIX_TYPENAME_SLUG
Definition: Data.h:64
armarx::aron::type::reader::NlohmannJSONReader::readAnyObject
void readAnyObject(InputType &input, type::Maybe &maybe, Path &p) override
Definition: NlohmannJSONReader.cpp:366
armarx::aron::type::reader::NlohmannJSONReader::readBool
void readBool(InputType &input, std::optional< bool > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::type::rw::json::constantes::USED_TYPE_SLUG
const constexpr auto USED_TYPE_SLUG
Definition: Data.h:52
armarx::aron::type::rw::json::constantes::TEMPLATES_SLUG
const constexpr auto TEMPLATES_SLUG
Definition: Data.h:53
armarx::aron::type::reader::NlohmannJSONReader::readList
void readList(InputType &input, InputTypeNonConst &acceptedType, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::rw::json::constantes::ACCEPTED_TYPE_SLUG
const constexpr auto ACCEPTED_TYPE_SLUG
Definition: Data.h:49
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
NlohmannJSONVisitor.h
armarx::aron::type::rw::json::constantes::TUPLE_TYPENAME_SLUG
const constexpr auto TUPLE_TYPENAME_SLUG
Definition: Data.h:60
armarx::aron::type::rw::json::conversion::String2VoxelType
const auto String2VoxelType
Definition: Data.h:150
armarx::aron::type::rw::json::constantes::BOOL_TYPENAME_SLUG
const constexpr auto BOOL_TYPENAME_SLUG
Definition: Data.h:73
armarx::aron::type::rw::json::constantes::TYPE_SLUG
const constexpr auto TYPE_SLUG
Definition: Data.h:38
armarx::aron::type::rw::json::constantes::MAYBE_SLUG
const constexpr auto MAYBE_SLUG
Definition: Data.h:37
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::aron::type::rw::json::constantes::INT_ENUM_TYPENAME_SLUG
const constexpr auto INT_ENUM_TYPENAME_SLUG
Definition: Data.h:67
armarx::aron::type::reader::NlohmannJSONReader::readQuaternion
void readQuaternion(InputType &input, type::quaternion::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:185
armarx::aron::type::rw::json::conversion::String2QuaternionType
const auto String2QuaternionType
Definition: Data.h:135
armarx::aron::type::reader::NlohmannJSONReader::readInt
void readInt(InputType &input, std::optional< int > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::type::rw::json::constantes::POINT_CLOUD_TYPENAME_SLUG
const constexpr auto POINT_CLOUD_TYPENAME_SLUG
Definition: Data.h:66
armarx::aron::type::rw::json::constantes::PAIR_TYPENAME_SLUG
const constexpr auto PAIR_TYPENAME_SLUG
Definition: Data.h:61
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::type::rw::json::constantes::IMAGE_TYPENAME_SLUG
const constexpr auto IMAGE_TYPENAME_SLUG
Definition: Data.h:63
armarx::aron::type::rw::json::constantes::MEMBERS_SLUG
const constexpr auto MEMBERS_SLUG
Definition: Data.h:44
armarx::aron::type::rw::json::constantes::LIST_TYPENAME_SLUG
const constexpr auto LIST_TYPENAME_SLUG
Definition: Data.h:57
armarx::aron::type::reader::NlohmannJSONReader::readFloat
void readFloat(InputType &input, std::optional< float > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::type::rw::json::constantes::OBJECT_TYPENAME_SLUG
const constexpr auto OBJECT_TYPENAME_SLUG
Definition: Data.h:59
armarx::aron::type::rw::json::constantes::LONG_TYPENAME_SLUG
const constexpr auto LONG_TYPENAME_SLUG
Definition: Data.h:69
armarx::aron::type::rw::json::constantes::STRING_TYPENAME_SLUG
const constexpr auto STRING_TYPENAME_SLUG
Definition: Data.h:72
armarx::aron::type::reader::NlohmannJSONReader::readNDArray
void readNDArray(InputType &input, int &ndim, type::ndarray::ElementType &type, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Exception.h
armarx::aron::type::rw::json::conversion::String2Maybe
const auto String2Maybe
Definition: Data.h:107
armarx::aron::type::rw::json::conversion::String2MatrixType
const auto String2MatrixType
Definition: Data.h:130
armarx::aron::type::rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG
const constexpr auto TEMPLATE_INSTANTIATIONS_SLUG
Definition: Data.h:54
armarx::aron::type::reader::NlohmannJSONReader::getDescriptor
type::Descriptor getDescriptor(InputType &input) final
Definition: NlohmannJSONReader.cpp:59
armarx::aron::type::reader::NlohmannJSONReader::readDict
void readDict(InputType &input, InputTypeNonConst &acceptedType, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::aron::type::reader::NlohmannJSONReader::readObject
void readObject(InputType &input, std::string &name, std::vector< std::string > &templates, std::vector< std::string > &templateInstantiations, std::map< std::string, InputTypeNonConst > &memberTypes, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:65
armarx::aron::type::rw::json::constantes::DEFAULT_SLUG
const constexpr auto DEFAULT_SLUG
Definition: Data.h:55
armarx::aron::type::reader::NlohmannJSONReader::readImage
void readImage(InputType &input, type::image::PixelType &type, std::string &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
Definition: NlohmannJSONReader.cpp:217
armarx::aron::type::rw::json::constantes::NDARRAY_TYPENAME_SLUG
const constexpr auto NDARRAY_TYPENAME_SLUG
Definition: Data.h:62
armarx::aron::type::reader::NlohmannJSONReader::readString
void readString(InputType &input, std::optional< std::string > &defaultValue, type::Maybe &maybe, Path &p= *std::unique_ptr< Path >(new Path())) override
armarx::aron::type::rw::json::conversion::String2PixelType
const auto String2PixelType
Definition: Data.h:140
armarx::aron::type::rw::json::constantes::ELEMENTS_SLUG
const constexpr auto ELEMENTS_SLUG
Definition: Data.h:45
NlohmannJSONReader.h
armarx::aron::type::Descriptor
Descriptor
Definition: Descriptor.h:76
armarx::aron::type::rw::json::constantes::FLOAT_TYPENAME_SLUG
const constexpr auto FLOAT_TYPENAME_SLUG
Definition: Data.h:70