Skip to content

Commit 2081335

Browse files
committed
feat: clean JSON impl
1 parent 570acd0 commit 2081335

2 files changed

Lines changed: 14 additions & 40 deletions

File tree

include/REX/REX/JSON.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ namespace REX::JSON
3030
{
3131
public:
3232
Setting(path_t a_path, T a_default) :
33-
TSetting<T, Store>(a_default),
34-
m_path(a_path)
35-
{}
33+
TSetting<T, Store>(a_default)
34+
{
35+
if (a_path[0] != '/') {
36+
m_path = std::format("/{}"sv, a_path);
37+
} else {
38+
m_path = std::move(a_path);
39+
}
40+
}
3641

3742
public:
3843
virtual void Load(void* a_data, bool a_isBase) override

src/REX/REX/JSON.cpp

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,7 @@ namespace REX::JSON
1717
T& a_valueDefault)
1818
{
1919
const auto& json = *static_cast<glz::json_t*>(a_data);
20-
if (a_path[0] == '/') {
21-
a_value = glz::get<T>(json, a_path).value_or(a_valueDefault);
22-
} else {
23-
auto path = std::format("/{}"sv, a_path);
24-
REX::DEBUG("reading from {}", path);
25-
auto value = glz::get<T>(json, path);
26-
REX::DEBUG("found: {}", value.has_value());
27-
REX::DEBUG("test output1: {}", value->get());
28-
REX::DEBUG("test output2: {}", value.value_or(a_valueDefault).get());
29-
a_value = value.value_or(a_valueDefault);
30-
}
20+
a_value = glz::get<T>(json, a_path).value_or(a_valueDefault);
3121
}
3222

3323
template void SettingLoad<bool>(void*, path_t, bool&, bool&);
@@ -48,12 +38,7 @@ namespace REX::JSON
4838
T& a_value)
4939
{
5040
auto& json = *static_cast<glz::json_t*>(a_data);
51-
if (a_path[0] == '/') {
52-
glz::set(json, a_path, a_value);
53-
} else {
54-
auto path = std::format("/{}"sv, a_path);
55-
glz::set(json, path, a_value);
56-
}
41+
glz::set(json, a_path, a_value);
5742
}
5843

5944
template void SettingSave<bool>(void*, path_t, bool&);
@@ -72,11 +57,7 @@ namespace REX::JSON
7257
{
7358
if (std::filesystem::exists(m_fileBase)) {
7459
glz::json_t result;
75-
std::string buffer;
76-
const auto errorc = glz::read_file_json(result, m_fileBase, buffer);
77-
if (errorc) {
78-
REX::ERROR("{}"sv, glz::format_error(errorc, buffer));
79-
} else {
60+
if (!glz::read_file_json(result, m_fileBase, std::string{})) {
8061
for (auto setting : m_settings) {
8162
setting->Load(&result, true);
8263
}
@@ -85,11 +66,7 @@ namespace REX::JSON
8566

8667
if (std::filesystem::exists(m_fileUser)) {
8768
glz::json_t result;
88-
std::string buffer;
89-
const auto errorc = glz::read_file_json(result, m_fileUser, buffer);
90-
if (errorc) {
91-
REX::ERROR("{}"sv, glz::format_error(errorc, buffer));
92-
} else {
69+
if (!glz::read_file_json(result, m_fileUser, std::string{})) {
9370
for (auto setting : m_settings) {
9471
setting->Load(&result, true);
9572
}
@@ -101,22 +78,14 @@ namespace REX::JSON
10178
{
10279
glz::json_t output;
10380
if (std::filesystem::exists(m_fileBase)) {
104-
std::string buffer;
105-
const auto errorc = glz::read_file_json(output, m_fileBase, buffer);
106-
if (errorc) {
107-
REX::ERROR("{}"sv, glz::format_error(errorc, buffer));
108-
}
81+
(void)glz::read_file_json(output, m_fileBase, std::string{});
10982
}
11083

11184
for (auto& setting : m_settings) {
11285
setting->Save(&output);
11386
}
11487

115-
std::string buffer;
116-
const auto errorc = glz::write_file_json(output, m_fileBase, buffer);
117-
if (errorc) {
118-
REX::ERROR("{}"sv, glz::format_error(errorc, buffer));
119-
}
88+
(void)glz::write_file_json(output, m_fileBase, std::string{});
12089
}
12190
}
12291
#endif

0 commit comments

Comments
 (0)