JSONObject.h
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* @package ArmarX::Core
17* @author ALexey Kozlov ( kozlov at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#pragma once
24
25#include <ArmarXCore/interface/observers/VariantBase.h>
26#include <ArmarXCore/interface/serialization/JSONSerialization.h>
28
29#include <jsoncpp/json/json.h>
30
31namespace armarx
32{
33 class JSONObject;
35
36 /**
37 * @brief The JSONObject class is used to represent and (de)serialize JSON objects.
38 *
39 * Accessing the object is not thread safe.
40 * (De)Serializing multiple JSON objects should be done with a new instance for
41 * each object.
42 */
44 {
45 public:
49 JSONObject(const JSONObject& toCopy);
50 ~JSONObject() override;
51
52 public: // ObjectSerializerBase methods implementation
53 std::string toString(const ::Ice::Current& = Ice::emptyCurrent) const override;
54 void fromString(const ::std::string& jsonString,
55 const ::Ice::Current& = Ice::emptyCurrent) override;
56 const Json::Value& getJsonValue() const;
57
58 public: //
60 getElementType(const ::Ice::Current& = Ice::emptyCurrent) const override;
62 const ::Ice::Current& = Ice::emptyCurrent) override;
63
64 unsigned int size() const override;
65 bool hasElement(const ::std::string& key) const override;
66
67 float getFloat(const ::std::string& key) const override;
68 double getDouble(const ::std::string& key) const override;
69 int getInt(const ::std::string& key) const override;
70 bool getBool(const ::std::string& key) const override;
71 std::string getString(const ::std::string& key) const override;
72 armarx::AbstractObjectSerializerPtr getElement(unsigned int index) const override;
73 armarx::AbstractObjectSerializerPtr getElement(const ::std::string& key) const override;
74 std::vector<std::string> getElementNames() const override;
75
76 template <typename T>
77 void
78 getArray(const std::string& key, std::vector<T>& result)
79 {
80 const Json::Value& arrValue = getValue(key);
81
82 if (!arrValue.isArray())
83 {
84 throw JSONInvalidDataTypeException();
85 }
86
87 result.clear();
88 result.resize(arrValue.size());
89
90 for (size_t i = 0; i < arrValue.size(); ++i)
91 {
92 get(arrValue[int(i)], result[int(i)]);
93 }
94 }
95
96 void
97 getIntArray(const ::std::string& key, std::vector<int>& result) override
98 {
99 getArray(key, result);
100 }
101
102 void
103 getFloatArray(const ::std::string& key, std::vector<float>& result) override
104 {
105 getArray(key, result);
106 }
107
108 void
109 getDoubleArray(const ::std::string& key, std::vector<double>& result) override
110 {
111 getArray(key, result);
112 }
113
114 void
115 getStringArray(const ::std::string& key, std::vector<std::string>& result) override
116 {
117 getArray(key, result);
118 }
119
120 void getVariantArray(const ::std::string& key,
121 std::vector<armarx::VariantPtr>& result) override;
122 void getVariantMap(const ::std::string& key, armarx::StringVariantBaseMap& result) override;
123
124 template <typename T>
125 void
126 set(const ::std::string& key, T val)
127 {
128 jsonValue[key] = Json::Value(val);
129 }
130
131 template <typename T>
132 void
133 set(int index, T val)
134 {
135 jsonValue[index] = Json::Value(val);
136 }
137
138 template <typename T>
139 void
140 setArray(const ::std::string& key, const std::vector<T>& val)
141 {
142 Json::Value arr(Json::arrayValue);
143
144 if (val.size() > 0)
145 {
146 arr.resize(val.size());
147
148 for (size_t i = 0; i < val.size(); ++i)
149 {
150 arr[int(i)] = Json::Value(val[int(i)]);
151 }
152 }
153
154 jsonValue[key] = arr;
155 }
156
157 void
158 setBool(const ::std::string& key, bool val) override
159 {
160 set(key, val);
161 }
162
163 void
164 setInt(const ::std::string& key, int val) override
165 {
166 set(key, val);
167 }
168
169 void
170 setFloat(const ::std::string& key, float val) override
171 {
172 set(key, val);
173 }
174
175 void
176 setDouble(const ::std::string& key, double val) override
177 {
178 set(key, val);
179 }
180
181 void
182 setString(const ::std::string& key, const std::string& val) override
183 {
184 set(key, val);
185 }
186
187 void setElement(const ::std::string& key,
188 const armarx::AbstractObjectSerializerPtr& obj) override;
189
190 void
191 setIntArray(const ::std::string& key, const std::vector<int>& val) override
192 {
193 setArray(key, val);
194 }
195
196 void
197 setFloatArray(const ::std::string& key, const std::vector<float>& val) override
198 {
199 setArray(key, val);
200 }
201
202 void
203 setDoubleArray(const ::std::string& key, const std::vector<double>& val) override
204 {
205 setArray(key, val);
206 }
207
208 void
209 setStringArray(const ::std::string& key, const std::vector<std::string>& val) override
210 {
211 setArray(key, val);
212 }
213
214 void setVariantArray(const ::std::string& key,
215 const std::vector<armarx::VariantBasePtr>& val) override;
216 void setVariantArray(const ::std::string& key,
217 const std::vector<armarx::VariantPtr>& val) override;
218 void setVariantMap(const ::std::string& key,
219 const armarx::StringVariantBaseMap& val) override;
220
221 void
222 setBool(unsigned int index, bool val) override
223 {
224 set(index, val);
225 }
226
227 void
228 setInt(unsigned int index, int val) override
229 {
230 set(index, val);
231 }
232
233 void
234 setFloat(unsigned int index, float val) override
235 {
236 set(index, val);
237 }
238
239 void
240 setDouble(unsigned int index, double val) override
241 {
242 set(index, val);
243 }
244
245 void
246 setString(unsigned int index, const std::string& val) override
247 {
248 set(index, val);
249 }
250
251 void setElement(unsigned int index,
252 const armarx::AbstractObjectSerializerPtr& obj) override;
253
254 // add all elements of val to json as siblings
255 void merge(const armarx::AbstractObjectSerializerPtr& val) override;
256
257 void append(const armarx::AbstractObjectSerializerPtr& val) override;
258
259 void reset() override;
260
262 const VariantBasePtr& variant);
263
264 public:
266
267 std::string asString(bool pretty = false) const;
268
269 private:
270 Json::Value jsonValue;
271
272 const Json::Value& getValue(const std::string& key) const;
273
274 void get(const Json::Value& val, bool& result) const;
275 void get(const Json::Value& val, int& result) const;
276 void get(const Json::Value& val, float& result) const;
277 void get(const Json::Value& val, double& result) const;
278 void get(const Json::Value& val, std::string& result) const;
279 };
280
281} // namespace armarx
uint8_t index
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition JSONObject.h:44
std::vector< std::string > getElementNames() const override
void setVariantArray(const ::std::string &key, const std::vector< armarx::VariantBasePtr > &val) override
void setDoubleArray(const ::std::string &key, const std::vector< double > &val) override
Definition JSONObject.h:203
unsigned int size() const override
void getDoubleArray(const ::std::string &key, std::vector< double > &result) override
Definition JSONObject.h:109
void setString(unsigned int index, const std::string &val) override
Definition JSONObject.h:246
void setVariantArray(const ::std::string &key, const std::vector< armarx::VariantPtr > &val) override
std::string toString(const ::Ice::Current &=Ice::emptyCurrent) const override
void getArray(const std::string &key, std::vector< T > &result)
Definition JSONObject.h:78
double getDouble(const ::std::string &key) const override
void getStringArray(const ::std::string &key, std::vector< std::string > &result) override
Definition JSONObject.h:115
std::string asString(bool pretty=false) const
const Json::Value & getJsonValue() const
float getFloat(const ::std::string &key) const override
void setArray(const ::std::string &key, const std::vector< T > &val)
Definition JSONObject.h:140
armarx::ElementType getElementType(const ::Ice::Current &=Ice::emptyCurrent) const override
std::string getString(const ::std::string &key) const override
void setInt(unsigned int index, int val) override
Definition JSONObject.h:228
armarx::AbstractObjectSerializerPtr getElement(const ::std::string &key) const override
void setDouble(const ::std::string &key, double val) override
Definition JSONObject.h:176
void setString(const ::std::string &key, const std::string &val) override
Definition JSONObject.h:182
void getFloatArray(const ::std::string &key, std::vector< float > &result) override
Definition JSONObject.h:103
void setDouble(unsigned int index, double val) override
Definition JSONObject.h:240
void setElement(const ::std::string &key, const armarx::AbstractObjectSerializerPtr &obj) override
int getInt(const ::std::string &key) const override
JSONObject(armarx::ElementType nodeType=armarx::ElementTypes::eObject, const Ice::CommunicatorPtr ic=Ice::CommunicatorPtr())
bool hasElement(const ::std::string &key) const override
armarx::AbstractObjectSerializerPtr createElement() const override
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
void set(int index, T val)
Definition JSONObject.h:133
void setStringArray(const ::std::string &key, const std::vector< std::string > &val) override
Definition JSONObject.h:209
void setBool(const ::std::string &key, bool val) override
Definition JSONObject.h:158
void setFloat(unsigned int index, float val) override
Definition JSONObject.h:234
static StringVariantBaseMap ConvertToBasicVariantMap(const JSONObjectPtr &serializer, const VariantBasePtr &variant)
bool getBool(const ::std::string &key) const override
void setInt(const ::std::string &key, int val) override
Definition JSONObject.h:164
void getVariantArray(const ::std::string &key, std::vector< armarx::VariantPtr > &result) override
void set(const ::std::string &key, T val)
Definition JSONObject.h:126
void getIntArray(const ::std::string &key, std::vector< int > &result) override
Definition JSONObject.h:97
void setBool(unsigned int index, bool val) override
Definition JSONObject.h:222
void merge(const armarx::AbstractObjectSerializerPtr &val) override
void setIntArray(const ::std::string &key, const std::vector< int > &val) override
Definition JSONObject.h:191
armarx::AbstractObjectSerializerPtr getElement(unsigned int index) const override
void setFloat(const ::std::string &key, float val) override
Definition JSONObject.h:170
~JSONObject() override
void getVariantMap(const ::std::string &key, armarx::StringVariantBaseMap &result) override
void setVariantMap(const ::std::string &key, const armarx::StringVariantBaseMap &val) override
void setFloatArray(const ::std::string &key, const std::vector< float > &val) override
Definition JSONObject.h:197
void reset() override
void append(const armarx::AbstractObjectSerializerPtr &val) override
void setElementType(armarx::ElementType nodeType, const ::Ice::Current &=Ice::emptyCurrent) override
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr
ElementTypes::ElementType ElementType
IceInternal::Handle< JSONObject > JSONObjectPtr
Definition JSONObject.h:34
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr