From 09227eebe4285a53322af5aec3610cbc52406c97 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 <2724172+shad0wshayd3@users.noreply.github.com> Date: Fri, 13 Jun 2025 18:32:42 -0600 Subject: [PATCH 1/3] refactor: replace `nlohmann_json` with `glaze` --- src/REX/REX/JSON.cpp | 45 +++++++++++++++++++------------------------- xmake.lua | 4 ++-- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/REX/REX/JSON.cpp b/src/REX/REX/JSON.cpp index fddea15..f33040e 100644 --- a/src/REX/REX/JSON.cpp +++ b/src/REX/REX/JSON.cpp @@ -1,9 +1,7 @@ #include "REX/REX/JSON.h" -#include "REX/REX/LOG.h" - #ifdef COMMONLIB_OPTION_JSON -# include +# include namespace REX::JSON { @@ -16,11 +14,12 @@ namespace REX::JSON T& a_value, T& a_valueDefault) { - const auto& json = *static_cast(a_data); - if (a_path[0] == '/') { - a_value = json.value(nlohmann::json::json_pointer(a_path.data()), a_valueDefault); + const auto& json = *static_cast(a_data); + if (a_path[0] != '/') { + const auto path = std::format("/{}"sv, a_path); + a_value = glz::get(json, path).value_or(a_valueDefault); } else { - a_value = json.value(a_path, a_valueDefault); + a_value = glz::get(json, a_path).value_or(a_valueDefault); } } @@ -41,11 +40,12 @@ namespace REX::JSON path_t a_path, T& a_value) { - auto& json = *static_cast(a_data); - if (a_path[0] == '/') { - json[nlohmann::json::json_pointer(a_path.data())] = a_value; + auto& json = *static_cast(a_data); + if (a_path[0] != '/') { + const auto path = std::format("/{}"sv, a_path); + glz::set(json, path, a_value); } else { - json[a_path] = a_value; + glz::set(json, a_path, a_value); } } @@ -64,44 +64,37 @@ namespace REX::JSON void SettingStore::Load() { if (std::filesystem::exists(m_fileBase)) { - std::ifstream file{ m_fileBase.data() }; - try { - auto result = nlohmann::json::parse(file); + glz::json_t result; + if (!glz::read_file_json(result, m_fileBase, std::string{})) { for (auto setting : m_settings) { setting->Load(&result, true); } - } catch (const std::exception& e) { - REX::ERROR("{}", e.what()); } } if (std::filesystem::exists(m_fileUser)) { - std::ifstream file{ m_fileUser.data() }; - try { - auto result = nlohmann::json::parse(file); + glz::json_t result; + if (!glz::read_file_json(result, m_fileUser, std::string{})) { for (auto setting : m_settings) { setting->Load(&result, false); } - } catch (const std::exception& e) { - REX::ERROR("{}", e.what()); } } } void SettingStore::Save() { - nlohmann::json output{}; + glz::json_t output; if (std::filesystem::exists(m_fileBase)) { - std::ifstream file{ m_fileBase.data() }; - output = nlohmann::json::parse(file); + (void)glz::read_file_json(output, m_fileBase, std::string{}); } for (auto& setting : m_settings) { setting->Save(&output); } - std::ofstream file{ m_fileBase.data(), std::ios::trunc }; - file << std::setw(4) << output; + constexpr glz::opts opts{ .prettify = true, .indentation_width = 4 }; + (void)glz::write_file_json(output, m_fileBase, std::string{}); } } #endif diff --git a/xmake.lua b/xmake.lua index ede97de..4c00c17 100644 --- a/xmake.lua +++ b/xmake.lua @@ -40,7 +40,7 @@ add_requires("spdlog", { configs = { header_only = false, wchar = true, std_form -- add config packages if has_config("commonlib_ini") then add_requires("simpleini") end -if has_config("commonlib_json") then add_requires("nlohmann_json") end +if has_config("commonlib_json") then add_requires("glaze") end if has_config("commonlib_toml") then add_requires("toml11") end if has_config("commonlib_xbyak") then add_requires("xbyak") end @@ -53,7 +53,7 @@ target("commonlib-shared", function() -- add config packages if has_config("commonlib_ini") then add_packages("simpleini", { public = true }) end - if has_config("commonlib_json") then add_packages("nlohmann_json", { public = true }) end + if has_config("commonlib_json") then add_packages("glaze", { public = true }) end if has_config("commonlib_toml") then add_packages("toml11", { public = true }) end if has_config("commonlib_xbyak") then add_packages("xbyak", { public = true }) end From 39ebcd1ed7eff7ec2e32b24e1599a2c9d5810320 Mon Sep 17 00:00:00 2001 From: shad0wshayd3 <2724172+shad0wshayd3@users.noreply.github.com> Date: Tue, 11 Nov 2025 21:29:48 -0700 Subject: [PATCH 2/3] fix: `glz::json_t` to `glz::generic` --- src/REX/REX/JSON.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/REX/REX/JSON.cpp b/src/REX/REX/JSON.cpp index f33040e..794ad78 100644 --- a/src/REX/REX/JSON.cpp +++ b/src/REX/REX/JSON.cpp @@ -14,7 +14,7 @@ namespace REX::JSON T& a_value, T& a_valueDefault) { - const auto& json = *static_cast(a_data); + const auto& json = *static_cast(a_data); if (a_path[0] != '/') { const auto path = std::format("/{}"sv, a_path); a_value = glz::get(json, path).value_or(a_valueDefault); @@ -40,7 +40,7 @@ namespace REX::JSON path_t a_path, T& a_value) { - auto& json = *static_cast(a_data); + auto& json = *static_cast(a_data); if (a_path[0] != '/') { const auto path = std::format("/{}"sv, a_path); glz::set(json, path, a_value); @@ -64,7 +64,7 @@ namespace REX::JSON void SettingStore::Load() { if (std::filesystem::exists(m_fileBase)) { - glz::json_t result; + glz::generic result{}; if (!glz::read_file_json(result, m_fileBase, std::string{})) { for (auto setting : m_settings) { setting->Load(&result, true); @@ -73,7 +73,7 @@ namespace REX::JSON } if (std::filesystem::exists(m_fileUser)) { - glz::json_t result; + glz::generic result{}; if (!glz::read_file_json(result, m_fileUser, std::string{})) { for (auto setting : m_settings) { setting->Load(&result, false); @@ -84,7 +84,7 @@ namespace REX::JSON void SettingStore::Save() { - glz::json_t output; + glz::generic output{}; if (std::filesystem::exists(m_fileBase)) { (void)glz::read_file_json(output, m_fileBase, std::string{}); } From dccd574da25bc2655af4e1d0be878af6580b23aa Mon Sep 17 00:00:00 2001 From: shad0wshayd3 <2724172+shad0wshayd3@users.noreply.github.com> Date: Tue, 11 Nov 2025 21:30:26 -0700 Subject: [PATCH 3/3] feat: `REX::JSON` array types --- include/REX/REX/JSON.h | 33 +++++++++++++++++++++++++++++++++ src/REX/REX/JSON.cpp | 20 ++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/REX/REX/JSON.h b/include/REX/REX/JSON.h index c02f734..3f85dce 100644 --- a/include/REX/REX/JSON.h +++ b/include/REX/REX/JSON.h @@ -83,6 +83,39 @@ namespace REX::JSON template using Str = Setting; + + template + using SettingA = Setting, Store>; + + template + using BoolA = SettingA; + + template + using F32A = SettingA; + + template + using F64A = SettingA; + + template + using I8A = SettingA; + + template + using I16A = SettingA; + + template + using I32A = SettingA; + + template + using U8A = SettingA; + + template + using U16A = SettingA; + + template + using U32A = SettingA; + + template + using StrA = SettingA; } template diff --git a/src/REX/REX/JSON.cpp b/src/REX/REX/JSON.cpp index 794ad78..bfc3457 100644 --- a/src/REX/REX/JSON.cpp +++ b/src/REX/REX/JSON.cpp @@ -33,6 +33,16 @@ namespace REX::JSON template void SettingLoad(void*, path_t, std::int16_t&, std::int16_t&); template void SettingLoad(void*, path_t, std::int32_t&, std::int32_t&); template void SettingLoad(void*, path_t, std::string&, std::string&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); + template void SettingLoad>(void*, path_t, std::vector&, std::vector&); template void SettingSave( @@ -59,6 +69,16 @@ namespace REX::JSON template void SettingSave(void*, path_t, std::int16_t&); template void SettingSave(void*, path_t, std::int32_t&); template void SettingSave(void*, path_t, std::string&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); + template void SettingSave>(void*, path_t, std::vector&); } void SettingStore::Load()