30 #include <SimoxUtility/algorithm/string/string_tools.h>
67 return jsonValue.toStyledString();
71 Json::FastWriter writer;
73 std::string jsonString = writer.write(jsonValue);
77 while (jsonString.at(jsonString.size() - 1) ==
'\n')
79 jsonString.erase(jsonString.end() - 1);
90 reader.parse(jsonString, jsonValue);
106 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
107 jsonValue[key] = jsonObj->jsonValue;
116 arr.resize(val.size());
118 for (
size_t i = 0; i < val.size(); ++i)
120 VariantPtr var = VariantPtr::dynamicCast(val.at(i));
122 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(serial);
123 arr[int(i)] = jsonObj->jsonValue;
127 jsonValue[key] = arr;
132 std::vector<VariantBasePtr> baseList;
133 baseList.resize(val.size());
135 for (
unsigned int i = 0; i < val.size(); i++)
137 baseList.at(i) = val.at(i);
149 arr.resize(val.size());
152 for (StringVariantBaseMap::const_iterator it = val.begin(); it != val.end(); ++it, i++)
154 VariantPtr var = VariantPtr::dynamicCast(it->second);
158 arr[int(i)] = json.jsonValue;
162 jsonValue[key] = arr;
167 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
168 jsonValue[
index] = jsonObj->jsonValue;
173 const JSONObjectPtr jsonObj = JSONObjectPtr::dynamicCast(obj);
175 if (jsonValue.isArray())
177 jsonValue.append(jsonObj->jsonValue);
181 throw JSONInvalidDataTypeException();
189 const std::vector<std::string> elemNames = obj->getElementNames();
191 for (std::vector<std::string>::const_iterator it = elemNames.begin(); it != elemNames.end(); ++it)
193 set(*it, obj->getElement(*it));
198 throw JSONInvalidDataTypeException();
204 return jsonValue.size();
209 return jsonValue.isMember(key);
212 void JSONObject::get(
const Json::Value& val,
bool& result)
const
216 throw JSONInvalidDataTypeException();
220 result = val.asBool();
224 void JSONObject::get(
const Json::Value& val,
int& result)
const
226 if (!val.isIntegral())
228 throw JSONInvalidDataTypeException();
232 result = val.asInt();
236 void JSONObject::get(
const Json::Value& val,
float& result)
const
238 if (!val.isNumeric())
240 throw JSONInvalidDataTypeException();
244 result = val.asFloat();
248 void JSONObject::get(
const Json::Value& val,
double& result)
const
250 if (!val.isNumeric())
252 throw JSONInvalidDataTypeException();
256 result = val.asDouble();
260 void JSONObject::get(
const Json::Value& val, std::string& result)
const
264 throw JSONInvalidDataTypeException();
268 result = val.asString();
272 const Json::Value& JSONObject::getValue(
const std::string& key)
const
274 if (!jsonValue.isObject())
276 throw JSONInvalidDataTypeException(
"Attemping to get a key from a non object. key: " + key);
278 if (key.find_first_of(
'.') > 0)
285 for (
size_t i = 1; i < keys.size(); ++i)
286 if (result->isObject())
288 result = &((*result)[keys[i]]);
292 throw JSONInvalidFieldException(key);
299 return jsonValue[key];
309 throw JSONInvalidFieldException(key);
311 else if (!val.isNumeric())
313 throw JSONInvalidDataTypeException();
317 return val.asFloat();
327 throw JSONInvalidFieldException(key);
329 else if (!val.isNumeric())
331 throw JSONInvalidDataTypeException();
335 return val.asDouble();
345 throw JSONInvalidFieldException(key);
347 else if (!val.isInt())
349 throw JSONInvalidDataTypeException();
363 throw JSONInvalidFieldException(key);
365 else if (!val.isBool())
367 throw JSONInvalidDataTypeException();
382 throw JSONInvalidFieldException(key);
384 else if (!val.isString())
386 throw JSONInvalidDataTypeException();
390 return val.asString();
396 if (!jsonValue.isArray())
398 throw JSONInvalidDataTypeException();
400 else if (
index >= jsonValue.size())
402 throw JSONInvalidArrayIndexException();
407 elem->jsonValue = jsonValue[
index];
414 if (!jsonValue.isObject())
416 throw JSONInvalidDataTypeException();
418 else if (!jsonValue.isMember(key))
420 throw JSONInvalidFieldException(key);
425 elem->jsonValue = jsonValue[key];
432 if (!jsonValue.isObject())
434 throw JSONInvalidDataTypeException();
438 return jsonValue.getMemberNames();
446 if (!arrValue.isArray())
448 throw JSONInvalidDataTypeException();
452 result.resize(arrValue.size());
454 for (
size_t i = 0; i < arrValue.size(); ++i)
457 json->jsonValue = arrValue[int(i)];
467 if (!arrValue.isArray())
469 throw JSONInvalidDataTypeException();
474 for (
size_t i = 0; i < arrValue.size(); ++i)
477 json->jsonValue = arrValue[int(i)];
479 result[json->getString(
"key")] = var;
485 if (jsonValue.isArray())
489 else if (jsonValue.isObject())
529 var = JSONObjectPtr::dynamicCast(serializer->serializeVariant(VariantPtr::dynamicCast(variant)));
538 for (
size_t i = 0; i < var->size(); i++)
541 auto names = var->getElementNames();
543 for (
auto name :
names)
545 JSONObjectPtr elem = JSONObjectPtr::dynamicCast(var->getElement(name));
549 if (elem->jsonValue.isBool())
551 result[name] =
new Variant(var->getBool(name));
553 else if (elem->jsonValue.isString())
555 result[name] =
new Variant(var->getString(name));
557 else if (elem->jsonValue.isDouble())
559 result[name] =
new Variant(var->getDouble(name));
561 else if (elem->jsonValue.isInt())
563 result[name] =
new Variant(var->getInt(name));
567 throw LocalException() <<
"Unhandled type in variant to Dict conversion: " << elem->jsonValue.type();
572 auto subResult = ConvertToBasicVariantMap(elem,
nullptr);
574 for (
auto& entry : subResult)
576 result[name +
"." + entry.first] = entry.second;