persist Eol InProgress state
parent
1a2d7ef5e1
commit
e945a9a99c
Binary file not shown.
Binary file not shown.
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
|
namespace nsRUNCTRL
|
||||||
|
{
|
||||||
|
using stRunEolState = struct _stRunEolState
|
||||||
|
{
|
||||||
|
std::string strName;
|
||||||
|
int bEol{ true };
|
||||||
|
};
|
||||||
|
|
||||||
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(
|
||||||
|
stRunEolState,
|
||||||
|
strName,
|
||||||
|
bEol
|
||||||
|
)
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@ -0,0 +1,96 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include "nlohmann/json.hpp"
|
||||||
|
|
||||||
|
#include "DebugLog.h"
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class CJsonParser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool Deserialize(const std::string& strFile, T& myOption)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// load json file
|
||||||
|
std::ifstream infile(strFile);
|
||||||
|
if (!infile.is_open())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json jf;
|
||||||
|
|
||||||
|
// parse string into json
|
||||||
|
infile >> jf;
|
||||||
|
|
||||||
|
// deserialize json into object
|
||||||
|
myOption = jf.template get<T>();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::string ss = e.what();
|
||||||
|
DebugLog("{} : {}", strFile, ss);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Serialize(const std::string& strFile, const T& myOption)
|
||||||
|
{
|
||||||
|
// convert object into json
|
||||||
|
nlohmann::json jf(myOption);
|
||||||
|
|
||||||
|
// persist to file
|
||||||
|
std::ofstream outfile(strFile, std::ofstream::trunc);
|
||||||
|
|
||||||
|
outfile << std::setw(4) << jf;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Deserialize(const std::string& strFile, std::vector<T>& myOption)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// load json file
|
||||||
|
std::ifstream infile(strFile);
|
||||||
|
if (!infile.is_open())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
nlohmann::json jf;
|
||||||
|
|
||||||
|
// parse string into json
|
||||||
|
infile >> jf;
|
||||||
|
|
||||||
|
// deserialize json into object
|
||||||
|
myOption = jf.template get<std::vector<T>>();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::string ss = e.what();
|
||||||
|
DebugLog("{} : {}", strFile, ss);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Serialize(const std::string& strFile, const std::vector<T>& myOption)
|
||||||
|
{
|
||||||
|
// convert object into json
|
||||||
|
nlohmann::json jf(myOption);
|
||||||
|
|
||||||
|
// persist to file
|
||||||
|
std::ofstream outfile(strFile, std::ofstream::trunc);
|
||||||
|
|
||||||
|
outfile << std::setw(4) << jf;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue