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;
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);
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)
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];
333 throw JSONInvalidFieldException(key);
335 else if (!val.isNumeric())
337 throw JSONInvalidDataTypeException();
341 return val.asFloat();
352 throw JSONInvalidFieldException(key);
354 else if (!val.isNumeric())
356 throw JSONInvalidDataTypeException();
360 return val.asDouble();
371 throw JSONInvalidFieldException(key);
373 else if (!val.isInt())
375 throw JSONInvalidDataTypeException();
390 throw JSONInvalidFieldException(key);
392 else if (!val.isBool())
394 throw JSONInvalidDataTypeException();
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();
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)];
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())
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();
613 auto subResult = ConvertToBasicVariantMap(elem,
nullptr);
615 for (
auto& entry : subResult)
617 result[name +
"." + entry.first] = entry.second;