* 支持二进制json

This commit is contained in:
2026-07-07 14:42:27 +08:00
parent 4cd72a3068
commit f71cd683cc
5 changed files with 52 additions and 32 deletions
+2 -2
View File
@@ -514,7 +514,7 @@ bool CheckPathFileModified(std::string_view path_name, const SearchRule& rule, s
j["version"] = VERSION;
j["files"] = vec_file;
File::SaveString(j.dump(4), cache);
Json::SaveFile(j, cache, true);
}
else
{//cache文件存在
@@ -522,7 +522,7 @@ bool CheckPathFileModified(std::string_view path_name, const SearchRule& rule, s
bool not_match = false;
do
{
json j = Json::ParseFile(cache);
json j = Json::ParseFile(cache, true);
if (j.is_null())
{
log_err("cache加载失败:{}", cache);
+31 -21
View File
@@ -2,7 +2,6 @@
#include "io/dl_file.h"
#include "io/dl_log.h"
#include "memory/dl_code_cvt.h"
#include "base/dl_string.h"
namespace dl::Json
@@ -15,15 +14,20 @@ void SetLog(bool open)
g_bJsonLog = open;
}
json Parse(BufferView buf)
json Parse(BufferView buf, bool binary)
{
// 基础接口
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
json j;
if (binary)
{
j = json::from_cbor(buf.data(), buf.data() + buf.size());
}
else
{
j = json::parse((char*)buf.data(), (char*)buf.data() + buf.size());
}
return j;
}
catch (nlohmann::detail::exception& e)
@@ -34,25 +38,31 @@ json Parse(BufferView buf)
return {};
}
json ParseFile(std::string_view path_name)
json ParseString(std::string_view str)
{
std::ifstream ifs = File::Get(path_name);
if (!ifs.is_open())
return Parse(CreateBufferView(str));
}
json ParseFile(std::string_view path_name, bool binary)
{
if (binary)
{
if(g_bJsonLog)
log_err("json文件读取失败:{}", path_name);
Buffer buf = File::GetBuffer(path_name);
return Parse(buf, binary);
}
try
else
{
json j = json::parse(ifs);
return j;
std::string str = File::GetString(path_name);
return ParseString(str);
}
catch (nlohmann::detail::exception& e)
{
if (g_bJsonLog)
log_err("{}解析失败:{}", path_name, dl::to_string(e));
}
return {};
}
bool SaveFile(const json& j, std::string_view path_name, bool binary /*= false*/)
{
if (binary)
return File::SaveBuffer(CreateBufferView(json::to_cbor(j)), path_name);
else
return File::SaveString(j.dump(4), path_name);
}
}
+15 -6
View File
@@ -39,24 +39,26 @@ struct DisableLog
/**
* @brief 从 内存 构建json对象
* @param[in] buf 数据
* @param[in] binary 是否二进制json
* @note 失败返回null,并输出日志
*/
json Parse(BufferView buf);
json Parse(BufferView buf, bool binary = false);
/**
* @brief 从 文件 构建json对象
* @param[in] buf 数据
* @param[in] binary 是否二进制json文件
* @note 失败返回null,并输出日志
*/
json ParseFile(std::string_view path_name);
json ParseFile(std::string_view path_name, bool binary = false);
/**
* @brief 从 字符串 构建json对象
* @param[in] str 文本json
* @note 失败返回null,并输出日志
*/
inline json ParseString(std::string_view str)
{
return Parse(CreateBufferView(str));
}
json ParseString(std::string_view str);
/**
@@ -122,6 +124,13 @@ inline std::string Dump(BufferView buf)
return {};
return Dump(j);
}
/**
* @brief 保存到文件
* @note binary保存的文件,加载时也需要传入true
*/
bool SaveFile(const json& j, std::string_view path_name, bool binary = false);
}
namespace dl
+3 -2
View File
@@ -1303,7 +1303,7 @@ void Factory::_load_sprite_binary()
path_out += '/';
Json::DisableLog l;
json j = Json::ParseFile(path_out + "sprite.json");
json j = Json::ParseFile(path_out + "sprite.json", true);
if (j.is_null())
{
log_err("从缓存目录读取2d资源失败:{}", path_out);
@@ -1699,7 +1699,8 @@ void Factory::_load_sprite_sub(std::string_view path, std::string_view name, std
json j;
j = group;
File::SaveString(j.dump(4), path_out + "sprite.json");
Json::SaveFile(j, path_out + "sprite.json", true);
log_msg("合并图集并导出信息完成:{} -> {}", name, path_out);
}