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 // check type
45 if (input.at(rw::json::constantes::TYPE_SLUG) != expectedType)
46 {
47 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
48 "Wrong type in json encountered.",
50 expectedType);
51 }
52
53 // set path
54 std::vector<std::string> pathElements = input.at(rw::json::constantes::PATH_SLUG);
55 p = Path(pathElements);
56 }
57 } // namespace
58
61 {
63 return ret;
64 }
65
66 std::optional<float>
68 {
69 if (!input.contains(rw::json::constantes::IMPORTANCE_SLUG))
70 {
71 return std::nullopt;
72 }
73 return input.at(rw::json::constantes::IMPORTANCE_SLUG).get<float>();
74 }
75
76 void
77 NlohmannJSONReader::readList(const nlohmann::json& input,
78 std::vector<nlohmann::json>& elements,
79 Path& p)
80 {
81 getAronMetaInformationForType(input, rw::json::constantes::LIST_TYPENAME_SLUG, p);
82 elements = input.at(rw::json::constantes::ELEMENTS_SLUG).get<std::vector<nlohmann::json>>();
83 }
84
85 void
86 NlohmannJSONReader::readDict(const nlohmann::json& input,
87 std::map<std::string, nlohmann::json>& elements,
88 Path& p)
89 {
90 getAronMetaInformationForType(input, rw::json::constantes::DICT_TYPENAME_SLUG, p);
91 elements = input.at(rw::json::constantes::ELEMENTS_SLUG)
92 .get<std::map<std::string, nlohmann::json>>();
93 }
94
95 void
96 NlohmannJSONReader::readNDArray(const nlohmann::json& input,
97 std::vector<int>& shape,
98 std::string& typeAsString,
99 std::vector<unsigned char>& data,
100 Path& p)
101 {
102 getAronMetaInformationForType(input, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
103 shape = input.at(rw::json::constantes::DIMENSIONS_SLUG).get<std::vector<int>>();
104 typeAsString = input.at(rw::json::constantes::USED_TYPE_SLUG);
105 data = input.at(rw::json::constantes::DATA_SLUG).get<std::vector<unsigned char>>();
106 }
107
108 void
109 NlohmannJSONReader::readInt(const nlohmann::json& input, int& i, Path& p)
110 {
111 getAronMetaInformationForType(input, rw::json::constantes::INT_TYPENAME_SLUG, p);
113 }
114
115 void
116 NlohmannJSONReader::readLong(const nlohmann::json& input, long& i, Path& p)
117 {
118 getAronMetaInformationForType(input, rw::json::constantes::LONG_TYPENAME_SLUG, p);
120 }
121
122 void
123 NlohmannJSONReader::readFloat(const nlohmann::json& input, float& i, Path& p)
124 {
125 getAronMetaInformationForType(input, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
127 }
128
129 void
130 NlohmannJSONReader::readDouble(const nlohmann::json& input, double& i, Path& p)
131 {
132 getAronMetaInformationForType(input, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
134 }
135
136 void
137 NlohmannJSONReader::readString(const nlohmann::json& input, std::string& i, Path& p)
138 {
139 getAronMetaInformationForType(input, rw::json::constantes::STRING_TYPENAME_SLUG, p);
141 }
142
143 void
144 NlohmannJSONReader::readBool(const nlohmann::json& input, bool& i, Path& p)
145 {
146 getAronMetaInformationForType(input, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
148 }
149} // namespace armarx::aron::data::reader
The Path class.
Definition Path.h:36
void readString(InputType &input, std::string &s, Path &p) override
std::optional< float > readImportance(InputType &input) final
void readBool(InputType &input, bool &i, Path &p) override
void readFloat(InputType &input, float &i, Path &p) override
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 readLong(InputType &input, long &i, Path &p) override
data::Descriptor getDescriptor(InputType &input) final
void readDict(InputType &input, std::map< std::string, InputTypeNonConst > &elements, Path &p) override
void readDouble(InputType &input, double &i, Path &p) override
void readInt(InputType &input, int &i, Path &p) override
The ValueNotValidException class.
Definition Exception.h:99
const std::string FLOAT_TYPENAME_SLUG
Definition Data.h:54
const std::string INT_TYPENAME_SLUG
Definition Data.h:52
const std::string LIST_TYPENAME_SLUG
Definition Data.h:49
const std::string IMPORTANCE_SLUG
Definition Data.h:36
const std::string DOUBLE_TYPENAME_SLUG
Definition Data.h:55
const std::string NDARRAY_TYPENAME_SLUG
Definition Data.h:51
const std::string DIMENSIONS_SLUG
Definition Data.h:45
const std::string BOOL_TYPENAME_SLUG
Definition Data.h:57
const std::string STRING_TYPENAME_SLUG
Definition Data.h:56
const std::string DICT_TYPENAME_SLUG
Definition Data.h:50
const std::string LONG_TYPENAME_SLUG
Definition Data.h:53
A convenience header to include all aron files (full include, not forward declared)
static data::Descriptor GetDescriptor(Input &n)