27#include <SimoxUtility/algorithm/string/string_tools.h>
68 return jsonValue.toStyledString();
72 Json::FastWriter writer;
74 std::string jsonString = writer.write(jsonValue);
78 while (jsonString.at(jsonString.size() - 1) ==
'\n')
80 jsonString.erase(jsonString.end() - 1);
92 reader.parse(jsonString, jsonValue);
111 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
112 jsonValue[key] = jsonObj->jsonValue;
118 Json::Value arr(Json::arrayValue);
122 arr.resize(val.size());
124 for (
size_t i = 0; i < val.size(); ++i)
126 VariantPtr var = VariantPtr::dynamicCast(val.at(i));
128 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(serial);
129 arr[int(i)] = jsonObj->jsonValue;
133 jsonValue[key] = arr;
139 std::vector<VariantBasePtr> baseList;
140 baseList.resize(val.size());
142 for (
unsigned int i = 0; i < val.size(); i++)
144 baseList.at(i) = val.at(i);
153 Json::Value arr(Json::arrayValue);
157 arr.resize(val.size());
160 for (StringVariantBaseMap::const_iterator it = val.begin(); it != val.end(); ++it, i++)
162 VariantPtr var = VariantPtr::dynamicCast(it->second);
166 arr[int(i)] = json.jsonValue;
170 jsonValue[key] = arr;
176 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
177 jsonValue[
index] = jsonObj->jsonValue;
183 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
185 if (jsonValue.isArray())
187 jsonValue.append(jsonObj->jsonValue);
191 throw JSONInvalidDataTypeException();
201 const std::vector<std::string> elemNames = obj->getElementNames();
203 for (std::vector<std::string>::const_iterator it = elemNames.begin();
204 it != elemNames.end();
207 set(*it, obj->getElement(*it));
212 throw JSONInvalidDataTypeException();
219 return jsonValue.size();
225 return jsonValue.isMember(key);
229 JSONObject::get(
const Json::Value& val,
bool& result)
const
233 throw JSONInvalidDataTypeException();
237 result = val.asBool();
242 JSONObject::get(
const Json::Value& val,
int& result)
const
244 if (!val.isIntegral())
246 throw JSONInvalidDataTypeException();
250 result = val.asInt();
255 JSONObject::get(
const Json::Value& val,
float& result)
const
257 if (!val.isNumeric())
259 throw JSONInvalidDataTypeException();
263 result = val.asFloat();
268 JSONObject::get(
const Json::Value& val,
double& result)
const
270 if (!val.isNumeric())
272 throw JSONInvalidDataTypeException();
276 result = val.asDouble();
281 JSONObject::get(
const Json::Value& val, std::string& result)
const
285 throw JSONInvalidDataTypeException();
289 result = val.asString();
294 JSONObject::getValue(
const std::string& key)
const
296 if (!jsonValue.isObject())
298 throw JSONInvalidDataTypeException(
"Attemping to get a key from a non object. key: " +
301 if (key.find_first_of(
'.') > 0)
305 std::vector<std::string> keys = simox::alg::split(key,
".");
306 Json::Value
const* result = &jsonValue[keys[0]];
308 for (
size_t i = 1; i < keys.size(); ++i)
309 if (result->isObject())
311 result = &((*result)[keys[i]]);
315 throw JSONInvalidFieldException(key);
322 return jsonValue[key];
329 const Json::Value& val = getValue(key);
333 throw JSONInvalidFieldException(key);
335 else if (!val.isNumeric())
337 throw JSONInvalidDataTypeException();
341 return val.asFloat();
348 const Json::Value& val = getValue(key);
352 throw JSONInvalidFieldException(key);
354 else if (!val.isNumeric())
356 throw JSONInvalidDataTypeException();
360 return val.asDouble();
367 const Json::Value& val = getValue(key);
371 throw JSONInvalidFieldException(key);
373 else if (!val.isInt())
375 throw JSONInvalidDataTypeException();
386 const Json::Value& val = getValue(key);
390 throw JSONInvalidFieldException(key);
392 else if (!val.isBool())
394 throw JSONInvalidDataTypeException();
405 const Json::Value& val = getValue(key);
411 throw JSONInvalidFieldException(key);
413 else if (!val.isString())
415 throw JSONInvalidDataTypeException();
419 return val.asString();
426 if (!jsonValue.isArray())
428 throw JSONInvalidDataTypeException();
430 else if (
index >= jsonValue.size())
432 throw JSONInvalidArrayIndexException();
437 elem->jsonValue = jsonValue[
index];
445 if (!jsonValue.isObject())
447 throw JSONInvalidDataTypeException();
449 else if (!jsonValue.isMember(key))
451 throw JSONInvalidFieldException(key);
456 elem->jsonValue = jsonValue[key];
461 std::vector<std::string>
464 if (!jsonValue.isObject())
466 throw JSONInvalidDataTypeException();
470 return jsonValue.getMemberNames();
477 const Json::Value& arrValue = getValue(key);
479 if (!arrValue.isArray())
481 throw JSONInvalidDataTypeException();
485 result.resize(arrValue.size());
487 for (
size_t i = 0; i < arrValue.size(); ++i)
490 json->jsonValue = arrValue[int(i)];
499 const Json::Value& arrValue = getValue(key);
501 if (!arrValue.isArray())
503 throw JSONInvalidDataTypeException();
508 for (
size_t i = 0; i < arrValue.size(); ++i)
511 json->jsonValue = arrValue[int(i)];
513 result[json->getString(
"key")] = var;
520 if (jsonValue.isArray())
524 else if (jsonValue.isObject())
540 jsonValue = Json::Value();
544 jsonValue = Json::Value(Json::arrayValue);
549 jsonValue = Json::Value(Json::objectValue);
568 var = JSONObjectPtr::dynamicCast(
569 serializer->serializeVariant(VariantPtr::dynamicCast(variant)));
578 for (
size_t i = 0; i < var->size(); i++)
581 auto names = var->getElementNames();
583 for (
auto name : names)
585 JSONObjectPtr elem = JSONObjectPtr::dynamicCast(var->getElement(name));
589 if (elem->jsonValue.isBool())
591 result[name] =
new Variant(var->getBool(name));
593 else if (elem->jsonValue.isString())
595 result[name] =
new Variant(var->getString(name));
597 else if (elem->jsonValue.isDouble())
599 result[name] =
new Variant(var->getDouble(name));
601 else if (elem->jsonValue.isInt())
603 result[name] =
new Variant(var->getInt(name));
607 throw LocalException() <<
"Unhandled type in variant to Dict conversion: "
608 << elem->jsonValue.type();
615 for (
auto& entry : subResult)
617 result[name +
"." + entry.first] = entry.second;
std::string getIdField() const
void setIdField(const std::string &fieldName)
AbstractObjectSerializerPtr serializeVariant(const VariantPtr &val) const
virtual void setVariant(const ::std::string &key, const VariantPtr &val)
AbstractObjectSerializer()
std::vector< std::string > getElementNames() const override
void setVariantArray(const ::std::string &key, const std::vector< armarx::VariantBasePtr > &val) override
unsigned int size() const override
std::string toString(const ::Ice::Current &=Ice::emptyCurrent) const override
double getDouble(const ::std::string &key) const override
std::string asString(bool pretty=false) const
const Json::Value & getJsonValue() const
float getFloat(const ::std::string &key) const override
armarx::ElementType getElementType(const ::Ice::Current &=Ice::emptyCurrent) const override
std::string getString(const ::std::string &key) const override
void setString(const ::std::string &key, const std::string &val) override
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
static StringVariantBaseMap ConvertToBasicVariantMap(const JSONObjectPtr &serializer, const VariantBasePtr &variant)
bool getBool(const ::std::string &key) const override
void getVariantArray(const ::std::string &key, std::vector< armarx::VariantPtr > &result) override
void set(const ::std::string &key, T val)
void merge(const armarx::AbstractObjectSerializerPtr &val) override
armarx::AbstractObjectSerializerPtr getElement(unsigned int index) const override
void getVariantMap(const ::std::string &key, armarx::StringVariantBaseMap &result) override
void setVariantMap(const ::std::string &key, const armarx::StringVariantBaseMap &val) override
void append(const armarx::AbstractObjectSerializerPtr &val) override
void setElementType(armarx::ElementType nodeType, const ::Ice::Current &=Ice::emptyCurrent) override
The Variant class is described here: Variants.
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::map< std::string, VariantBasePtr > StringVariantBaseMap
IceInternal::Handle< Variant > VariantPtr
IceInternal::Handle< AbstractObjectSerializer > AbstractObjectSerializerPtr
ElementTypes::ElementType ElementType
IceInternal::Handle< JSONObject > JSONObjectPtr
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr