From f71cd683cc8286a80ec968c8103598adcc8a4098 Mon Sep 17 00:00:00 2001 From: Lveyou <1339484752@qq.com> Date: Tue, 7 Jul 2026 14:42:27 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E6=94=AF=E6=8C=81=E4=BA=8C=E8=BF=9B?= =?UTF-8?q?=E5=88=B6json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dl/io/dl_file.cpp | 4 ++-- dl/io/dl_json.cpp | 52 ++++++++++++++++++++++++---------------- dl/io/dl_json.h | 21 +++++++++++----- dl/render/dl_factory.cpp | 5 ++-- project/dl_chess | 2 +- 5 files changed, 52 insertions(+), 32 deletions(-) diff --git a/dl/io/dl_file.cpp b/dl/io/dl_file.cpp index d6a68483..5fcfdb1b 100644 --- a/dl/io/dl_file.cpp +++ b/dl/io/dl_file.cpp @@ -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); diff --git a/dl/io/dl_json.cpp b/dl/io/dl_json.cpp index 755d4ce8..71af35d9 100644 --- a/dl/io/dl_json.cpp +++ b/dl/io/dl_json.cpp @@ -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); } } \ No newline at end of file diff --git a/dl/io/dl_json.h b/dl/io/dl_json.h index 401da544..2d6ac749 100644 --- a/dl/io/dl_json.h +++ b/dl/io/dl_json.h @@ -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 diff --git a/dl/render/dl_factory.cpp b/dl/render/dl_factory.cpp index 6bfc5a02..50275666 100644 --- a/dl/render/dl_factory.cpp +++ b/dl/render/dl_factory.cpp @@ -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); } diff --git a/project/dl_chess b/project/dl_chess index 3b7efbae..3a50383f 160000 --- a/project/dl_chess +++ b/project/dl_chess @@ -1 +1 @@ -Subproject commit 3b7efbaecb7b67897f5c780e5cd9b25fc1bf67cb +Subproject commit 3a50383faaf6f107a57a85d46d5e67d208f8cd67