2020-12-02 16:19:16 -05:00
|
|
|
#pragma once
|
2021-01-03 06:48:07 -05:00
|
|
|
#include "vendor\json.hpp"
|
2020-12-02 16:19:16 -05:00
|
|
|
|
|
|
|
class CJson
|
|
|
|
{
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string file_path;
|
|
|
|
public:
|
|
|
|
nlohmann::json data;
|
2020-12-09 08:15:50 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Returns a value from json structure hierarchy using '.'
|
|
|
|
Example: "Menu.Window.X"
|
|
|
|
*/
|
2021-01-03 06:48:07 -05:00
|
|
|
// specialize since typeid(std::string) doesn't work
|
2020-12-21 15:24:07 -05:00
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
template <typename T>
|
|
|
|
T GetValue(std::string&& key, T&& default_val)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::stringstream ss(key);
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
nlohmann::json *json = &data;
|
|
|
|
|
|
|
|
while (getline(ss, line, '.'))
|
|
|
|
json = &((*json)[line]);
|
2020-12-21 15:24:07 -05:00
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
// json library bugs with bool, using int instead
|
|
|
|
if (typeid(T) == typeid(bool))
|
|
|
|
{
|
|
|
|
return ((json->get<int>() == 1) ? true : false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return json->get<T>();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
return default_val;
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 06:48:07 -05:00
|
|
|
|
|
|
|
template<>
|
|
|
|
std::string GetValue(std::string&& key, std::string&& default_val)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::stringstream ss(key);
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
nlohmann::json *json = &data;
|
|
|
|
|
|
|
|
while (getline(ss, line, '.'))
|
|
|
|
json = &((*json)[line]);
|
|
|
|
|
|
|
|
return json->get<std::string>();
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
return default_val;
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 08:15:50 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
Allows to save values in json hierarchy using '.'
|
|
|
|
Example: "Menu.Window.X"
|
|
|
|
*/
|
2020-12-21 15:24:07 -05:00
|
|
|
|
2020-12-02 16:19:16 -05:00
|
|
|
template <typename T>
|
2021-01-03 06:48:07 -05:00
|
|
|
void SetValue(std::string&& key, T& val)
|
2020-12-02 16:19:16 -05:00
|
|
|
{
|
|
|
|
std::stringstream ss(key);
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
nlohmann::json *json = &data;
|
|
|
|
|
|
|
|
while (getline(ss, line, '.'))
|
|
|
|
json = &((*json)[line]);
|
|
|
|
|
|
|
|
// json library bugs with bool, using int instead
|
|
|
|
if (typeid(T) == typeid(bool))
|
|
|
|
*json = (val ? 1 : 0);
|
|
|
|
else
|
|
|
|
*json = val;
|
|
|
|
}
|
2020-12-09 08:15:50 -05:00
|
|
|
|
2021-01-03 06:48:07 -05:00
|
|
|
template<>
|
|
|
|
void SetValue(std::string&& key, std::string& val)
|
|
|
|
{
|
|
|
|
std::stringstream ss(key);
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
nlohmann::json *json = &data;
|
|
|
|
|
|
|
|
while (getline(ss, line, '.'))
|
|
|
|
json = &((*json)[line]);
|
|
|
|
|
|
|
|
*json = val;
|
|
|
|
}
|
2020-12-09 08:15:50 -05:00
|
|
|
/*
|
|
|
|
Loads the section names into a category vector.
|
|
|
|
Used to create drop down category menus
|
|
|
|
*/
|
2020-12-20 14:23:32 -05:00
|
|
|
void LoadData(std::vector<std::string>& vec, std::string& selected);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Saves json data to disk
|
|
|
|
*/
|
|
|
|
void WriteToDisk();
|
2020-12-21 15:24:07 -05:00
|
|
|
CJson(const char* text);
|
2020-12-20 14:23:32 -05:00
|
|
|
~CJson();
|
2020-12-02 16:19:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|