Usage
JsonType available
- DEFAULT_ENEMY,
- DEFAULT_PLAYER,
- DEFAULT_PARALLAX,
Methods
- Get the instance of the class :
Json &Json::getInstance();
- Get a data with JsonType :
nlohmann::json Json::getDataByJsonType(JsonType dataType);
- Get a data with a key :
nlohmann::json Json::getDataByJsonType(const std::string &index, JsonType dataType);
- Get a data with vector of key :
nlohmann::json Json::getDataByVector(const std::vector<std::string> &indexes, JsonType dataType);
- Get a data from a json list :
This function is useful when you want to take a json list inside your json because all of your items of the list are in the vector.
std::vector<nlohmann::json> Json::getDatasByJsonType(const std::vector<std::string> &indexes, JsonType dataType);
- Get a data but precise the type of what you want with template :
template <typename T>
T Json::getDataFromJson(nlohmann::json jsonData, const std::string &index);
- Get a data from a vector of json data :
std::vector<nlohmann::json> Json::getDatasFromList(const std::vector<nlohmann::json> &list, const std::string &key);
- Get a json object from a jsonType and an id :
nlohmann::json Json::getJsonObjectById(JsonType type, const std::string &id, const std::string &arrayName);
- Get a json list from a json data :
std::vector<nlohmann::json> Json::getDatasFromList(const nlohmann::json &list, const std::string &key);
- Get a json list from a json data :
std::vector<nlohmann::json> Json::getDatasFromList(const nlohmann::json &list);
- Check if the data exists in the json :
bool Json::isDataExist(nlohmann::json jsonData, const std::string &index);
Errors handling
All the methods above Log an error with the class Logger and throw an std::runtime_error if the arguments are incorect or the key in the json is not found because if the data is not get correctly the rest of rest programm might crash.
Here's a json with spritePath of enemy missing :
{
"enemy" : {
}
}
Here's a code that will try to acces it :
Json::getInstance().getDataByVector({"enemy", "spritePath"},
JsonType::DEFAULT_ENEMY)
This won't work and output this :
2023-10-15 13:29:57.813141624 [FATAL] (getDataByVector) Key : spritePath is not valid
However you can handle you proper way the errors with the method isDataExist listed above.
Some examples
Basic example
Here's a json :
{
"enemy" : {
"spritePath" : "path"
}
}
Here's how you can get spritePath data :
std::cout << Json::getInstance().getDataByVector({"enemy", "spritePath"},
JsonType::DEFAULT_ENEMY) << std::endl;
Using list data
Here's a new json but with a list inside :
{
"enemy" : [
{
"spritePath" : "path"
},
{
"spritePath" : "path"
}
]
}
You can see that all spritePath are in a list.
Here's how to proceed :
std::vector<nlohmann::json> enemyData =
Json::getInstance().getDataByJsonType("enemy", enemyType);
And now you can iterate on your datas :
for (auto &data : enemyData) {
std::cout << Json::getInstance().getDataFromJson<std::string>(elem, "spritePath") <<
std::endl;
}
Get a json object from a jsonType and an id
Here's a json file with an array of enemy objects :
{
"enemy" : [
{
"id" : "1",
"spritePath" : "path"
},
{
"id" : "2",
"spritePath" : "path"
}
]
}
Here's how to get an enemy object from his id :
nlohmann::json object = Json::getJsonObjectById(enemyType, "1", "enemy");
//the returned object will be :
// {
// "id" : "1",
// "spritePath" : "path"
// }