Skip to content

Commit 7e4a3ac

Browse files
committed
Merge branch 'main' into repe
2 parents 6799054 + c00883e commit 7e4a3ac

File tree

5 files changed

+88
-24
lines changed

5 files changed

+88
-24
lines changed

include/glaze/beve/read.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ namespace glz
968968
}
969969

970970
// for types like std::vector<std::pair...> that can't look up with operator[]
971-
// Intead of hashing or linear searching, we just clear the input and overwrite the entire contents
971+
// Instead of hashing or linear searching, we just clear the input and overwrite the entire contents
972972
template <auto Opts>
973973
requires(pair_t<range_value_t<T>> && Opts.concatenate == true)
974974
static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end)

include/glaze/json/read.hpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,8 @@ namespace glz
13831383

13841384
using V = std::decay_t<decltype(item)>;
13851385

1386-
if constexpr (str_t<typename V::first_type>) {
1386+
if constexpr (str_t<typename V::first_type> ||
1387+
(std::is_enum_v<typename V::first_type> && detail::glaze_t<typename V::first_type>)) {
13871388
read<JSON>::op<Opts>(item.first, ctx, it, end);
13881389
if (bool(ctx.error)) [[unlikely]]
13891390
return;
@@ -1783,7 +1784,7 @@ namespace glz
17831784
GLZ_SKIP_WS();
17841785
const size_t ws_size = size_t(it - ws_start);
17851786

1786-
if constexpr ((glaze_object_t<T> || reflectable<T>)&&num_members == 0 && Opts.error_on_unknown_keys) {
1787+
if constexpr ((glaze_object_t<T> || reflectable<T>) && num_members == 0 && Opts.error_on_unknown_keys) {
17871788
if (*it == '}') [[likely]] {
17881789
GLZ_SUB_LEVEL;
17891790
++it;
@@ -1798,8 +1799,8 @@ namespace glz
17981799
}
17991800
else {
18001801
decltype(auto) fields = [&]() -> decltype(auto) {
1801-
if constexpr ((glaze_object_t<T> || reflectable<T>)&&(Opts.error_on_missing_keys ||
1802-
Opts.partial_read)) {
1802+
if constexpr ((glaze_object_t<T> || reflectable<T>) &&
1803+
(Opts.error_on_missing_keys || Opts.partial_read)) {
18031804
return bit_array<num_members>{};
18041805
}
18051806
else {
@@ -1811,7 +1812,7 @@ namespace glz
18111812

18121813
bool first = true;
18131814
while (true) {
1814-
if constexpr ((glaze_object_t<T> || reflectable<T>)&&Opts.partial_read) {
1815+
if constexpr ((glaze_object_t<T> || reflectable<T>) && Opts.partial_read) {
18151816
static constexpr bit_array<num_members> all_fields = [] {
18161817
bit_array<num_members> arr{};
18171818
for (size_t i = 0; i < num_members; ++i) {
@@ -1828,14 +1829,14 @@ namespace glz
18281829

18291830
if (*it == '}') {
18301831
GLZ_SUB_LEVEL;
1831-
if constexpr ((glaze_object_t<T> ||
1832-
reflectable<T>)&&(Opts.partial_read && Opts.error_on_missing_keys)) {
1832+
if constexpr ((glaze_object_t<T> || reflectable<T>) &&
1833+
(Opts.partial_read && Opts.error_on_missing_keys)) {
18331834
ctx.error = error_code::missing_key;
18341835
return;
18351836
}
18361837
else {
18371838
++it;
1838-
if constexpr ((glaze_object_t<T> || reflectable<T>)&&Opts.error_on_missing_keys) {
1839+
if constexpr ((glaze_object_t<T> || reflectable<T>) && Opts.error_on_missing_keys) {
18391840
constexpr auto req_fields = required_fields<T, Opts>();
18401841
if ((req_fields & fields) != req_fields) {
18411842
ctx.error = error_code::missing_key;

include/glaze/json/write.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ namespace glz
671671
}
672672
}
673673
else [[unlikely]] {
674-
// What do we want to happen if the value doesnt have a mapped string
674+
// What do we want to happen if the value doesn't have a mapped string
675675
write<JSON>::op<Opts>(static_cast<std::underlying_type_t<T>>(value), ctx, std::forward<Args>(args)...);
676676
}
677677
}

tests/example_json/example_json.cpp

+26-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,18 @@ struct EnumHolder
118118
static_assert(glz::detail::reflectable<EnumHolder>);
119119

120120
suite enum_test = [] {
121-
"enum_as_string"_test = [] {
121+
"enum_as_string_key"_test = [] {
122+
std::map<Color, bool> obj{{Color::Red, true}};
123+
std::string json;
124+
expect(not glz::write_json(obj, json));
125+
expect(json == R"({"Red":true})");
126+
127+
std::string input = R"({"Green":true})";
128+
expect(!glz::read_json(obj, input));
129+
expect(obj[Color::Green]);
130+
};
131+
132+
"enum_as_string_value"_test = [] {
122133
EnumHolder obj{};
123134
std::string json;
124135
expect(not glz::write_json(obj, json));
@@ -128,6 +139,20 @@ suite enum_test = [] {
128139
expect(!glz::read_json(obj, input));
129140
expect(obj.c == Color::Blue);
130141
};
142+
143+
"enum_as_key_vector_pair_concatenate"_test = [] {
144+
std::vector<std::pair<Color, int>> obj{{Color::Red, 1}, {Color::Green, 2}};
145+
std::string json;
146+
expect(not glz::write_json(obj, json));
147+
expect(json == R"({"Red":1,"Green":2})");
148+
149+
std::vector<std::pair<Color, int>> obj2{};
150+
std::string input = R"({"Blue":3})";
151+
expect(!glz::read_json(obj2, input));
152+
expect(obj2.size() == 1);
153+
expect(obj2[0].first == Color::Blue);
154+
expect(obj2[0].second == 3);
155+
};
131156
};
132157

133158
//------------------------------------

tests/json_test/json_test.cpp

+51-13
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ struct Thing
346346
std::map<int, double> mapi{{5, 3.14}, {7, 7.42}, {2, 9.63}};
347347
sub_thing* thing_ptr{};
348348

349-
Thing() : thing_ptr(&thing){};
349+
Thing() : thing_ptr(&thing) {};
350350
};
351351

352352
template <>
@@ -4197,7 +4197,7 @@ struct glz::meta<cat>
41974197

41984198
struct person
41994199
{
4200-
void eat(const std::string&){};
4200+
void eat(const std::string&) {};
42014201
};
42024202

42034203
template <>
@@ -6155,11 +6155,8 @@ suite char_buffer = [] {
61556155
static_assert(!glz::detail::char_array_t<char*>);
61566156

61576157
suite enum_map = [] {
6158-
"enum map"_test = [] {
6159-
std::map<Color, std::string> color_map;
6160-
color_map[Color::Red] = "red";
6161-
color_map[Color::Green] = "green";
6162-
color_map[Color::Blue] = "blue";
6158+
"enum map key"_test = [] {
6159+
std::map<Color, std::string> color_map{{Color::Red, "red"}, {Color::Green, "green"}, {Color::Blue, "blue"}};
61636160

61646161
std::string s{};
61656162
expect(not glz::write_json(color_map, s));
@@ -6172,6 +6169,48 @@ suite enum_map = [] {
61726169
expect(color_map.at(Color::Green) == "green");
61736170
expect(color_map.at(Color::Blue) == "blue");
61746171
};
6172+
6173+
"enum map key vector pair concatenate"_test = [] {
6174+
std::vector<std::pair<Color, std::string>> colors{
6175+
{Color::Red, "red"}, {Color::Green, "green"}, {Color::Blue, "blue"}};
6176+
6177+
std::string s{};
6178+
expect(not glz::write_json(colors, s));
6179+
6180+
expect(s == R"({"Red":"red","Green":"green","Blue":"blue"})");
6181+
6182+
auto expected = colors;
6183+
colors.clear();
6184+
expect(!glz::read_json(colors, s));
6185+
expect(colors == expected);
6186+
};
6187+
6188+
"enum map value"_test = [] {
6189+
std::map<int, Color> color_map{{0, Color::Red}, {1, Color::Green}, {2, Color::Blue}};
6190+
6191+
std::string s{};
6192+
expect(not glz::write_json(color_map, s));
6193+
6194+
expect(s == R"({"0":"Red","1":"Green","2":"Blue"})");
6195+
6196+
auto expectedMap = color_map;
6197+
color_map.clear();
6198+
expect(!glz::read_json(color_map, s));
6199+
expect(expectedMap == color_map);
6200+
};
6201+
6202+
"enum map value vector pair concatenate"_test = [] {
6203+
std::vector<std::pair<int, Color>> colors{{0, Color::Red}, {1, Color::Green}, {2, Color::Blue}};
6204+
6205+
std::string s{};
6206+
expect(not glz::write_json(colors, s));
6207+
expect(s == R"({"0":"Red","1":"Green","2":"Blue"})");
6208+
6209+
auto expected = colors;
6210+
colors.clear();
6211+
expect(!glz::read_json(colors, s));
6212+
expect(colors == expected);
6213+
};
61756214
};
61766215

61776216
suite obj_handling = [] {
@@ -6419,9 +6458,8 @@ template <>
64196458
struct glz::meta<test_mapping_t>
64206459
{
64216460
using T = test_mapping_t;
6422-
static constexpr auto value = object("id", &T::id, "coordinates", [](auto& self) {
6423-
return coordinates_t{&self.latitude, &self.longitude};
6424-
});
6461+
static constexpr auto value =
6462+
object("id", &T::id, "coordinates", [](auto& self) { return coordinates_t{&self.latitude, &self.longitude}; });
64256463
};
64266464

64276465
suite mapping_struct = [] {
@@ -9699,8 +9737,8 @@ template <class V>
96999737
struct glz::meta<response_t<V>>
97009738
{
97019739
using T = response_t<V>;
9702-
static constexpr auto value = object(
9703-
"result", [](auto& s) -> auto& { return s.result; }, "id", &T::id, "error", &T::error);
9740+
static constexpr auto value =
9741+
object("result", [](auto& s) -> auto& { return s.result; }, "id", &T::id, "error", &T::error);
97049742
};
97059743

97069744
template <>
@@ -9919,7 +9957,7 @@ namespace trr
99199957

99209958
struct Person
99219959
{
9922-
Person(Address* const p_add) : p_add(p_add){};
9960+
Person(Address* const p_add) : p_add(p_add) {};
99239961
std::string name;
99249962
Address* const p_add; // pointer is const, Address object is mutable
99259963
};

0 commit comments

Comments
 (0)