Files
dl_old/dl/io/dl_json.cpp
T
2024-04-12 18:19:42 +08:00

53 lines
835 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "io/dl_json.h"
#include "io/dl_file.h"
#include "io/dl_log.h"
namespace dl::Json
{
bool g_bJsonLog = true;
void SetLog(bool open)
{
g_bJsonLog = open;
}
json Parse(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
{
//j = json::parse(buf.data(), buf.data() + buf.size());
json j = json::parse(ifs);
return j;
}
catch (nlohmann::detail::exception& e)
{
if (g_bJsonLog)
log_err("{}解析失败:{}", path_name, e.what());
}
return {};
}
json Parse(BufferView buf)
{
try
{
json j = json::parse(buf.data(), buf.data() + buf.size());
return j;
}
catch (nlohmann::detail::exception& e)
{
if (g_bJsonLog)
log_err("解析失败:{}", e.what());
}
return {};
}
}