Files
dl_old/dl/io/dl_json.cpp
T
2026-04-01 18:12:58 +08:00

58 lines
985 B
C++

#include "io/dl_json.h"
#include "io/dl_file.h"
#include "io/dl_log.h"
#include "memory/dl_code_cvt.h"
#include "base/dl_string.h"
namespace dl::Json
{
bool g_bJsonLog = true;
void SetLog(bool open)
{
g_bJsonLog = open;
}
json Parse(BufferView buf)
{
try
{
#if DL_PLATFORM == DL_PLATFORM_WIN32
json j = json::parse(buf.data(), buf.data() + buf.size());
#else
json j = json::parse((char*)buf.data(), (char*)buf.data() + buf.size());
#endif
return j;
}
catch (nlohmann::detail::exception& e)
{
if (g_bJsonLog)
log_err("解析失败:{}", dl::to_string(e));
}
return {};
}
json ParseFile(std::string_view path_name)
{
std::ifstream ifs = File::Get(path_name);
if (!ifs.is_open())
{
if(g_bJsonLog)
log_err("json文件读取失败:{}", path_name);
}
try
{
json j = json::parse(ifs);
return j;
}
catch (nlohmann::detail::exception& e)
{
if (g_bJsonLog)
log_err("{}解析失败:{}", path_name, dl::to_string(e));
}
return {};
}
}