NlohmannJSONWriter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Fabian Peller (fabian dot peller at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <numeric>
25 
26 // Header
27 #include "NlohmannJSONWriter.h"
28 
29 // ArmarX
30 #include "../../json/Data.h"
32 
34 {
35  namespace
36  {
37  /// Set important members for json object (aron meta information)
38  void setupAronMetaInformationForType(nlohmann::json& json, const std::string& type, const Path& p)
39  {
42  }
43  }
44 
46  {
48  }
49 
50  nlohmann::json NlohmannJSONWriter::writeList(const std::vector<nlohmann::json>& elements, const Path& p)
51  {
52  nlohmann::json o;
53  setupAronMetaInformationForType(o, rw::json::constantes::LIST_TYPENAME_SLUG, p);
55  return o;
56  }
57 
58  nlohmann::json NlohmannJSONWriter::writeDict(const std::map<std::string, nlohmann::json>& elements, const std::optional<nlohmann::json>& extends, const Path& p)
59  {
60  auto o = extends ? extends.value() : nlohmann::json();
61 
62  setupAronMetaInformationForType(o, rw::json::constantes::DICT_TYPENAME_SLUG, p);
64  return o;
65  }
66 
67  nlohmann::json NlohmannJSONWriter::writeNDArray(const std::vector<int>& shape, const std::string& typeAsString, const unsigned char* data, const Path& p)
68  {
69  nlohmann::json o;
70  setupAronMetaInformationForType(o, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
72  o[rw::json::constantes::USED_TYPE_SLUG] = typeAsString;
73 
74  int elements = shape.empty() ? 0 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
75  std::vector<unsigned char> d = std::vector<unsigned char>(elements);
76  memcpy(d.data(), data, elements);
78  return o;
79  }
80 
81  nlohmann::json NlohmannJSONWriter::writeInt(const int i, const Path& p)
82  {
83  nlohmann::json o;
84  setupAronMetaInformationForType(o, rw::json::constantes::INT_TYPENAME_SLUG, p);
86  return o;
87  }
88 
89  nlohmann::json NlohmannJSONWriter::writeLong(const long i, const Path& p)
90  {
91  nlohmann::json o;
92  setupAronMetaInformationForType(o, rw::json::constantes::LONG_TYPENAME_SLUG, p);
94  return o;
95  }
96 
97  nlohmann::json NlohmannJSONWriter::writeFloat(const float i, const Path& p)
98  {
99  nlohmann::json o;
100  setupAronMetaInformationForType(o, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
102  return o;
103  }
104 
105  nlohmann::json NlohmannJSONWriter::writeDouble(const double i, const Path& p)
106  {
107  nlohmann::json o;
108  setupAronMetaInformationForType(o, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
110  return o;
111  }
112 
113  nlohmann::json NlohmannJSONWriter::writeString(const std::string& i, const Path& p)
114  {
115  nlohmann::json o;
116  setupAronMetaInformationForType(o, rw::json::constantes::STRING_TYPENAME_SLUG, p);
118  return o;
119  }
120 
121  nlohmann::json NlohmannJSONWriter::writeBool(const bool i, const Path& p)
122  {
123  nlohmann::json o;
124  setupAronMetaInformationForType(o, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
126  return o;
127  }
128 }
armarx::aron::data::writer::NlohmannJSONWriter::writeInt
nlohmann::json writeInt(const int i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:81
NlohmannJSONWriter.h
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::writer::NlohmannJSONWriter::getDescriptor
data::Descriptor getDescriptor(ReturnTypeConst &input) final
Definition: NlohmannJSONWriter.cpp:45
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::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::writer::NlohmannJSONWriter::writeString
nlohmann::json writeString(const std::string &i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:113
armarx::aron::data::writer::NlohmannJSONWriter::writeFloat
nlohmann::json writeFloat(const float i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:97
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::writer::NlohmannJSONWriter::writeNDArray
nlohmann::json writeNDArray(const std::vector< int > &shape, const std::string &typeAsString, const unsigned char *data, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:67
armarx::aron::data::writer::NlohmannJSONWriter::writeBool
nlohmann::json writeBool(const bool i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:121
armarx::aron::data::writer::NlohmannJSONWriter::writeDouble
nlohmann::json writeDouble(const double i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:105
armarx::aron::Path
The Path class.
Definition: Path.h:36
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::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
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::aron::data::rw::json::constantes::STRING_TYPENAME_SLUG
const std::string STRING_TYPENAME_SLUG
Definition: Data.h:55
armarx::aron::data::writer::NlohmannJSONWriter::writeDict
nlohmann::json writeDict(const std::map< std::string, nlohmann::json > &elements, const std::optional< nlohmann::json > &extends=std::nullopt, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:58
armarx::aron::data::rw::json::constantes::USED_TYPE_SLUG
const std::string USED_TYPE_SLUG
Definition: Data.h:46
armarx::aron::data::writer
Definition: NlohmannJSONWriter.cpp:33
armarx::aron::data::writer::NlohmannJSONWriter::writeLong
nlohmann::json writeLong(const long i, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:89
armarx::aron::data::WriterInterface< nlohmann::json >::ReturnTypeConst
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition: Writer.h:42
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::aron::data::rw::json::constantes::ELEMENTS_SLUG
const std::string ELEMENTS_SLUG
Definition: Data.h:41
armarx::aron::data::writer::NlohmannJSONWriter::writeList
nlohmann::json writeList(const std::vector< nlohmann::json > &elements, const Path &p=Path()) override
Definition: NlohmannJSONWriter.cpp:50
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:85