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::generic*>(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
@@ -34,18 +33,29 @@ namespace REX::JSON
3433 template void SettingLoad<std::int16_t >(void *, path_t , std::int16_t &, std::int16_t &);
3534 template void SettingLoad<std::int32_t >(void *, path_t , std::int32_t &, std::int32_t &);
3635 template void SettingLoad<std::string>(void *, path_t , std::string&, std::string&);
36+ template void SettingLoad<std::vector<bool >>(void *, path_t , std::vector<bool >&, std::vector<bool >&);
37+ template void SettingLoad<std::vector<float >>(void *, path_t , std::vector<float >&, std::vector<float >&);
38+ template void SettingLoad<std::vector<double >>(void *, path_t , std::vector<double >&, std::vector<double >&);
39+ template void SettingLoad<std::vector<std::uint8_t >>(void *, path_t , std::vector<std::uint8_t >&, std::vector<std::uint8_t >&);
40+ template void SettingLoad<std::vector<std::uint16_t >>(void *, path_t , std::vector<std::uint16_t >&, std::vector<std::uint16_t >&);
41+ template void SettingLoad<std::vector<std::uint32_t >>(void *, path_t , std::vector<std::uint32_t >&, std::vector<std::uint32_t >&);
42+ template void SettingLoad<std::vector<std::int8_t >>(void *, path_t , std::vector<std::int8_t >&, std::vector<std::int8_t >&);
43+ template void SettingLoad<std::vector<std::int16_t >>(void *, path_t , std::vector<std::int16_t >&, std::vector<std::int16_t >&);
44+ template void SettingLoad<std::vector<std::int32_t >>(void *, path_t , std::vector<std::int32_t >&, std::vector<std::int32_t >&);
45+ template void SettingLoad<std::vector<std::string>>(void *, path_t , std::vector<std::string>&, std::vector<std::string>&);
3746
3847 template <class T >
3948 void SettingSave<T>(
4049 void * a_data,
4150 path_t a_path,
4251 T& a_value)
4352 {
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;
53+ auto & json = *static_cast <glz::generic*>(a_data);
54+ if (a_path[0 ] != ' /' ) {
55+ const auto path = std::format (" /{}" sv, a_path);
56+ glz::set (json, path, a_value);
4757 } else {
48- json[ a_path] = a_value;
58+ glz::set ( json, a_path, a_value) ;
4959 }
5060 }
5161
@@ -59,49 +69,52 @@ namespace REX::JSON
5969 template void SettingSave<std::int16_t >(void *, path_t , std::int16_t &);
6070 template void SettingSave<std::int32_t >(void *, path_t , std::int32_t &);
6171 template void SettingSave<std::string>(void *, path_t , std::string&);
72+ template void SettingSave<std::vector<bool >>(void *, path_t , std::vector<bool >&);
73+ template void SettingSave<std::vector<float >>(void *, path_t , std::vector<float >&);
74+ template void SettingSave<std::vector<double >>(void *, path_t , std::vector<double >&);
75+ template void SettingSave<std::vector<std::uint8_t >>(void *, path_t , std::vector<std::uint8_t >&);
76+ template void SettingSave<std::vector<std::uint16_t >>(void *, path_t , std::vector<std::uint16_t >&);
77+ template void SettingSave<std::vector<std::uint32_t >>(void *, path_t , std::vector<std::uint32_t >&);
78+ template void SettingSave<std::vector<std::int8_t >>(void *, path_t , std::vector<std::int8_t >&);
79+ template void SettingSave<std::vector<std::int16_t >>(void *, path_t , std::vector<std::int16_t >&);
80+ template void SettingSave<std::vector<std::int32_t >>(void *, path_t , std::vector<std::int32_t >&);
81+ template void SettingSave<std::vector<std::string>>(void *, path_t , std::vector<std::string>&);
6282 }
6383
6484 void SettingStore::Load ()
6585 {
6686 if (std::filesystem::exists (m_fileBase)) {
67- std::ifstream file{ m_fileBase.data () };
68- try {
69- auto result = nlohmann::json::parse (file);
87+ glz::generic result{};
88+ if (!glz::read_file_json (result, m_fileBase, std::string{})) {
7089 for (auto setting : m_settings) {
7190 setting->Load (&result, true );
7291 }
73- } catch (const std::exception& e) {
74- REX::ERROR (" {}" , e.what ());
7592 }
7693 }
7794
7895 if (std::filesystem::exists (m_fileUser)) {
79- std::ifstream file{ m_fileUser.data () };
80- try {
81- auto result = nlohmann::json::parse (file);
96+ glz::generic result{};
97+ if (!glz::read_file_json (result, m_fileUser, std::string{})) {
8298 for (auto setting : m_settings) {
8399 setting->Load (&result, false );
84100 }
85- } catch (const std::exception& e) {
86- REX::ERROR (" {}" , e.what ());
87101 }
88102 }
89103 }
90104
91105 void SettingStore::Save ()
92106 {
93- nlohmann::json output{};
107+ glz::generic output{};
94108 if (std::filesystem::exists (m_fileBase)) {
95- std::ifstream file{ m_fileBase.data () };
96- output = nlohmann::json::parse (file);
109+ (void )glz::read_file_json (output, m_fileBase, std::string{});
97110 }
98111
99112 for (auto & setting : m_settings) {
100113 setting->Save (&output);
101114 }
102115
103- std::ofstream file{ m_fileBase. data (), std::ios::trunc };
104- file << std::setw ( 4 ) << output ;
116+ constexpr glz::opts opts{ . prettify = true , . indentation_width = 4 };
117+ ( void )glz::write_file_json<opts>(output, m_fileBase, std::string{}) ;
105118 }
106119}
107120#endif
0 commit comments