CheatMenuSA/src/Json.cpp

46 lines
833 B
C++
Raw Normal View History

2020-12-02 16:19:16 -05:00
#include "pch.h"
#include "Json.h"
CJson::CJson(const char* name)
2020-12-02 16:19:16 -05:00
{
file_path = "./CheatMenu/json/"+ std::string(name) +".json";
if (fs::exists(file_path))
2020-12-02 16:19:16 -05:00
{
try
2020-12-02 16:19:16 -05:00
{
std::ifstream file(file_path);
file >> data;
file.close();
}
catch (...)
{
flog << "Error occured trying to read " << file_path << std::endl;
2020-12-02 16:19:16 -05:00
data = "{}"_json;
}
}
else
{
data = "{}"_json;
flog << "File doesn't exist " << file_path << std::endl;
2020-12-02 16:19:16 -05:00
}
}
void CJson::WriteToDisk()
{
std::ofstream file(file_path);
file << data.dump(4,' ',false, nlohmann::json::error_handler_t::replace) << std::endl;
file.close();
}
void CJson::LoadData(std::vector<std::string>& vec, std::string& selected) // Test
2020-12-02 16:19:16 -05:00
{
for (auto element : data.items())
vec.push_back(element.key());
}
CJson::~CJson()
{
// Saving here won't work on crash
2020-12-02 16:19:16 -05:00
}