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 void
67 NlohmannJSONReader::readList(const nlohmann::json& input,
68 std::vector<nlohmann::json>& elements,
69 Path& p)
70 {
71 getAronMetaInformationForType(input, rw::json::constantes::LIST_TYPENAME_SLUG, p);
72 elements = input.at(rw::json::constantes::ELEMENTS_SLUG).get<std::vector<nlohmann::json>>();
73 }
74
75 void
76 NlohmannJSONReader::readDict(const nlohmann::json& input,
77 std::map<std::string, nlohmann::json>& elements,
78 Path& p)
79 {
80 getAronMetaInformationForType(input, rw::json::constantes::DICT_TYPENAME_SLUG, p);
81 elements = input.at(rw::json::constantes::ELEMENTS_SLUG)
82 .get<std::map<std::string, nlohmann::json>>();
83 }
84
85 void
86 NlohmannJSONReader::readNDArray(const nlohmann::json& input,
87 std::vector<int>& shape,
88 std::string& typeAsString,
89 std::vector<unsigned char>& data,
90 Path& p)
91 {
92 getAronMetaInformationForType(input, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
93 shape = input.at(rw::json::constantes::DIMENSIONS_SLUG).get<std::vector<int>>();
94 typeAsString = input.at(rw::json::constantes::USED_TYPE_SLUG);
95 data = input.at(rw::json::constantes::DATA_SLUG).get<std::vector<unsigned char>>();
96 }
97
98 void
99 NlohmannJSONReader::readInt(const nlohmann::json& input, int& i, Path& p)
100 {
101 getAronMetaInformationForType(input, rw::json::constantes::INT_TYPENAME_SLUG, p);
103 }
104
105 void
106 NlohmannJSONReader::readLong(const nlohmann::json& input, long& i, Path& p)
107 {
108 getAronMetaInformationForType(input, rw::json::constantes::LONG_TYPENAME_SLUG, p);
110 }
111
112 void
113 NlohmannJSONReader::readFloat(const nlohmann::json& input, float& i, Path& p)
114 {
115 getAronMetaInformationForType(input, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
117 }
118
119 void
120 NlohmannJSONReader::readDouble(const nlohmann::json& input, double& i, Path& p)
121 {
122 getAronMetaInformationForType(input, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
124 }
125
126 void
127 NlohmannJSONReader::readString(const nlohmann::json& input, std::string& i, Path& p)
128 {
129 getAronMetaInformationForType(input, rw::json::constantes::STRING_TYPENAME_SLUG, p);
131 }
132
133 void
134 NlohmannJSONReader::readBool(const nlohmann::json& input, bool& i, Path& p)
135 {
136 getAronMetaInformationForType(input, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
138 }
139} // namespace armarx::aron::data::reader
The Path class.
Definition Path.h:36
void readString(InputType &input, std::string &s, Path &p) override
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:53
const std::string INT_TYPENAME_SLUG
Definition Data.h:51
const std::string LIST_TYPENAME_SLUG
Definition Data.h:48
const std::string DOUBLE_TYPENAME_SLUG
Definition Data.h:54
const std::string NDARRAY_TYPENAME_SLUG
Definition Data.h:50
const std::string DIMENSIONS_SLUG
Definition Data.h:44
const std::string BOOL_TYPENAME_SLUG
Definition Data.h:56
const std::string STRING_TYPENAME_SLUG
Definition Data.h:55
const std::string DICT_TYPENAME_SLUG
Definition Data.h:49
const std::string LONG_TYPENAME_SLUG
Definition Data.h:52
A convenience header to include all aron files (full include, not forward declared)
static data::Descriptor GetDescriptor(Input &n)