NlohmannJSONReaderWithoutTypeCheck.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 <cmath>
23 #include <cstdint>
24 #include <cstring>
25 #include <memory>
26 #include <numeric>
27 
28 #include <SimoxUtility/algorithm/get_map_keys_values.h>
29 
31 
32 // Header
34 
35 // ArmarX
36 #include <RobotAPI/interface/aron/Aron.h>
39 
40 #include "../../json/Data.h"
41 
42 
44 {
47  {
49  }
50 
51  void
53  std::vector<nlohmann::json>& elements,
54  Path& p)
55  {
56  elements = input.get<std::vector<nlohmann::json>>();
57  }
58 
59  void
61  std::map<std::string, nlohmann::json>& elements,
62  Path& p)
63  {
64  elements = input.get<std::map<std::string, nlohmann::json>>();
65  }
66 
67 
68  const std::map<std::string, type::matrix::ElementType> ElementTypeAsString = {
69  {"unsigned char", ::armarx::aron::type::matrix::UINT8},
70  {"unsigned short", ::armarx::aron::type::matrix::UINT16},
71  {"unsigned int", ::armarx::aron::type::matrix::UINT32},
72  {"char", ::armarx::aron::type::matrix::INT8},
73  {"short", ::armarx::aron::type::matrix::INT16},
74  {"int", ::armarx::aron::type::matrix::INT32},
75  {"long", ::armarx::aron::type::matrix::INT64},
76  {"float", ::armarx::aron::type::matrix::FLOAT32},
77  {"double", ::armarx::aron::type::matrix::FLOAT64}};
78 
79  template <typename T>
80  void
81  readTo(const nlohmann::json& input, std::vector<unsigned char>& data)
82  {
83  const std::vector<T> d = input.at("data").get<std::vector<T>>();
84 
85  const std::size_t bufferLen = d.size() * sizeof(T);
86 
87  data.resize(bufferLen);
88  memcpy(data.data(), d.data(), bufferLen);
89  }
90 
91  void
93  std::vector<int>& shape,
94  std::string& typeAsString,
95  std::vector<unsigned char>& data,
96  Path& p)
97  {
98  shape = input.at("dims").get<std::vector<int>>();
99 
100  typeAsString = input.at("type").get<std::string>();
101 
102  ARMARX_CHECK(ElementTypeAsString.count(typeAsString) > 0)
103  << "Invalid element `" << typeAsString << "`. Valid elements are "
104  << simox::alg::get_keys(ElementTypeAsString) << ".";
105 
106  const type::matrix::ElementType elementType = ElementTypeAsString.at(typeAsString);
107 
108  // as we need to return a vector<uchar>, we first need to read the json array as the specific type
109  // and then convert it to the vector<uchar>
110  switch (elementType)
111  {
112  case type::matrix::UINT8:
113  readTo<std::uint8_t>(input, data);
114  break;
115  case type::matrix::UINT16:
116  readTo<std::uint16_t>(input, data);
117  break;
118  case type::matrix::UINT32:
119  readTo<std::uint32_t>(input, data);
120  break;
121  case type::matrix::INT8:
122  readTo<std::int8_t>(input, data);
123  break;
124  case type::matrix::INT16:
125  readTo<std::int16_t>(input, data);
126  break;
127  case type::matrix::INT32:
128  readTo<std::int32_t>(input, data);
129  break;
130  case type::matrix::INT64:
131  readTo<std::int64_t>(input, data);
132  break;
133  case type::matrix::FLOAT32:
134  readTo<std::float_t>(input, data);
135  break;
136  case type::matrix::FLOAT64:
137  readTo<std::double_t>(input, data);
138  break;
139  }
140 
141  ARMARX_INFO << VAROUT(typeAsString);
142  }
143 
144  void
145  NlohmannJSONReaderWithoutTypeCheck::readInt(const nlohmann::json& input, int& i, Path& p)
146  {
147  i = input;
148  }
149 
150  void
151  NlohmannJSONReaderWithoutTypeCheck::readLong(const nlohmann::json& input, long& i, Path& p)
152  {
153  i = input;
154  }
155 
156  void
157  NlohmannJSONReaderWithoutTypeCheck::readFloat(const nlohmann::json& input, float& i, Path& p)
158  {
159  i = input;
160  }
161 
162  void
163  NlohmannJSONReaderWithoutTypeCheck::readDouble(const nlohmann::json& input, double& i, Path& p)
164  {
165  i = input;
166  }
167 
168  void
170  std::string& i,
171  Path& p)
172  {
173  i = input;
174  }
175 
176  void
177  NlohmannJSONReaderWithoutTypeCheck::readBool(const nlohmann::json& input, bool& i, Path& p)
178  {
179  i = input;
180  }
181 
182 } // namespace armarx::aron::data::reader
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readString
void readString(InputType &input, std::string &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:169
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readInt
void readInt(InputType &input, int &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:145
armarx::aron::data::ConstNlohmannJSONVisitor::GetDescriptor
static data::Descriptor GetDescriptor(Input &n)
Definition: NlohmannJSONVisitor.cpp:29
NlohmannJSONVisitor.h
armarx::aron::data::ReaderInterface< const nlohmann::json >::InputType
const nlohmann::json InputType
Definition: Reader.h:39
NlohmannJSONReaderWithoutTypeCheck.h
armarx::aron::data::reader::ElementTypeAsString
const std::map< std::string, type::matrix::ElementType > ElementTypeAsString
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:68
armarx::aron::data::reader::readTo
void readTo(const nlohmann::json &input, std::vector< unsigned char > &data)
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:81
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readNDArray
void readNDArray(InputType &input, std::vector< int > &shape, std::string &typeAsString, std::vector< unsigned char > &data, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:92
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readDict
void readDict(InputType &input, std::map< std::string, InputTypeNonConst > &elements, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:60
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::getDescriptor
data::Descriptor getDescriptor(InputType &input) final
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:46
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readList
void readList(InputType &input, std::vector< InputTypeNonConst > &elements, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:52
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readFloat
void readFloat(InputType &input, float &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:157
armarx::aron::data::reader
Definition: NlohmannJSONReader.cpp:34
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readDouble
void readDouble(InputType &input, double &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:163
Exception.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readBool
void readBool(InputType &input, bool &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:177
armarx::aron::data::reader::NlohmannJSONReaderWithoutTypeCheck::readLong
void readLong(InputType &input, long &i, Path &p) override
Definition: NlohmannJSONReaderWithoutTypeCheck.cpp:151
Logging.h
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35