33#include " REX/REX/LOG.h"
44
55#ifdef COMMONLIB_OPTION_JSON
6- # include < nlohmann/json .hpp>
6+ # include < glaze/glaze .hpp>
77
88namespace REX ::JSON
99{
@@ -16,12 +16,8 @@ namespace REX::JSON
1616 T& a_value,
1717 T& a_valueDefault)
1818 {
19- const auto & json = *static_cast <nlohmann::json*>(a_data);
20- if (a_path[0 ] == ' /' ) {
21- a_value = json.value <T>(nlohmann::json::json_pointer (a_path.data ()), a_valueDefault);
22- } else {
23- a_value = json.value <T>(a_path, a_valueDefault);
24- }
19+ const auto & json = *static_cast <glz::json_t *>(a_data);
20+ a_value = glz::get<T>(json, a_path).value_or (a_valueDefault);
2521 }
2622
2723 template void SettingLoad<bool >(void *, path_t , bool &, bool &);
@@ -41,12 +37,8 @@ namespace REX::JSON
4137 path_t a_path,
4238 T& a_value)
4339 {
44- auto & json = *static_cast <nlohmann::json*>(a_data);
45- if (a_path[0 ] == ' /' ) {
46- json[nlohmann::json::json_pointer (a_path.data ())] = a_value;
47- } else {
48- json[a_path] = a_value;
49- }
40+ const auto & json = *static_cast <glz::json_t *>(a_data);
41+ glz::set (json, a_path, a_value);
5042 }
5143
5244 template void SettingSave<bool >(void *, path_t , bool &);
@@ -64,44 +56,48 @@ namespace REX::JSON
6456 void SettingStore::Load ()
6557 {
6658 if (std::filesystem::exists (m_fileBase)) {
67- std::ifstream file{ m_fileBase.data () };
68- try {
69- auto result = nlohmann::json::parse (file);
59+ glz::json_t result;
60+ std::string buffer;
61+ const auto error = glz::read_file_json (result, m_fileBase, buffer);
62+ if (error) {
63+ REX::ERROR (" {}" sv, glz::format_error (error, buffer));
64+ } else {
7065 for (auto setting : m_settings) {
7166 setting->Load (&result, true );
7267 }
73- } catch (const std::exception& e) {
74- REX::ERROR (" {}" , e.what ());
7568 }
7669 }
7770
7871 if (std::filesystem::exists (m_fileUser)) {
79- std::ifstream file{ m_fileUser.data () };
80- try {
81- auto result = nlohmann::json::parse (file);
72+ glz::json_t result;
73+ std::string buffer;
74+ const auto error = glz::read_file_json (result, m_fileUser, buffer);
75+ if (error) {
76+ REX::ERROR (" {}" sv, glz::format_error (error, buffer));
77+ } else {
8278 for (auto setting : m_settings) {
83- setting->Load (&result, false );
79+ setting->Load (&result, true );
8480 }
85- } catch (const std::exception& e) {
86- REX::ERROR (" {}" , e.what ());
8781 }
8882 }
8983 }
9084
9185 void SettingStore::Save ()
9286 {
93- nlohmann::json output{} ;
87+ glz:: json_t output;
9488 if (std::filesystem::exists (m_fileBase)) {
95- std::ifstream file{ m_fileBase.data () };
96- output = nlohmann::json::parse (file);
89+ std::string buffer;
90+ const auto error = glz::read_file_json (output, m_fileBase, buffer);
91+ if (error) {
92+ REX::ERROR (" {}" sv, glz::format_error (error, buffer));
93+ }
9794 }
9895
9996 for (auto & setting : m_settings) {
10097 setting->Save (&output);
10198 }
10299
103- std::ofstream file{ m_fileBase.data (), std::ios::trunc };
104- file << std::setw (4 ) << output;
100+ glz::write_file_json (output, m_fileBase, std::string{});
105101 }
106102}
107103#endif
0 commit comments