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
22
23#include <cmath>
24#include <cstdint>
25#include <cstring>
26
27#include <SimoxUtility/algorithm/get_map_keys_values.h>
28
31
32#include <RobotAPI/interface/aron/Aron.h>
35
37{
43
44 void
46 std::vector<nlohmann::json>& elements,
47 Path& p)
48 {
49 elements = input.get<std::vector<nlohmann::json>>();
50 }
51
52 void
54 std::map<std::string, nlohmann::json>& elements,
55 Path& p)
56 {
57 elements = input.get<std::map<std::string, nlohmann::json>>();
58 }
59
60 const std::map<std::string, type::matrix::ElementType> ElementTypeAsString = {
61 {"unsigned char", ::armarx::aron::type::matrix::UINT8},
62 {"unsigned short", ::armarx::aron::type::matrix::UINT16},
63 {"unsigned int", ::armarx::aron::type::matrix::UINT32},
64 {"char", ::armarx::aron::type::matrix::INT8},
65 {"short", ::armarx::aron::type::matrix::INT16},
66 {"int", ::armarx::aron::type::matrix::INT32},
67 {"long", ::armarx::aron::type::matrix::INT64},
68 {"float", ::armarx::aron::type::matrix::FLOAT32},
69 {"double", ::armarx::aron::type::matrix::FLOAT64}};
70
71 template <typename T>
72 void
73 readTo(const nlohmann::json& input, std::vector<unsigned char>& data)
74 {
75 const std::vector<T> d = input.at("data").get<std::vector<T>>();
76
77 const std::size_t bufferLen = d.size() * sizeof(T);
78
79 data.resize(bufferLen);
80 memcpy(data.data(), d.data(), bufferLen);
81 }
82
83 void
85 std::vector<int>& shape,
86 std::string& typeAsString,
87 std::vector<unsigned char>& data,
88 Path& p)
89 {
90 shape = input.at("dims").get<std::vector<int>>();
91
92 typeAsString = input.at("type").get<std::string>();
93
94 ARMARX_CHECK(ElementTypeAsString.count(typeAsString) > 0)
95 << "Invalid element `" << typeAsString << "`. Valid elements are "
96 << simox::alg::get_keys(ElementTypeAsString) << ".";
97
98 const type::matrix::ElementType elementType = ElementTypeAsString.at(typeAsString);
99
100 // as we need to return a vector<uchar>, we first need to read the json array as the specific type
101 // and then convert it to the vector<uchar>
102 switch (elementType)
103 {
104 case type::matrix::UINT8:
106 break;
107 case type::matrix::UINT16:
109 break;
110 case type::matrix::UINT32:
112 break;
113 case type::matrix::INT8:
115 break;
116 case type::matrix::INT16:
118 break;
119 case type::matrix::INT32:
121 break;
122 case type::matrix::INT64:
124 break;
125 case type::matrix::FLOAT32:
127 break;
128 case type::matrix::FLOAT64:
130 break;
131 }
132
133 ARMARX_INFO << VAROUT(typeAsString);
134 }
135
136 void
137 NlohmannJSONReaderWithoutTypeCheck::readInt(const nlohmann::json& input, int& i, Path& p)
138 {
139 i = input;
140 }
141
142 void
143 NlohmannJSONReaderWithoutTypeCheck::readLong(const nlohmann::json& input, long& i, Path& p)
144 {
145 i = input;
146 }
147
148 void
149 NlohmannJSONReaderWithoutTypeCheck::readFloat(const nlohmann::json& input, float& i, Path& p)
150 {
151 i = input;
152 }
153
154 void
155 NlohmannJSONReaderWithoutTypeCheck::readDouble(const nlohmann::json& input, double& i, Path& p)
156 {
157 i = input;
158 }
159
160 void
162 std::string& i,
163 Path& p)
164 {
165 i = input;
166 }
167
168 void
169 NlohmannJSONReaderWithoutTypeCheck::readBool(const nlohmann::json& input, bool& i, Path& p)
170 {
171 i = input;
172 }
173
174} // namespace armarx::aron::data::reader
#define VAROUT(x)
The Path class.
Definition Path.h:36
void readNDArray(InputType &input, std::vector< int > &shape, std::string &typeAsString, std::vector< unsigned char > &data, Path &p) override
void readList(InputType &input, std::vector< InputTypeNonConst > &elements, Path &p) override
void readString(InputType &input, std::string &i, Path &p) override
void readDict(InputType &input, std::map< std::string, InputTypeNonConst > &elements, Path &p) override
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
void readTo(const nlohmann::json &input, std::vector< unsigned char > &data)
const std::map< std::string, type::matrix::ElementType > ElementTypeAsString
A convenience header to include all aron files (full include, not forward declared)
static data::Descriptor GetDescriptor(Input &n)