#include "fmt.json.h" #include #include #include 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(ifs)), std::istreambuf_iterator()); // 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; }