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 fddea15..bfc3457 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); } } @@ -34,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( @@ -41,11 +50,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); } } @@ -59,49 +69,52 @@ 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() { if (std::filesystem::exists(m_fileBase)) { - std::ifstream file{ m_fileBase.data() }; - try { - auto result = nlohmann::json::parse(file); + glz::generic 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::generic 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::generic 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 5a5b437..e50e4e4 100644 --- a/xmake.lua +++ b/xmake.lua @@ -39,7 +39,7 @@ add_requireconfs("*.cmake", { configs = { override = true, system = false } }) -- 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 @@ -57,7 +57,7 @@ target("commonlib-shared", function() end if has_config("commonlib_json") then - add_packages("nlohmann_json", { public = true }) + add_packages("glaze", { public = true }) add_defines("COMMONLIB_OPTION_JSON=1", { public = true }) end