Skip to content

Commit 15252a9

Browse files
authored
Merge pull request #3 from libxse/glaze
refactor: replace `nlohmann_json` with `glaze`
2 parents e7a8083 + dccd574 commit 15252a9

3 files changed

Lines changed: 74 additions & 28 deletions

File tree

include/REX/REX/JSON.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,39 @@ namespace REX::JSON
8383

8484
template <class Store = SettingStore>
8585
using Str = Setting<std::string, Store>;
86+
87+
template <class T, class Store = SettingStore>
88+
using SettingA = Setting<std::vector<T>, Store>;
89+
90+
template <class Store = SettingStore>
91+
using BoolA = SettingA<bool, Store>;
92+
93+
template <class Store = SettingStore>
94+
using F32A = SettingA<float, Store>;
95+
96+
template <class Store = SettingStore>
97+
using F64A = SettingA<double, Store>;
98+
99+
template <class Store = SettingStore>
100+
using I8A = SettingA<std::int8_t, Store>;
101+
102+
template <class Store = SettingStore>
103+
using I16A = SettingA<std::int16_t, Store>;
104+
105+
template <class Store = SettingStore>
106+
using I32A = SettingA<std::int32_t, Store>;
107+
108+
template <class Store = SettingStore>
109+
using U8A = SettingA<std::uint8_t, Store>;
110+
111+
template <class Store = SettingStore>
112+
using U16A = SettingA<std::uint16_t, Store>;
113+
114+
template <class Store = SettingStore>
115+
using U32A = SettingA<std::uint32_t, Store>;
116+
117+
template <class Store = SettingStore>
118+
using StrA = SettingA<std::string, Store>;
86119
}
87120

88121
template <class T, class S, class CharT>

src/REX/REX/JSON.cpp

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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

86
namespace 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

xmake.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ add_requireconfs("*.cmake", { configs = { override = true, system = false } })
3939

4040
-- add config packages
4141
if has_config("commonlib_ini") then add_requires("simpleini") end
42-
if has_config("commonlib_json") then add_requires("nlohmann_json") end
42+
if has_config("commonlib_json") then add_requires("glaze") end
4343
if has_config("commonlib_toml") then add_requires("toml11") end
4444
if has_config("commonlib_xbyak") then add_requires("xbyak") end
4545

@@ -57,7 +57,7 @@ target("commonlib-shared", function()
5757
end
5858

5959
if has_config("commonlib_json") then
60-
add_packages("nlohmann_json", { public = true })
60+
add_packages("glaze", { public = true })
6161
add_defines("COMMONLIB_OPTION_JSON=1", { public = true })
6262
end
6363

0 commit comments

Comments
 (0)