NlohmannJSONWriter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller (fabian dot peller at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 // Header
25 #include "NlohmannJSONWriter.h"
26 
27 // Constantes
29 
30 #include "../../json/Data.h"
31 
33 {
34  namespace
35  {
36  /// Set important members for json object (aron meta information)
37  void
38  setupAronMetaInformationForType(nlohmann::json& json,
39  const std::string& type,
40  const type::Maybe& maybe,
41  const Path& p)
42  {
46  }
47  } // namespace
48 
51  {
53  }
54 
55  nlohmann::json
56  NlohmannJSONWriter::writeObject(const std::string& name,
57  const std::vector<std::string>& templates,
58  const std::vector<std::string>& templateInstantiations,
59  const std::map<std::string, nlohmann::json>& memberTypes,
60  const std::optional<nlohmann::json>& extends,
61  const type::Maybe maybe,
62  const Path& p)
63  {
64  nlohmann::json o;
65  setupAronMetaInformationForType(o, rw::json::constantes::OBJECT_TYPENAME_SLUG, maybe, p);
67  if (extends.has_value())
68  {
70  }
71  if (!templates.empty())
72  {
74  }
75  if (!templateInstantiations.empty())
76  {
77  o[rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG] = templateInstantiations;
78  }
79 
80  o[rw::json::constantes::MEMBERS_SLUG] = nlohmann::json(nlohmann::json::value_t::object);
81  for (const auto& [key, value] : memberTypes)
82  {
84  }
85  return o;
86  }
87 
88  nlohmann::json
89  NlohmannJSONWriter::writeList(const nlohmann::json& acceptedType,
90  const type::Maybe maybe,
91  const Path& p)
92  {
93  nlohmann::json o;
94  setupAronMetaInformationForType(o, rw::json::constantes::LIST_TYPENAME_SLUG, maybe, p);
96  return o;
97  }
98 
99  nlohmann::json
100  NlohmannJSONWriter::writeDict(const nlohmann::json& acceptedType,
101  const type::Maybe maybe,
102  const Path& p)
103  {
104  nlohmann::json o;
105  setupAronMetaInformationForType(o, rw::json::constantes::DICT_TYPENAME_SLUG, maybe, p);
107  return o;
108  }
109 
110  nlohmann::json
111  NlohmannJSONWriter::writePair(const nlohmann::json& acceptedType1,
112  const nlohmann::json& acceptedType2,
113  const type::Maybe maybe,
114  const Path& p)
115  {
116  nlohmann::json o;
117  setupAronMetaInformationForType(o, rw::json::constantes::PAIR_TYPENAME_SLUG, maybe, p);
118  o[rw::json::constantes::ACCEPTED_TYPE_SLUG] = {acceptedType1, acceptedType2};
119  return o;
120  }
121 
122  nlohmann::json
123  NlohmannJSONWriter::writeTuple(const std::vector<nlohmann::json>& acceptedTypes,
124  const type::Maybe maybe,
125  const Path& p)
126  {
127  nlohmann::json o;
128  setupAronMetaInformationForType(o, rw::json::constantes::TUPLE_TYPENAME_SLUG, maybe, p);
129  o[rw::json::constantes::ACCEPTED_TYPE_SLUG] = acceptedTypes;
130  return o;
131  }
132 
133  nlohmann::json
135  const type::ndarray::ElementType type,
136  const std::string& defaultValue,
137  const type::Maybe maybe,
138  const Path& p)
139  {
140  nlohmann::json o;
141  setupAronMetaInformationForType(o, rw::json::constantes::NDARRAY_TYPENAME_SLUG, maybe, p);
144  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
145  return o;
146  }
147 
148  nlohmann::json
150  const int cols,
151  const type::matrix::ElementType type,
152  const std::string& defaultValue,
153  const type::Maybe maybe,
154  const Path& p)
155  {
156  nlohmann::json o;
157  setupAronMetaInformationForType(o, rw::json::constantes::MATRIX_TYPENAME_SLUG, maybe, p);
159  o[rw::json::constantes::DIMENSIONS_SLUG] = {rows, cols};
160  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
161  return o;
162  }
163 
164  nlohmann::json
166  const std::string& defaultValue,
167  const type::Maybe maybe,
168  const Path& p)
169  {
170  nlohmann::json o;
171  setupAronMetaInformationForType(
175  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
176  return o;
177  }
178 
179  nlohmann::json
180  NlohmannJSONWriter::writeImage(const type::image::PixelType type,
181  const std::string& defaultValue,
182  const type::Maybe maybe,
183  const Path& p)
184  {
185  nlohmann::json o;
186  setupAronMetaInformationForType(o, rw::json::constantes::IMAGE_TYPENAME_SLUG, maybe, p);
188  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
189  return o;
190  }
191 
192  nlohmann::json
193  NlohmannJSONWriter::writePointCloud(const type::pointcloud::VoxelType type,
194  const std::string& defaultValue,
195  const type::Maybe maybe,
196  const Path& p)
197  {
198  nlohmann::json o;
199  setupAronMetaInformationForType(
202  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
203  return o;
204  }
205 
206  nlohmann::json
207  NlohmannJSONWriter::writeIntEnum(const std::string& name,
208  const std::map<std::string, int>& acceptedValues,
209  const std::string& defaultValue,
210  const type::Maybe maybe,
211  const Path& p)
212  {
213  nlohmann::json o;
214  setupAronMetaInformationForType(o, rw::json::constantes::INT_ENUM_TYPENAME_SLUG, maybe, p);
216  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue;
217 
218  o[rw::json::constantes::ELEMENTS_SLUG] = nlohmann::json(nlohmann::json::value_t::object);
219  for (const auto& [key, value] : acceptedValues)
220  {
222  }
223  return o;
224  }
225 
226  nlohmann::json
227  NlohmannJSONWriter::writeInt(const std::optional<int>& defaultValue,
228  const type::Maybe maybe,
229  const Path& p)
230  {
231  nlohmann::json o;
232  setupAronMetaInformationForType(o, rw::json::constantes::INT_TYPENAME_SLUG, maybe, p);
234  defaultValue.has_value() ? std::to_string(*defaultValue) : "";
235  return o;
236  }
237 
238  nlohmann::json
239  NlohmannJSONWriter::writeLong(const std::optional<long>& defaultValue,
240  const type::Maybe maybe,
241  const Path& p)
242  {
243  nlohmann::json o;
244  setupAronMetaInformationForType(o, rw::json::constantes::LONG_TYPENAME_SLUG, maybe, p);
246  defaultValue.has_value() ? std::to_string(*defaultValue) : "";
247  return o;
248  }
249 
250  nlohmann::json
251  NlohmannJSONWriter::writeFloat(const std::optional<float>& defaultValue,
252  const type::Maybe maybe,
253  const Path& p)
254  {
255  nlohmann::json o;
256  setupAronMetaInformationForType(o, rw::json::constantes::FLOAT_TYPENAME_SLUG, maybe, p);
258  defaultValue.has_value() ? std::to_string(*defaultValue) : "";
259  return o;
260  }
261 
262  nlohmann::json
263  NlohmannJSONWriter::writeDouble(const std::optional<double>& defaultValue,
264  const type::Maybe maybe,
265  const Path& p)
266  {
267  nlohmann::json o;
268  setupAronMetaInformationForType(o, rw::json::constantes::DOUBLE_TYPENAME_SLUG, maybe, p);
270  defaultValue.has_value() ? std::to_string(*defaultValue) : "";
271  return o;
272  }
273 
274  nlohmann::json
275  NlohmannJSONWriter::writeString(const std::optional<std::string>& defaultValue,
276  const type::Maybe maybe,
277  const Path& p)
278  {
279  nlohmann::json o;
280  setupAronMetaInformationForType(o, rw::json::constantes::STRING_TYPENAME_SLUG, maybe, p);
281  o[rw::json::constantes::DEFAULT_SLUG] = defaultValue.has_value() ? *defaultValue : "";
282  return o;
283  }
284 
285  nlohmann::json
286  NlohmannJSONWriter::writeBool(const std::optional<bool>& defaultValue,
287  const type::Maybe maybe,
288  const Path& p)
289  {
290  nlohmann::json o;
291  setupAronMetaInformationForType(o, rw::json::constantes::BOOL_TYPENAME_SLUG, maybe, p);
293  defaultValue.has_value() ? std::to_string(*defaultValue) : "";
294  return o;
295  }
296 
297  nlohmann::json
298  NlohmannJSONWriter::writeAnyObject(const type::Maybe maybe, const Path& p)
299  {
300  nlohmann::json o;
301  setupAronMetaInformationForType(
303  return o;
304  }
305 } // namespace armarx::aron::type::writer
armarx::aron::type::writer::NlohmannJSONWriter::writeQuaternion
ReturnType writeQuaternion(const type::quaternion::ElementType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a quaternion from the params.
Definition: NlohmannJSONWriter.cpp:165
armarx::aron::type::writer::NlohmannJSONWriter::writeMatrix
ReturnType writeMatrix(const int rows, const int cols, const type::matrix::ElementType type, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a matrix from the params.
Definition: NlohmannJSONWriter.cpp:149
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::writer::NlohmannJSONWriter::writeImage
ReturnType writeImage(const type::image::PixelType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a image from the params.
Definition: NlohmannJSONWriter.cpp:180
armarx::aron::type::rw::json::constantes::DIMENSIONS_SLUG
const constexpr auto DIMENSIONS_SLUG
Definition: Data.h:50
armarx::aron::type::ConstNlohmannJSONVisitor::GetDescriptor
static type::Descriptor GetDescriptor(Input &n)
Definition: NlohmannJSONVisitor.cpp:29
armarx::aron::type::rw::json::constantes::EXTENDS_SLUG
const constexpr auto EXTENDS_SLUG
Definition: Data.h:48
armarx::aron::type::writer::NlohmannJSONWriter::writeFloat
ReturnType writeFloat(const std::optional< float > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a float from the params.
Definition: NlohmannJSONWriter.cpp:251
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::conversion::QuaternionType2String
const std::map< type::quaternion::ElementType, std::string > QuaternionType2String
Definition: Data.h:128
armarx::aron::type::writer::NlohmannJSONWriter::writeDict
ReturnType writeDict(const ReturnType &acceptedType, const type::Maybe maybe, const Path &p=Path()) override
Construct a dict from the params.
armarx::aron::type::writer::NlohmannJSONWriter::writeObject
ReturnType writeObject(const std::string &name, const std::vector< std::string > &templates, const std::vector< std::string > &templateInstantiations, const std::map< std::string, ReturnType > &memberTypes, const std::optional< ReturnType > &extends, const type::Maybe maybe, const Path &p=Path()) override
Construct an object from the params.
Definition: NlohmannJSONWriter.cpp:56
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::rw::json::conversion::PixelType2String
const std::map< type::image::PixelType, std::string > PixelType2String
Definition: Data.h:133
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::writer::NlohmannJSONWriter::writeString
ReturnType writeString(const std::optional< std::string > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a string from the params.
Definition: NlohmannJSONWriter.cpp:275
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::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::type::writer::NlohmannJSONWriter::writePointCloud
ReturnType writePointCloud(const type::pointcloud::VoxelType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a pointcloud from the params.
Definition: NlohmannJSONWriter.cpp:193
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::type::WriterInterface< nlohmann::json >::ReturnTypeConst
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition: Writer.h:42
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::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::type::writer::NlohmannJSONWriter::getDescriptor
type::Descriptor getDescriptor(ReturnTypeConst &input) final
Definition: NlohmannJSONWriter.cpp:50
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::rw::json::constantes::POINT_CLOUD_TYPENAME_SLUG
const constexpr auto POINT_CLOUD_TYPENAME_SLUG
Definition: Data.h:66
armarx::aron::type::writer::NlohmannJSONWriter::writeAnyObject
ReturnType writeAnyObject(const type::Maybe maybe, const Path &p=Path()) override
Construct a time from the params.
Definition: NlohmannJSONWriter.cpp:298
armarx::aron::type::writer::NlohmannJSONWriter::writeDouble
ReturnType writeDouble(const std::optional< double > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a double from the params.
Definition: NlohmannJSONWriter.cpp:263
armarx::aron::type::rw::json::constantes::PAIR_TYPENAME_SLUG
const constexpr auto PAIR_TYPENAME_SLUG
Definition: Data.h:61
armarx::aron::type::writer::NlohmannJSONWriter::writeIntEnum
ReturnType writeIntEnum(const std::string &name, const std::map< std::string, int > &acceptedValues, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a int enum from the params.
Definition: NlohmannJSONWriter.cpp:207
armarx::aron::type::writer::NlohmannJSONWriter::writeList
ReturnType writeList(const ReturnType &acceptedType, const type::Maybe maybe, const Path &p=Path()) override
Construct a list from the params.
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::conversion::NDArrayType2String
const std::map< type::ndarray::ElementType, std::string > NDArrayType2String
Definition: Data.h:109
armarx::aron::type::rw::json::conversion::MatrixType2String
const std::map< type::matrix::ElementType, std::string > MatrixType2String
Definition: Data.h:120
armarx::aron::type::writer::NlohmannJSONWriter::writeNDArray
ReturnType writeNDArray(const int ndim, const type::ndarray::ElementType, const std::string &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a ndarray from the params.
Definition: NlohmannJSONWriter.cpp:134
armarx::aron::type::rw::json::constantes::LIST_TYPENAME_SLUG
const constexpr auto LIST_TYPENAME_SLUG
Definition: Data.h:57
NlohmannJSONWriter.h
armarx::aron::type::rw::json::constantes::OBJECT_TYPENAME_SLUG
const constexpr auto OBJECT_TYPENAME_SLUG
Definition: Data.h:59
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
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::writer::NlohmannJSONWriter::writeInt
ReturnType writeInt(const std::optional< int > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a int from the params.
Definition: NlohmannJSONWriter.cpp:227
armarx::aron::type::rw::json::conversion::Maybe2String
const std::map< type::Maybe, std::string > Maybe2String
Definition: Data.h:101
armarx::aron::type::rw::json::constantes::TEMPLATE_INSTANTIATIONS_SLUG
const constexpr auto TEMPLATE_INSTANTIATIONS_SLUG
Definition: Data.h:54
armarx::aron::type::writer::NlohmannJSONWriter::writeTuple
ReturnType writeTuple(const std::vector< ReturnType > &acceptedTypes, const type::Maybe maybe, const Path &p=Path()) override
Construct a tuple from the params.
Definition: NlohmannJSONWriter.cpp:123
armarx::aron::type::rw::json::constantes::DEFAULT_SLUG
const constexpr auto DEFAULT_SLUG
Definition: Data.h:55
armarx::aron::type::writer
Definition: NlohmannJSONWriter.cpp:32
armarx::aron::type::rw::json::constantes::NDARRAY_TYPENAME_SLUG
const constexpr auto NDARRAY_TYPENAME_SLUG
Definition: Data.h:62
armarx::aron::type::writer::NlohmannJSONWriter::writeLong
ReturnType writeLong(const std::optional< long > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a long from the params.
Definition: NlohmannJSONWriter.cpp:239
armarx::aron::type::rw::json::constantes::ELEMENTS_SLUG
const constexpr auto ELEMENTS_SLUG
Definition: Data.h:45
armarx::aron::type::rw::json::conversion::VoxelType2String
const std::map< type::pointcloud::VoxelType, std::string > VoxelType2String
Definition: Data.h:138
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
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:85
armarx::aron::type::writer::NlohmannJSONWriter::writeBool
ReturnType writeBool(const std::optional< bool > &defaultValue, const type::Maybe maybe, const Path &p=Path()) override
Construct a bool from the params.
Definition: NlohmannJSONWriter.cpp:286
armarx::aron::type::writer::NlohmannJSONWriter::writePair
ReturnType writePair(const ReturnType &acceptedType1, const ReturnType &acceptedType2, const type::Maybe maybe, const Path &p=Path()) override
Construct a pair from the params.
Definition: NlohmannJSONWriter.cpp:111