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
31
32#include "../../json/Data.h"
33
35{
36 namespace
37 {
38 /// Set important members for json object (aron meta information)
39 void
40 setupAronMetaInformationForType(nlohmann::json& json,
41 const std::string& type,
42 const Path& p)
43 {
46 }
47 } // namespace
48
54
55 void
57 const std::optional<float>& importance)
58 {
59 if (importance)
60 {
61 output[rw::json::constantes::IMPORTANCE_SLUG] = *importance;
62 }
63 }
64
65 nlohmann::json
66 NlohmannJSONWriter::writeList(const std::vector<nlohmann::json>& elements, const Path& p)
67 {
68 nlohmann::json o;
69 setupAronMetaInformationForType(o, rw::json::constantes::LIST_TYPENAME_SLUG, p);
71 return o;
72 }
73
74 nlohmann::json
75 NlohmannJSONWriter::writeDict(const std::map<std::string, nlohmann::json>& elements,
76 const std::optional<nlohmann::json>& extends,
77 const Path& p)
78 {
79 auto o = extends ? extends.value() : nlohmann::json();
80
81 setupAronMetaInformationForType(o, rw::json::constantes::DICT_TYPENAME_SLUG, p);
83 return o;
84 }
85
86 nlohmann::json
87 NlohmannJSONWriter::writeNDArray(const std::vector<int>& shape,
88 const std::string& typeAsString,
89 const unsigned char* data,
90 const Path& p)
91 {
92 nlohmann::json o;
93 setupAronMetaInformationForType(o, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
96
97 int elements =
98 shape.empty()
99 ? 0
100 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
101 std::vector<unsigned char> d = std::vector<unsigned char>(elements);
102 memcpy(d.data(), data, elements);
104 return o;
105 }
106
107 nlohmann::json
108 NlohmannJSONWriter::writeInt(const int i, const Path& p)
109 {
110 nlohmann::json o;
111 setupAronMetaInformationForType(o, rw::json::constantes::INT_TYPENAME_SLUG, p);
113 return o;
114 }
115
116 nlohmann::json
117 NlohmannJSONWriter::writeLong(const long i, const Path& p)
118 {
119 nlohmann::json o;
120 setupAronMetaInformationForType(o, rw::json::constantes::LONG_TYPENAME_SLUG, p);
122 return o;
123 }
124
125 nlohmann::json
126 NlohmannJSONWriter::writeFloat(const float i, const Path& p)
127 {
128 nlohmann::json o;
129 setupAronMetaInformationForType(o, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
131 return o;
132 }
133
134 nlohmann::json
135 NlohmannJSONWriter::writeDouble(const double i, const Path& p)
136 {
137 nlohmann::json o;
138 setupAronMetaInformationForType(o, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
140 return o;
141 }
142
143 nlohmann::json
144 NlohmannJSONWriter::writeString(const std::string& i, const Path& p)
145 {
146 nlohmann::json o;
147 setupAronMetaInformationForType(o, rw::json::constantes::STRING_TYPENAME_SLUG, p);
149 return o;
150 }
151
152 nlohmann::json
153 NlohmannJSONWriter::writeBool(const bool i, const Path& p)
154 {
155 nlohmann::json o;
156 setupAronMetaInformationForType(o, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
158 return o;
159 }
160} // namespace armarx::aron::data::writer
The Path class.
Definition Path.h:36
std::vector< std::string > getPath() const
Definition Path.cpp:87
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition Writer.h:42
nlohmann::json writeLong(const long i, const Path &p=Path()) override
nlohmann::json writeInt(const int i, const Path &p=Path()) override
nlohmann::json writeNDArray(const std::vector< int > &shape, const std::string &typeAsString, const unsigned char *data, const Path &p=Path()) override
nlohmann::json writeString(const std::string &i, const Path &p=Path()) override
void writeImportance(ReturnType &output, const std::optional< float > &importance) final
nlohmann::json writeList(const std::vector< nlohmann::json > &elements, const Path &p=Path()) override
nlohmann::json writeDict(const std::map< std::string, nlohmann::json > &elements, const std::optional< nlohmann::json > &extends=std::nullopt, const Path &p=Path()) override
nlohmann::json writeDouble(const double i, const Path &p=Path()) override
nlohmann::json writeBool(const bool i, const Path &p=Path()) override
data::Descriptor getDescriptor(ReturnTypeConst &input) final
nlohmann::json writeFloat(const float i, const Path &p=Path()) override
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)
A convenience header to include all aron files (full include, not forward declared)
static data::Descriptor GetDescriptor(Input &n)