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 nlohmann::json
56 NlohmannJSONWriter::writeList(const std::vector<nlohmann::json>& elements, const Path& p)
57 {
58 nlohmann::json o;
59 setupAronMetaInformationForType(o, rw::json::constantes::LIST_TYPENAME_SLUG, p);
61 return o;
62 }
63
64 nlohmann::json
65 NlohmannJSONWriter::writeDict(const std::map<std::string, nlohmann::json>& elements,
66 const std::optional<nlohmann::json>& extends,
67 const Path& p)
68 {
69 auto o = extends ? extends.value() : nlohmann::json();
70
71 setupAronMetaInformationForType(o, rw::json::constantes::DICT_TYPENAME_SLUG, p);
73 return o;
74 }
75
76 nlohmann::json
77 NlohmannJSONWriter::writeNDArray(const std::vector<int>& shape,
78 const std::string& typeAsString,
79 const unsigned char* data,
80 const Path& p)
81 {
82 nlohmann::json o;
83 setupAronMetaInformationForType(o, rw::json::constantes::NDARRAY_TYPENAME_SLUG, p);
86
87 int elements =
88 shape.empty()
89 ? 0
90 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
91 std::vector<unsigned char> d = std::vector<unsigned char>(elements);
92 memcpy(d.data(), data, elements);
94 return o;
95 }
96
97 nlohmann::json
98 NlohmannJSONWriter::writeInt(const int i, const Path& p)
99 {
100 nlohmann::json o;
101 setupAronMetaInformationForType(o, rw::json::constantes::INT_TYPENAME_SLUG, p);
103 return o;
104 }
105
106 nlohmann::json
107 NlohmannJSONWriter::writeLong(const long i, const Path& p)
108 {
109 nlohmann::json o;
110 setupAronMetaInformationForType(o, rw::json::constantes::LONG_TYPENAME_SLUG, p);
112 return o;
113 }
114
115 nlohmann::json
116 NlohmannJSONWriter::writeFloat(const float i, const Path& p)
117 {
118 nlohmann::json o;
119 setupAronMetaInformationForType(o, rw::json::constantes::FLOAT_TYPENAME_SLUG, p);
121 return o;
122 }
123
124 nlohmann::json
125 NlohmannJSONWriter::writeDouble(const double i, const Path& p)
126 {
127 nlohmann::json o;
128 setupAronMetaInformationForType(o, rw::json::constantes::DOUBLE_TYPENAME_SLUG, p);
130 return o;
131 }
132
133 nlohmann::json
134 NlohmannJSONWriter::writeString(const std::string& i, const Path& p)
135 {
136 nlohmann::json o;
137 setupAronMetaInformationForType(o, rw::json::constantes::STRING_TYPENAME_SLUG, p);
139 return o;
140 }
141
142 nlohmann::json
143 NlohmannJSONWriter::writeBool(const bool i, const Path& p)
144 {
145 nlohmann::json o;
146 setupAronMetaInformationForType(o, rw::json::constantes::BOOL_TYPENAME_SLUG, p);
148 return o;
149 }
150} // 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
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: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)
A convenience header to include all aron files (full include, not forward declared)
static data::Descriptor GetDescriptor(Input &n)