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
29 #include "../../json/Data.h"
32 
33 
35 {
36  namespace
37  {
38  /// Throw an exception if the type is other than expected
39  void getAronMetaInformationForType(const nlohmann::json& input, const std::string& expectedType, Path& p)
40  {
41  // check type
42  if (input.at(rw::json::constantes::TYPE_SLUG) != expectedType)
43  {
44  throw error::ValueNotValidException(__PRETTY_FUNCTION__, "Wrong type in json encountered.", input.at(rw::json::constantes::TYPE_SLUG), expectedType);
45  }
46 
47  // set path
48  std::vector<std::string> pathElements = input.at(rw::json::constantes::PATH_SLUG);
49  p = Path(pathElements);
50  }
51  }
52 
54  {
56  return ret;
57  }
58 
59  void NlohmannJSONReader::readList(const nlohmann::json& input, std::vector<nlohmann::json>& elements, Path& p)
60  {
61  getAronMetaInformationForType(input, rw::json::constantes::LIST_TYPENAME_SLUG, p);
62  elements = input.at(rw::json::constantes::ELEMENTS_SLUG).get<std::vector<nlohmann::json>>();
63  }
64 
65  void NlohmannJSONReader::readDict(const nlohmann::json& input, std::map<std::string, nlohmann::json>& elements, Path& p)
66  {
67  getAronMetaInformationForType(input, rw::json::constantes::DICT_TYPENAME_SLUG, p);
68  elements = input.at(rw::json::constantes::ELEMENTS_SLUG).get<std::map<std::string, nlohmann::json>>();
69  }
70 
71  void NlohmannJSONReader::readNDArray(const nlohmann::json& input, std::vector<int>& shape, std::string& typeAsString, std::vector<unsigned char>& data, Path& p)
72  {
73  getAronMetaInformationForType(input, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
74  shape = input.at(rw::json::constantes::DIMENSIONS_SLUG).get<std::vector<int>>();
76  data = input.at(rw::json::constantes::DATA_SLUG).get<std::vector<unsigned char>>();
77  }
78 
79  void NlohmannJSONReader::readInt(const nlohmann::json& input, int& i, Path& p)
80  {
81  getAronMetaInformationForType(input, rw::json::constantes::INT_TYPENAME_SLUG, p);
83  }
84 
85  void NlohmannJSONReader::readLong(const nlohmann::json& input, long& i, Path& p)
86  {
87  getAronMetaInformationForType(input, rw::json::constantes::LONG_TYPENAME_SLUG, p);
89  }
90 
91  void NlohmannJSONReader::readFloat(const nlohmann::json& input, float& i, Path& p)
92  {
93  getAronMetaInformationForType(input, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
95  }
96 
97  void NlohmannJSONReader::readDouble(const nlohmann::json& input, double& i, Path& p)
98  {
99  getAronMetaInformationForType(input, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
101  }
102 
103  void NlohmannJSONReader::readString(const nlohmann::json& input, std::string& i, Path& p)
104  {
105  getAronMetaInformationForType(input, rw::json::constantes::STRING_TYPENAME_SLUG, p);
107  }
108 
109  void NlohmannJSONReader::readBool(const nlohmann::json& input, bool& i, Path& p)
110  {
111  getAronMetaInformationForType(input, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
113  }
114 }
armarx::aron::data::reader::NlohmannJSONReader::readString
void readString(InputType &input, std::string &s, Path &p) override
armarx::aron::data::reader::NlohmannJSONReader::readFloat
void readFloat(InputType &input, float &i, Path &p) override
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::data::rw::json::constantes::PATH_SLUG
const std::string PATH_SLUG
Definition: Data.h:35
armarx::aron::data::ConstNlohmannJSONVisitor::GetDescriptor
static data::Descriptor GetDescriptor(Input &n)
Definition: NlohmannJSONVisitor.cpp:29
NlohmannJSONVisitor.h
armarx::aron::data::reader::NlohmannJSONReader::readDict
void readDict(InputType &input, std::map< std::string, InputTypeNonConst > &elements, Path &p) override
armarx::aron::data::rw::json::constantes::DOUBLE_TYPENAME_SLUG
const std::string DOUBLE_TYPENAME_SLUG
Definition: Data.h:54
armarx::aron::data::rw::json::constantes::INT_TYPENAME_SLUG
const std::string INT_TYPENAME_SLUG
Definition: Data.h:51
armarx::aron::data::ReaderInterface< const nlohmann::json >::InputType
const nlohmann::json InputType
Definition: Reader.h:39
armarx::aron::data::rw::json::constantes::VALUE_SLUG
const std::string VALUE_SLUG
Definition: Data.h:39
armarx::aron::data::rw::json::constantes::FLOAT_TYPENAME_SLUG
const std::string FLOAT_TYPENAME_SLUG
Definition: Data.h:53
armarx::aron::data::rw::json::constantes::DATA_SLUG
const std::string DATA_SLUG
Definition: Data.h:45
armarx::aron::data::rw::json::constantes::DICT_TYPENAME_SLUG
const std::string DICT_TYPENAME_SLUG
Definition: Data.h:49
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::reader::NlohmannJSONReader::getDescriptor
data::Descriptor getDescriptor(InputType &input) final
Definition: NlohmannJSONReader.cpp:53
armarx::aron::Path
The Path class.
Definition: Path.h:36
NlohmannJSONReader.h
armarx::aron::data::rw::json::constantes::NDARRAY_TYPENAME_SLUG
const std::string NDARRAY_TYPENAME_SLUG
Definition: Data.h:50
armarx::aron::data::rw::json::constantes::BOOL_TYPENAME_SLUG
const std::string BOOL_TYPENAME_SLUG
Definition: Data.h:56
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::data::rw::json::constantes::LIST_TYPENAME_SLUG
const std::string LIST_TYPENAME_SLUG
Definition: Data.h:48
armarx::aron::data::rw::json::constantes::DIMENSIONS_SLUG
const std::string DIMENSIONS_SLUG
Definition: Data.h:44
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::aron::data::reader::NlohmannJSONReader::readNDArray
void readNDArray(InputType &input, std::vector< int > &shape, std::string &typeAsString, std::vector< unsigned char > &data, Path &p) override
armarx::aron::data::rw::json::constantes::STRING_TYPENAME_SLUG
const std::string STRING_TYPENAME_SLUG
Definition: Data.h:55
armarx::aron::data::reader::NlohmannJSONReader::readLong
void readLong(InputType &input, long &i, Path &p) override
armarx::aron::data::rw::json::constantes::USED_TYPE_SLUG
const std::string USED_TYPE_SLUG
Definition: Data.h:46
armarx::aron::data::reader::NlohmannJSONReader::readDouble
void readDouble(InputType &input, double &i, Path &p) override
armarx::aron::data::reader::NlohmannJSONReader::readBool
void readBool(InputType &input, bool &i, Path &p) override
armarx::aron::data::reader::NlohmannJSONReader::readInt
void readInt(InputType &input, int &i, Path &p) override
Exception.h
armarx::aron::data::reader::NlohmannJSONReader::readList
void readList(InputType &input, std::vector< InputTypeNonConst > &elements, Path &p) override
armarx::aron::data::rw::json::constantes::TYPE_SLUG
const std::string TYPE_SLUG
Definition: Data.h:34
armarx::aron::data::rw::json::constantes::LONG_TYPENAME_SLUG
const std::string LONG_TYPENAME_SLUG
Definition: Data.h:52
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::aron::data::rw::json::constantes::ELEMENTS_SLUG
const std::string ELEMENTS_SLUG
Definition: Data.h:41