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,11 +16,12 @@ 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);
19+ const auto & json = *static_cast <glz::json_t *>(a_data);
20+ if (a_path[0 ] != ' /' ) {
21+ const auto path = std::format (" /{}" sv, a_path);
22+ a_value = glz::get<T>(json, path).value_or (a_valueDefault);
2223 } else {
23- a_value = json. value <T>(a_path, a_valueDefault);
24+ a_value = glz::get <T>(json, a_path). value_or ( a_valueDefault);
2425 }
2526 }
2627
@@ -41,11 +42,12 @@ namespace REX::JSON
4142 path_t a_path,
4243 T& a_value)
4344 {
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;
45+ auto & json = *static_cast <glz::json_t *>(a_data);
46+ if (a_path[0 ] != ' /' ) {
47+ const auto path = std::format (" /{}" sv, a_path);
48+ glz::set (json, path, a_value);
4749 } else {
48- json[ a_path] = a_value;
50+ glz::set ( json, a_path, a_value) ;
4951 }
5052 }
5153
@@ -64,44 +66,36 @@ namespace REX::JSON
6466 void SettingStore::Load ()
6567 {
6668 if (std::filesystem::exists (m_fileBase)) {
67- std::ifstream file{ m_fileBase.data () };
68- try {
69- auto result = nlohmann::json::parse (file);
69+ glz::json_t result;
70+ if (!glz::read_file_json (result, m_fileBase, std::string{})) {
7071 for (auto setting : m_settings) {
7172 setting->Load (&result, true );
7273 }
73- } catch (const std::exception& e) {
74- REX::ERROR (" {}" , e.what ());
7574 }
7675 }
7776
7877 if (std::filesystem::exists (m_fileUser)) {
79- std::ifstream file{ m_fileUser.data () };
80- try {
81- auto result = nlohmann::json::parse (file);
78+ glz::json_t result;
79+ if (!glz::read_file_json (result, m_fileUser, std::string{})) {
8280 for (auto setting : m_settings) {
83- setting->Load (&result, false );
81+ setting->Load (&result, true );
8482 }
85- } catch (const std::exception& e) {
86- REX::ERROR (" {}" , e.what ());
8783 }
8884 }
8985 }
9086
9187 void SettingStore::Save ()
9288 {
93- nlohmann::json output{} ;
89+ glz:: json_t output;
9490 if (std::filesystem::exists (m_fileBase)) {
95- std::ifstream file{ m_fileBase.data () };
96- output = nlohmann::json::parse (file);
91+ (void )glz::read_file_json (output, m_fileBase, std::string{});
9792 }
9893
9994 for (auto & setting : m_settings) {
10095 setting->Save (&output);
10196 }
10297
103- std::ofstream file{ m_fileBase.data (), std::ios::trunc };
104- file << std::setw (4 ) << output;
98+ (void )glz::write_file_json (output, m_fileBase, std::string{});
10599 }
106100}
107101#endif
0 commit comments