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>
27 #include <ArmarXCore/interface/serialization/JSONSerialization.h>
28 
29 #include <jsoncpp/json/json.h>
30 
31 namespace 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  */
43  class JSONObject :
45  {
46  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, const ::Ice::Current& = Ice::emptyCurrent) override;
55  const Json::Value& getJsonValue() const;
56  public: //
57  armarx::ElementType getElementType(const ::Ice::Current& = Ice::emptyCurrent) const override;
58  void setElementType(armarx::ElementType nodeType, const ::Ice::Current& = Ice::emptyCurrent) override;
59 
60  unsigned int size() const override;
61  bool hasElement(const ::std::string& key) const override;
62 
63  float getFloat(const ::std::string& key) const override;
64  double getDouble(const ::std::string& key) const override;
65  int getInt(const ::std::string& key) const override;
66  bool getBool(const ::std::string& key) const override;
67  std::string getString(const ::std::string& key) const override;
68  armarx::AbstractObjectSerializerPtr getElement(unsigned int index) const override;
69  armarx::AbstractObjectSerializerPtr getElement(const ::std::string& key) const override;
70  std::vector<std::string> getElementNames() const override;
71 
72  template <typename T>
73  void getArray(const std::string& key, std::vector<T>& result)
74  {
75  const Json::Value& arrValue = getValue(key);
76 
77  if (!arrValue.isArray())
78  {
79  throw JSONInvalidDataTypeException();
80  }
81 
82  result.clear();
83  result.resize(arrValue.size());
84 
85  for (size_t i = 0; i < arrValue.size(); ++i)
86  {
87  get(arrValue[int(i)], result[int(i)]);
88  }
89  }
90 
91  void getIntArray(const ::std::string& key, std::vector<int>& result) override
92  {
93  getArray(key, result);
94  }
95  void getFloatArray(const ::std::string& key, std::vector<float>& result) override
96  {
97  getArray(key, result);
98  }
99  void getDoubleArray(const ::std::string& key, std::vector<double>& result) override
100  {
101  getArray(key, result);
102  }
103  void getStringArray(const ::std::string& key, std::vector<std::string>& result) override
104  {
105  getArray(key, result);
106  }
107  void getVariantArray(const ::std::string& key, std::vector<armarx::VariantPtr>& result) override;
108  void getVariantMap(const ::std::string& key, armarx::StringVariantBaseMap& result) override;
109 
110  template <typename T>
111  void set(const ::std::string& key, T val)
112  {
113  jsonValue[key] = Json::Value(val);
114  }
115 
116  template <typename T>
117  void set(int index, T val)
118  {
119  jsonValue[index] = Json::Value(val);
120  }
121 
122  template <typename T>
123  void setArray(const ::std::string& key, const std::vector<T>& val)
124  {
125  Json::Value arr(Json::arrayValue);
126 
127  if (val.size() > 0)
128  {
129  arr.resize(val.size());
130 
131  for (size_t i = 0; i < val.size(); ++i)
132  {
133  arr[int(i)] = Json::Value(val[int(i)]);
134  }
135  }
136 
137  jsonValue[key] = arr;
138  }
139 
140  void setBool(const ::std::string& key, bool val) override
141  {
142  set(key, val);
143  }
144  void setInt(const ::std::string& key, int val) override
145  {
146  set(key, val);
147  }
148  void setFloat(const ::std::string& key, float val) override
149  {
150  set(key, val);
151  }
152  void setDouble(const ::std::string& key, double val) override
153  {
154  set(key, val);
155  }
156  void setString(const ::std::string& key, const std::string& val) override
157  {
158  set(key, val);
159  }
160  void setElement(const ::std::string& key, const armarx::AbstractObjectSerializerPtr& obj) override;
161 
162  void setIntArray(const ::std::string& key, const std::vector<int>& val) override
163  {
164  setArray(key, val);
165  }
166  void setFloatArray(const ::std::string& key, const std::vector<float>& val) override
167  {
168  setArray(key, val);
169  }
170  void setDoubleArray(const ::std::string& key, const std::vector<double>& val) override
171  {
172  setArray(key, val);
173  }
174  void setStringArray(const ::std::string& key, const std::vector<std::string>& val) override
175  {
176  setArray(key, val);
177  }
178  void setVariantArray(const ::std::string& key, const std::vector<armarx::VariantBasePtr>& val) override;
179  void setVariantArray(const ::std::string& key, const std::vector<armarx::VariantPtr>& val) override;
180  void setVariantMap(const ::std::string& key, const armarx::StringVariantBaseMap& val) override;
181 
182  void setBool(unsigned int index, bool val) override
183  {
184  set(index, val);
185  }
186  void setInt(unsigned int index, int val) override
187  {
188  set(index, val);
189  }
190  void setFloat(unsigned int index, float val) override
191  {
192  set(index, val);
193  }
194  void setDouble(unsigned int index, double val) override
195  {
196  set(index, val);
197  }
198  void setString(unsigned int index, const std::string& val) override
199  {
200  set(index, val);
201  }
202  void setElement(unsigned int index, const armarx::AbstractObjectSerializerPtr& obj) override;
203 
204  // add all elements of val to json as siblings
205  void merge(const armarx::AbstractObjectSerializerPtr& val) override;
206 
207  void append(const armarx::AbstractObjectSerializerPtr& val) override;
208 
209  void reset() override;
210 
211  static StringVariantBaseMap ConvertToBasicVariantMap(const JSONObjectPtr& serializer, const VariantBasePtr& variant);
212 
213  public:
215 
216  std::string asString(bool pretty = false) const;
217 
218  private:
219  Json::Value jsonValue;
220 
221  const Json::Value& getValue(const std::string& key) const;
222 
223  void get(const Json::Value& val, bool& result) const;
224  void get(const Json::Value& val, int& result) const;
225  void get(const Json::Value& val, float& result) const;
226  void get(const Json::Value& val, double& result) const;
227  void get(const Json::Value& val, std::string& result) const;
228 
229  };
230 
231 }
232 
armarx::JSONObject::setBool
void setBool(const ::std::string &key, bool val) override
Definition: JSONObject.h:140
armarx::JSONObject::setString
void setString(unsigned int index, const std::string &val) override
Definition: JSONObject.h:198
armarx::JSONObject::getElement
armarx::AbstractObjectSerializerPtr getElement(unsigned int index) const override
Definition: JSONObject.cpp:394
armarx::JSONObject::hasElement
bool hasElement(const ::std::string &key) const override
Definition: JSONObject.cpp:207
ColorMap::Value
Value
Color maps that associate a color to every float from [0..1].
Definition: color.h:16
armarx::JSONObject::append
void append(const armarx::AbstractObjectSerializerPtr &val) override
Definition: JSONObject.cpp:171
armarx::JSONObject::setFloat
void setFloat(unsigned int index, float val) override
Definition: JSONObject.h:190
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::JSONObject::setInt
void setInt(unsigned int index, int val) override
Definition: JSONObject.h:186
armarx::JSONObject::setFloatArray
void setFloatArray(const ::std::string &key, const std::vector< float > &val) override
Definition: JSONObject.h:166
armarx::JSONObject::setVariantMap
void setVariantMap(const ::std::string &key, const armarx::StringVariantBaseMap &val) override
Definition: JSONObject.cpp:143
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::JSONObject::ConvertToBasicVariantMap
static StringVariantBaseMap ConvertToBasicVariantMap(const JSONObjectPtr &serializer, const VariantBasePtr &variant)
Definition: JSONObject.cpp:523
armarx::JSONObject::reset
void reset() override
Definition: JSONObject.cpp:518
armarx::JSONObject::setString
void setString(const ::std::string &key, const std::string &val) override
Definition: JSONObject.h:156
armarx::JSONObject
The JSONObject class is used to represent and (de)serialize JSON objects.
Definition: JSONObject.h:43
AbstractObjectSerializer.h
armarx::JSONObject::getDouble
double getDouble(const ::std::string &key) const override
Definition: JSONObject.cpp:321
armarx::JSONObject::setDouble
void setDouble(unsigned int index, double val) override
Definition: JSONObject.h:194
armarx::JSONObject::getIntArray
void getIntArray(const ::std::string &key, std::vector< int > &result) override
Definition: JSONObject.h:91
armarx::ElementTypes::eObject
@ eObject
Definition: AbstractObjectSerializer.h:35
armarx::JSONObject::set
void set(const ::std::string &key, T val)
Definition: JSONObject.h:111
armarx::JSONObject::toString
std::string toString(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: JSONObject.cpp:58
armarx::JSONObject::getString
std::string getString(const ::std::string &key) const override
Definition: JSONObject.cpp:375
armarx::JSONObject::getDoubleArray
void getDoubleArray(const ::std::string &key, std::vector< double > &result) override
Definition: JSONObject.h:99
armarx::JSONObject::getVariantMap
void getVariantMap(const ::std::string &key, armarx::StringVariantBaseMap &result) override
Definition: JSONObject.cpp:463
IceInternal::Handle< JSONObject >
armarx::JSONObject::getJsonValue
const Json::Value & getJsonValue() const
Definition: JSONObject.cpp:93
armarx::JSONObject::~JSONObject
~JSONObject() override
Definition: JSONObject.cpp:54
armarx::JSONObject::setElement
void setElement(const ::std::string &key, const armarx::AbstractObjectSerializerPtr &obj) override
Definition: JSONObject.cpp:104
armarx::JSONObject::setIntArray
void setIntArray(const ::std::string &key, const std::vector< int > &val) override
Definition: JSONObject.h:162
armarx::JSONObject::setDoubleArray
void setDoubleArray(const ::std::string &key, const std::vector< double > &val) override
Definition: JSONObject.h:170
armarx::JSONObject::createElement
armarx::AbstractObjectSerializerPtr createElement() const override
Definition: JSONObject.cpp:98
armarx::JSONObject::fromString
void fromString(const ::std::string &jsonString, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: JSONObject.cpp:86
armarx::JSONObject::getElementNames
std::vector< std::string > getElementNames() const override
Definition: JSONObject.cpp:430
armarx::JSONObject::setBool
void setBool(unsigned int index, bool val) override
Definition: JSONObject.h:182
armarx::JSONObject::setFloat
void setFloat(const ::std::string &key, float val) override
Definition: JSONObject.h:148
armarx::JSONObject::setDouble
void setDouble(const ::std::string &key, double val) override
Definition: JSONObject.h:152
armarx::JSONObject::getArray
void getArray(const std::string &key, std::vector< T > &result)
Definition: JSONObject.h:73
armarx::JSONObject::merge
void merge(const armarx::AbstractObjectSerializerPtr &val) override
Definition: JSONObject.cpp:185
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::JSONObject::set
void set(int index, T val)
Definition: JSONObject.h:117
armarx::JSONObject::getVariantArray
void getVariantArray(const ::std::string &key, std::vector< armarx::VariantPtr > &result) override
Definition: JSONObject.cpp:442
armarx::AbstractObjectSerializer::ic
Ice::CommunicatorPtr ic
Definition: AbstractObjectSerializer.h:166
armarx::JSONObject::setInt
void setInt(const ::std::string &key, int val) override
Definition: JSONObject.h:144
armarx::JSONObject::setStringArray
void setStringArray(const ::std::string &key, const std::vector< std::string > &val) override
Definition: JSONObject.h:174
armarx::JSONObject::getStringArray
void getStringArray(const ::std::string &key, std::vector< std::string > &result) override
Definition: JSONObject.h:103
armarx::JSONObject::setVariantArray
void setVariantArray(const ::std::string &key, const std::vector< armarx::VariantBasePtr > &val) override
armarx::JSONObject::JSONObject
JSONObject(armarx::ElementType nodeType=armarx::ElementTypes::eObject, const Ice::CommunicatorPtr ic=Ice::CommunicatorPtr())
Definition: JSONObject.cpp:34
armarx::JSONObject::getInt
int getInt(const ::std::string &key) const override
Definition: JSONObject.cpp:339
armarx::JSONObject::getFloatArray
void getFloatArray(const ::std::string &key, std::vector< float > &result) override
Definition: JSONObject.h:95
armarx::JSONObject::size
unsigned int size() const override
Definition: JSONObject.cpp:202
armarx::JSONObject::getFloat
float getFloat(const ::std::string &key) const override
Definition: JSONObject.cpp:303
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::JSONObject::asString
std::string asString(bool pretty=false) const
Definition: JSONObject.cpp:63
armarx::JSONObject::setArray
void setArray(const ::std::string &key, const std::vector< T > &val)
Definition: JSONObject.h:123
armarx::JSONObject::setElementType
void setElementType(armarx::ElementType nodeType, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: JSONObject.cpp:499
armarx::JSONObject::getElementType
armarx::ElementType getElementType(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: JSONObject.cpp:483
armarx::JSONObject::getBool
bool getBool(const ::std::string &key) const override
Definition: JSONObject.cpp:357
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::AbstractObjectSerializer
Definition: AbstractObjectSerializer.h:52