11#include " REX/REX/JSON.h"
22
3- #include " REX/REX/LOG.h"
4-
53#ifdef COMMONLIB_OPTION_JSON
6- # include < nlohmann/json .hpp>
4+ # include < glaze/glaze .hpp>
75
86namespace REX ::JSON
97{
@@ -16,11 +14,12 @@ namespace REX::JSON
1614 T& a_value,
1715 T& a_valueDefault)
1816 {
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);
17+ const auto & json = *static_cast <glz::json_t *>(a_data);
18+ if (a_path[0 ] != ' /' ) {
19+ const auto path = std::format (" /{}" sv, a_path);
20+ a_value = glz::get<T>(json, path).value_or (a_valueDefault);
2221 } else {
23- a_value = json. value <T>(a_path, a_valueDefault);
22+ a_value = glz::get <T>(json, a_path). value_or ( a_valueDefault);
2423 }
2524 }
2625
@@ -41,11 +40,12 @@ namespace REX::JSON
4140 path_t a_path,
4241 T& a_value)
4342 {
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;
43+ auto & json = *static_cast <glz::json_t *>(a_data);
44+ if (a_path[0 ] != ' /' ) {
45+ const auto path = std::format (" /{}" sv, a_path);
46+ glz::set (json, path, a_value);
4747 } else {
48- json[ a_path] = a_value;
48+ glz::set ( json, a_path, a_value) ;
4949 }
5050 }
5151
@@ -64,44 +64,37 @@ namespace REX::JSON
6464 void SettingStore::Load ()
6565 {
6666 if (std::filesystem::exists (m_fileBase)) {
67- std::ifstream file{ m_fileBase.data () };
68- try {
69- auto result = nlohmann::json::parse (file);
67+ glz::json_t result;
68+ if (!glz::read_file_json (result, m_fileBase, std::string{})) {
7069 for (auto setting : m_settings) {
7170 setting->Load (&result, true );
7271 }
73- } catch (const std::exception& e) {
74- REX::ERROR (" {}" , e.what ());
7572 }
7673 }
7774
7875 if (std::filesystem::exists (m_fileUser)) {
79- std::ifstream file{ m_fileUser.data () };
80- try {
81- auto result = nlohmann::json::parse (file);
76+ glz::json_t result;
77+ if (!glz::read_file_json (result, m_fileUser, std::string{})) {
8278 for (auto setting : m_settings) {
8379 setting->Load (&result, false );
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+ (void )glz::read_file_json (output, m_fileBase, std::string{});
9790 }
9891
9992 for (auto & setting : m_settings) {
10093 setting->Save (&output);
10194 }
10295
103- std::ofstream file{ m_fileBase. data (), std::ios::trunc };
104- file << std::setw ( 4 ) << output ;
96+ constexpr glz::opts opts{ . prettify = true , . indentation_width = 4 };
97+ ( void )glz::write_file_json<opts>(output, m_fileBase, std::string{}) ;
10598 }
10699}
107100#endif
0 commit comments