json编译速度优化初步

This commit is contained in:
2026-12-02 18:21:49 +08:00
parent 867d37e033
commit fcc463a307
20 changed files with 164 additions and 107 deletions
+3 -1
View File
@@ -161,6 +161,9 @@ message("------------------fmt 文本格式化库--------------")
option(FMT_INSTALL "开启fmt的安装" ON)
add_subdirectory ("third/fmt")
message("------------------nlohmann-json json库---------")
add_subdirectory ("third/json")
message("------------------lunasvg svg格式库-----------")
add_subdirectory ("third/lunasvg")
message("------------------libwebp webp格式库-----------")
@@ -323,7 +326,6 @@ endif()
add_subdirectory(third/libcurl)
message("------------------其余库以其他形式链接----------")
message("------------------nlohmann-json json库---------")
message("------------------stb_image 图像解析库----------")
message("------------------qrcodegen 二维码生成库-----------")
message("------------------vld 内存泄漏检测库----------")
+1 -2
View File
@@ -78,7 +78,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE lz4)
# fmt
target_link_libraries(${PROJECT_NAME} PRIVATE fmt)
# json
include_directories(${CMAKE_SOURCE_DIR}/third/json)
include_directories(${CMAKE_SOURCE_DIR}/third/json/include)
# lunasvg
target_link_libraries(${PROJECT_NAME} PRIVATE lunasvg)
# libwebp
@@ -235,7 +235,6 @@ install(DIRECTORY "./"
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/dl/
FILES_MATCHING PATTERN "*.h")
install(FILES ${CMAKE_SOURCE_DIR}/third/json/json.hpp DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
install(FILES ${CMAKE_SOURCE_DIR}/third/tinyxml2/tinyxml2.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/third/asio/include/
DESTINATION ${CMAKE_INSTALL_PREFIX}/include
+20
View File
@@ -3,6 +3,8 @@
#include <curl/curl.h>
#include <dl.h>
#include "io/dl_json_imp.h"
#define TO_STRING(a) #a
namespace dl
@@ -191,4 +193,22 @@ namespace dl
return true;
}
Http::RecvData::RecvData(BufferView buf)
{
json j = dl::Json::Parse(buf);
dl::Json::Get(j, "Code", _code);
dl::Json::Get(j, "Msg", _msg);
dl::Json::Get(j, "Data", _data);
}
std::string Http::RecvData::DumpError()
{
std::string d;
if (_data.is_string())
d = _data;
else
d = _data.dump();
return fmt::format("{},{}({})", _msg, d, _code);
}
}
+4 -17
View File
@@ -18,6 +18,7 @@
#include "io/dl_log.h"
#include "memory/dl_buffer.h"
#include "io/dl_json.h"
#include "io/dl_json_imp.h"
namespace dl
{
@@ -60,7 +61,7 @@ public:
}
void SetDataSend(const json& j)
{
SetDataSend(CreateBufferView(j.dump()));
SetDataSend(CreateBufferView(Json::Dump(j)));
}
void SetHeader(const std::vector<std::string>& vec_header)
{
@@ -118,13 +119,7 @@ public:
std::string _msg;
json _data;
RecvData(BufferView buf)
{
json j = dl::Json::Parse(buf);
dl::Json::Get(j, "Code", _code);
dl::Json::Get(j, "Msg", _msg);
dl::Json::Get(j, "Data", _data);
}
RecvData(BufferView buf);
bool Check(int code = 0)
{
@@ -136,15 +131,7 @@ public:
return true;
}
std::string DumpError()
{
std::string d;
if (_data.is_string())
d = _data;
else
d = _data.dump();
return fmt::format("{},{}({})", _msg, d, _code);
}
std::string DumpError();
};
private:
std::string _pathCACert; // 证书路径
+17
View File
@@ -1,5 +1,7 @@
#include "io/dl_json.h"
#include <nlohmann/json.hpp>
#include "io/dl_file.h"
#include "io/dl_log.h"
#include "base/dl_string.h"
@@ -71,4 +73,19 @@ bool SaveFile(const json& j, std::string_view path_name, bool binary /*= false*/
return File::SaveString(j.dump(4), path_name);
}
std::string Dump(const json& j)
{
j.dump(4, ' ', false, nlohmann::json::error_handler_t::replace);
return j.dump(4);
}
std::string Dump(BufferView buf)
{
json j = Parse(buf);
if (j.is_null())
return {};
return Dump(j);
}
}
+9 -65
View File
@@ -11,13 +11,13 @@
*/
#pragma once
#include <json.hpp>
#include <nlohmann/json_fwd.hpp>
using json = nlohmann::json;
#include "io/dl_log.h"
#include "base/dl_array.h"
#include "memory/dl_buffer.h"
using json = nlohmann::json;
//! json格式
namespace dl::Json
@@ -65,65 +65,21 @@ json ParseString(std::string_view str);
* @brief 从json返回子元素值,不存在则返回false,打印日志
*/
template<typename T>
bool Get(const json& j, const std::string& name, T& t)
{
auto iter = j.find(name);
if (iter == j.end())
{
if(g_bJsonLog)
log_err("json取值失败:{}", name);
return false;
}
try
{
t = iter->get<T>();
return true;
}
catch (nlohmann::detail::exception& e)
{
// 存在但失败,必须报错
log_err("json取值失败({}): {}", name, e.what());
}
return false;
}
bool Get(const json& j, const std::string& name, T& t);
/**
* @brief 从json返回值,不存在则返回false,打印日志
*/
template<typename T>
bool Cast(const json& j, T& t)
{
try
{
t = j;
return true;
}
catch (nlohmann::detail::exception& e)
{
// 存在但失败,必须报错
log_err("json取值失败: {}", e.what());
}
return false;
}
bool Cast(const json& j, T& t);
/**
* @brief dump
*/
inline std::string Dump(json& j)
{
j.dump(4, ' ', false, nlohmann::json::error_handler_t::replace);
return j.dump(4);
}
std::string Dump(const json& j);
/**
* @brief dump
*/
inline std::string Dump(BufferView buf)
{
json j = Parse(buf);
if (j.is_null())
return {};
return Dump(j);
}
std::string Dump(BufferView buf);
/**
* @brief 保存到文件
@@ -136,21 +92,9 @@ bool SaveFile(const json& j, std::string_view path_name, bool binary = false);
namespace dl
{
template<typename T, size_t N>
void from_json(const json& j, Array<T, N>& arr)
{
for (size_t i = 0; i < N; ++i)
{
arr[i] = j.at(i).get<T>();
}
}
void from_json(const json& j, Array<T, N>& arr);
template<typename T, size_t N>
void to_json(json& j, const Array<T, N>& arr)
{
j = json::array();
for (size_t i = 0; i < N; ++i)
{
j.push_back(arr[i]);
}
}
void to_json(json& j, const Array<T, N>& arr);
}
+81
View File
@@ -0,0 +1,81 @@
/**
* @file dl_json_imp.h
* @brief json格式相关
*
*
* @version 1.0
* @author lveyou
* @date 26-12-02
*
* @note 基于 nlohmann::json
*/
#pragma once
#include <nlohmann/json.hpp>
namespace dl::Json
{
template<typename T>
bool Get(const json& j, const std::string& name, T& t)
{
auto iter = j.find(name);
if (iter == j.end())
{
if (g_bJsonLog)
log_err("json取值失败:{}", name);
return false;
}
try
{
t = iter->get<T>();
return true;
}
catch (nlohmann::detail::exception& e)
{
// 存在但失败,必须报错
log_err("json取值失败({}): {}", name, e.what());
}
return false;
}
template<typename T>
bool Cast(const json& j, T& t)
{
try
{
t = j;
return true;
}
catch (nlohmann::detail::exception& e)
{
// 存在但失败,必须报错
log_err("json取值失败: {}", e.what());
}
return false;
}
}
namespace dl
{
template<typename T, size_t N>
void from_json(const json& j, Array<T, N>& arr)
{
for (size_t i = 0; i < N; ++i)
{
arr[i] = j.at(i).get<T>();
}
}
template<typename T, size_t N>
void to_json(json& j, const Array<T, N>& arr)
{
j = json::array();
for (size_t i = 0; i < N; ++i)
{
j.push_back(arr[i]);
}
}
}
+14 -1
View File
@@ -1,7 +1,20 @@
#include "dl_json_config.h"
#include "io/dl_json_imp.h"
namespace dl
{
bool JsonConfig::Load(std::string_view path_name)
{
json j = Json::ParseFile(path_name);
if (j.is_null())
return false;
_root = _parse_node(j, "__root");
return true;
}
std::string JsonConfig::GenerateCode(const GenerateParam& param)
{
std::string str;
@@ -230,7 +243,7 @@ namespace dl
#include <string>
#include <map>
#include <json.hpp>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
)", param._filename, param._brief, param._version, param._author, param._date, param._note);
+1 -9
View File
@@ -50,15 +50,7 @@ public:
* @brief 加载原始配置
* @note
*/
bool Load(std::string_view path_name)
{
json j = Json::ParseFile(path_name);
if (j.is_null())
return false;
_root = _parse_node(j, "__root");
return true;
}
bool Load(std::string_view path_name);
/**
* @brief 生成C++代码
+1
View File
@@ -6,6 +6,7 @@
#include "render/dl_factory.h"
#include "script/dl_lua.h"
#include "misc/dl_language.h"
#include "io/dl_json_imp.h"
namespace dl
{
+2
View File
@@ -1,5 +1,7 @@
#include "dl_value.h"
#include "io/dl_json_imp.h"
namespace dl
{
+3 -1
View File
@@ -1,5 +1,7 @@
#include "dl_animation_info.h"
#include "io/dl_json_imp.h"
namespace dl
{
@@ -59,7 +61,7 @@ namespace dl
auto iter1 = info._allAni.find(name);
if (iter1 != info._allAni.end())
{
log_warn("子动画名重复,将覆盖:{}", name);
log_warn("子动画名重复,将覆盖:{}", name);
}
info._allAni[name] = node;
}
+1
View File
@@ -10,6 +10,7 @@
#include <tiny_gltf_v3.h>
#include "math/dl_transform.h"
#include "io/dl_json_imp.h"
namespace dl
{
+1 -1
View File
@@ -16,7 +16,7 @@
#include <string>
#include <map>
#include <json.hpp>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#define LUA_USE_APICHECK
+2 -5
View File
@@ -9,6 +9,7 @@
#include "memory/dl_crypto.h"
#include "memory/dl_memory.h"
#include "tool/dl_tool_archiver.h"
#include "io/dl_json_imp.h"
namespace luabridge {
@@ -1010,10 +1011,6 @@ namespace dl
.addVariable("AXIS_Z", ColorDef::AXIS_Z)
.endNamespace();
}
#if DL_PLATFORM == DL_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif
void lua_pause()
{
@@ -1022,7 +1019,7 @@ namespace dl
void lua_register_platform(lua_State* L)
{
luabridge::getGlobalNamespace(L)
.addFunction("system", system);
.addFunction("system", std::system);
luabridge::getGlobalNamespace(L)
.addFunction("pause", lua_pause);
+1
View File
@@ -10,6 +10,7 @@
#include "io/dl_file.h"
#include "dl_audio_loader.h"
#include "render/dl_factory.h"
#include "io/dl_json_imp.h"
namespace dl
{
+1
View File
@@ -13,6 +13,7 @@
#include "misc/dl_tester.h"
#include "io/dl_json.h"
#include "io/dl_json_imp.h"
#include "base/dl_string.h"
#include "io/dl_http.h"
#include "io/dl_file.h"
-4
View File
@@ -166,10 +166,6 @@ void Time::_cacl_expect()
_imp->_deltaExpectNS = (std::chrono::microseconds)(int)(_deltaExpect * 1000000);
}
#if DL_PLATFORM == DL_PLATFORM_WIN32
#include <windows.h>
#endif
void Time::_sleep()
{
using namespace std::chrono;
+1
View File
@@ -25,6 +25,7 @@
#include "dl_ui_background.h"
#include "render/dl_factory.h"
#include "io/dl_json.h"
#include "io/dl_json_imp.h"
#include "ui/dl_ui_panel.h"
#include "ui/dl_ui_sprite.h"
#include "ui/dl_ui_text.h"
+1 -1
View File
@@ -70,7 +70,7 @@ target_link_libraries(${PROJECT_NAME} dlx)
# fmt
include_directories(${CMAKE_SOURCE_DIR}/third/fmt/include)
# json
include_directories(${CMAKE_SOURCE_DIR}/third/json)
include_directories(${CMAKE_SOURCE_DIR}/third/json/include)
# tinyxml2
include_directories(${CMAKE_SOURCE_DIR}/third/tinyxml2)
# asio