46 lines
1.2 KiB
C++
46 lines
1.2 KiB
C++
|
|
#include "fmt.json.h"
|
||
|
|
|
||
|
|
#include <boost/nowide/fstream.hpp>
|
||
|
|
#include <iostream>
|
||
|
|
#include <nlohmann/json.hpp>
|
||
|
|
|
||
|
|
using json = nlohmann::json;
|
||
|
|
|
||
|
|
JsonFormatter::JsonFormatter()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string JsonFormatter::format(const std::string& path, const FormatterArg& arg)
|
||
|
|
{
|
||
|
|
std::string result;
|
||
|
|
boost::nowide::ifstream ifs(path);
|
||
|
|
if (!ifs.is_open())
|
||
|
|
return "";
|
||
|
|
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
|
||
|
|
// std::cout << "Content: " << content << std::endl;
|
||
|
|
|
||
|
|
formatContent(content, arg, result);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool JsonFormatter::formatContent(const std::string& content, const FormatterArg& arg, std::string& outContent)
|
||
|
|
{
|
||
|
|
try {
|
||
|
|
json j = json::parse(content);
|
||
|
|
outContent = j.dump(arg.indent);
|
||
|
|
return true;
|
||
|
|
} catch (json::parse_error& e) {
|
||
|
|
AxcUtil::getLogger()->error("Parse json content error: [{}], return default content.", e.what());
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
bool JsonFormatter::save(const std::string& path, const std::string& content)
|
||
|
|
{
|
||
|
|
boost::nowide::ofstream ofs(path);
|
||
|
|
if (!ofs.is_open())
|
||
|
|
return false;
|
||
|
|
ofs << content;
|
||
|
|
return true;
|
||
|
|
}
|