diff --git a/CMakeLists.txt b/CMakeLists.txt index a2f11c30..94b796af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 内存泄漏检测库----------") diff --git a/dl/CMakeLists.txt b/dl/CMakeLists.txt index fbada706..289eb44d 100644 --- a/dl/CMakeLists.txt +++ b/dl/CMakeLists.txt @@ -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 diff --git a/dl/io/dl_http.cpp b/dl/io/dl_http.cpp index ca0a8681..50279868 100644 --- a/dl/io/dl_http.cpp +++ b/dl/io/dl_http.cpp @@ -3,6 +3,8 @@ #include #include +#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); + } + } \ No newline at end of file diff --git a/dl/io/dl_http.h b/dl/io/dl_http.h index 3029b03c..f70ee312 100644 --- a/dl/io/dl_http.h +++ b/dl/io/dl_http.h @@ -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& 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; // 证书路径 diff --git a/dl/io/dl_json.cpp b/dl/io/dl_json.cpp index a5918e69..e79c97d4 100644 --- a/dl/io/dl_json.cpp +++ b/dl/io/dl_json.cpp @@ -1,5 +1,7 @@ #include "io/dl_json.h" +#include + #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); +} + } \ No newline at end of file diff --git a/dl/io/dl_json.h b/dl/io/dl_json.h index a119c3af..d165fd21 100644 --- a/dl/io/dl_json.h +++ b/dl/io/dl_json.h @@ -11,13 +11,13 @@ */ #pragma once -#include +#include +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 -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(); - 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 -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 - void from_json(const json& j, Array& arr) - { - for (size_t i = 0; i < N; ++i) - { - arr[i] = j.at(i).get(); - } - } + void from_json(const json& j, Array& arr); + template - void to_json(json& j, const Array& arr) - { - j = json::array(); - for (size_t i = 0; i < N; ++i) - { - j.push_back(arr[i]); - } - } + void to_json(json& j, const Array& arr); } diff --git a/dl/io/dl_json_imp.h b/dl/io/dl_json_imp.h new file mode 100644 index 00000000..6a8fb36a --- /dev/null +++ b/dl/io/dl_json_imp.h @@ -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 + +namespace dl::Json +{ + + template + 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(); + return true; + } + catch (nlohmann::detail::exception& e) + { + // 存在但失败,必须报错 + log_err("json取值失败({}): {}", name, e.what()); + } + return false; + } + + template + 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 + void from_json(const json& j, Array& arr) + { + for (size_t i = 0; i < N; ++i) + { + arr[i] = j.at(i).get(); + } + } + + template + void to_json(json& j, const Array& arr) + { + j = json::array(); + for (size_t i = 0; i < N; ++i) + { + j.push_back(arr[i]); + } + } + +} \ No newline at end of file diff --git a/dl/misc/dl_json_config.cpp b/dl/misc/dl_json_config.cpp index abd66bdd..649ec8cb 100644 --- a/dl/misc/dl_json_config.cpp +++ b/dl/misc/dl_json_config.cpp @@ -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 #include -#include +#include using json = nlohmann::json; )", param._filename, param._brief, param._version, param._author, param._date, param._note); diff --git a/dl/misc/dl_json_config.h b/dl/misc/dl_json_config.h index f04801fe..4a1b7026 100644 --- a/dl/misc/dl_json_config.h +++ b/dl/misc/dl_json_config.h @@ -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++代码 diff --git a/dl/misc/dl_mod.cpp b/dl/misc/dl_mod.cpp index d298ed6a..5b7ab3f9 100644 --- a/dl/misc/dl_mod.cpp +++ b/dl/misc/dl_mod.cpp @@ -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 { diff --git a/dl/misc/dl_value.cpp b/dl/misc/dl_value.cpp index c30975a8..83e569fd 100644 --- a/dl/misc/dl_value.cpp +++ b/dl/misc/dl_value.cpp @@ -1,5 +1,7 @@ #include "dl_value.h" +#include "io/dl_json_imp.h" + namespace dl { diff --git a/dl/render/dl_animation_info.cpp b/dl/render/dl_animation_info.cpp index a4746dda..cdafa29d 100644 --- a/dl/render/dl_animation_info.cpp +++ b/dl/render/dl_animation_info.cpp @@ -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; } diff --git a/dl/render/dl_model_loader.cpp b/dl/render/dl_model_loader.cpp index 48251724..137d8eb3 100644 --- a/dl/render/dl_model_loader.cpp +++ b/dl/render/dl_model_loader.cpp @@ -10,6 +10,7 @@ #include #include "math/dl_transform.h" +#include "io/dl_json_imp.h" namespace dl { diff --git a/dl/render/dl_particle_config.h b/dl/render/dl_particle_config.h index aee782a2..56a09525 100644 --- a/dl/render/dl_particle_config.h +++ b/dl/render/dl_particle_config.h @@ -16,7 +16,7 @@ #include #include -#include +#include using json = nlohmann::json; #define LUA_USE_APICHECK diff --git a/dl/script/dl_lua_register.cpp b/dl/script/dl_lua_register.cpp index e7e201d0..fc8d5f77 100644 --- a/dl/script/dl_lua_register.cpp +++ b/dl/script/dl_lua_register.cpp @@ -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 -#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); diff --git a/dl/system/dl_audio.cpp b/dl/system/dl_audio.cpp index a595b0cf..819d4afb 100644 --- a/dl/system/dl_audio.cpp +++ b/dl/system/dl_audio.cpp @@ -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 { diff --git a/dl/system/dl_test.h b/dl/system/dl_test.h index dbd32e03..72e03690 100644 --- a/dl/system/dl_test.h +++ b/dl/system/dl_test.h @@ -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" diff --git a/dl/system/dl_time.cpp b/dl/system/dl_time.cpp index cb70de15..1e7c9ec7 100644 --- a/dl/system/dl_time.cpp +++ b/dl/system/dl_time.cpp @@ -166,10 +166,6 @@ void Time::_cacl_expect() _imp->_deltaExpectNS = (std::chrono::microseconds)(int)(_deltaExpect * 1000000); } -#if DL_PLATFORM == DL_PLATFORM_WIN32 -#include -#endif - void Time::_sleep() { using namespace std::chrono; diff --git a/dl/ui/dl_ui_manager.h b/dl/ui/dl_ui_manager.h index f3c17959..7f954b1d 100644 --- a/dl/ui/dl_ui_manager.h +++ b/dl/ui/dl_ui_manager.h @@ -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" diff --git a/project/project.cmake b/project/project.cmake index 6fe935fb..b1dafdd8 100644 --- a/project/project.cmake +++ b/project/project.cmake @@ -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