Skip to content

Commit

Permalink
ECS: Fix json type
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Oct 31, 2023
1 parent d5697c3 commit e023943
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions include/B-luga/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ class Json {
}

nlohmann::json
getJsonObjectById(const std::string &type, const std::string &id, const std::string &arrayName)
getJsonObjectById(const std::string &dataType, const std::string &id, const std::string &arrayName)
{
auto objectList = getDataByJsonType(type)[arrayName];
auto objectList = getDataByJsonType(dataType)[arrayName];

for (const auto &object : objectList) {
auto idField = object.find("id");
if (idField != object.end() && idField->is_string() && *idField == id) {
return object;
}
}
Logger::fatal(std::string("(getJsonObject) Key : " + id + " is not valid"));
Logger::fatal(std::string("(getJsonObject) Key : ") + id + std::string(" is not valid"));
throw std::runtime_error("Json error");
}

Expand All @@ -50,15 +50,10 @@ class Json {
{
nlohmann::json finalData(_jsonDatas[dataType]);

for (const auto &key : index) {
finalData = finalData[key];
if (finalData == nullptr) {
Logger::fatal(std::string("(getDataByVector) Key : " + key + " is not valid"));
throw std::runtime_error("Json error");
}
if (finalData.is_array()) {
return (finalData);
}
finalData = finalData[index];
if (finalData == nullptr) {
Logger::fatal(std::string("(getDataByVector) Key : ") + index + std::string(" is not valid"));
throw std::runtime_error("Json error");
}

return (finalData);
Expand All @@ -77,7 +72,7 @@ class Json {

finalData = finalData[index];
if (finalData == nullptr) {
Logger::error(std::string("(getDataByJsonType) Key : " + index + " is not valid"));
Logger::error(std::string("(getDataByJsonType) Key : ") + index + std::string(" is not valid"));
}
return (finalData);
}
Expand All @@ -90,7 +85,7 @@ class Json {

for (const auto &elem : list) {
if (elem[index] == nullptr) {
Logger::fatal(std::string("(getDatasDromList : 2) Key : " + index + " is not valid"));
Logger::fatal(std::string("(getDatasDromList : 2) Key : ") + index + std::string(" is not valid"));
throw std::runtime_error("Json error");
}
if (elem[index].is_array() == true) {
Expand All @@ -109,7 +104,7 @@ class Json {

for (const auto &elem : list) {
if (elem[index] == nullptr) {
Logger::fatal(std::string("(getDatasFromList : 1) Key : " + index + " is not valid"));
Logger::fatal(std::string("(getDatasFromList : 1) Key : ") + index + std::string(" is not valid"));
throw std::runtime_error("Json error");
}
datas.push_back(elem[index]);
Expand Down Expand Up @@ -165,7 +160,7 @@ class Json {
T getDataFromJson(nlohmann::json jsonData, const std::string &index)
{
if (jsonData[index] == nullptr) {
Logger::fatal(std::string("(getDataByJson<template>) Key : " + index + " is not valid"));
Logger::fatal(std::string("(getDataByJson<template>) Key : ") + index + std::string(" is not valid"));
throw std::runtime_error("Json error");
}
return jsonData[index].get<T>();
Expand All @@ -183,7 +178,7 @@ class Json {
}
for (; begin + 1 != indexes.end(); begin++) {
if (datas[*begin] == nullptr) {
Logger::fatal(std::string("(getDataByVector<T>) Key : " + *begin + " is not valid"));
Logger::fatal(std::string("(getDataByVector<T>) Key : ") + *begin + std::string(" is not valid"));
throw std::runtime_error("Json error");
}
datas = datas[*begin];
Expand Down

0 comments on commit e023943

Please sign in to comment.