Skip to content

Commit 3046a56

Browse files
authored
Fix extra comma when unknown writer is empty (#1435)
1 parent 161d98d commit 3046a56

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

include/glaze/json/write.hpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -1363,14 +1363,26 @@ namespace glz
13631363

13641364
using WriterType = meta_unknown_write_t<ValueType>;
13651365
if constexpr (std::is_member_object_pointer_v<WriterType>) {
1366-
// TODO: This intermediate is added to get GCC 14 to build
1367-
decltype(auto) merged = glz::merge{value, value.*writer};
1368-
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
1366+
decltype(auto) unknown_writer = value.*writer;
1367+
if (unknown_writer.size() > 0) {
1368+
// TODO: This intermediate is added to get GCC 14 to build
1369+
decltype(auto) merged = glz::merge{value, unknown_writer};
1370+
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
1371+
}
1372+
else {
1373+
write<JSON>::op<disable_write_unknown_on<Options>()>(value, ctx, b, ix);
1374+
}
13691375
}
13701376
else if constexpr (std::is_member_function_pointer_v<WriterType>) {
1371-
// TODO: This intermediate is added to get GCC 14 to build
1372-
decltype(auto) merged = glz::merge{value, (value.*writer)()};
1373-
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
1377+
decltype(auto) unknown_writer = (value.*writer)();
1378+
if (unknown_writer.size() > 0) {
1379+
// TODO: This intermediate is added to get GCC 14 to build
1380+
decltype(auto) merged = glz::merge{value, unknown_writer};
1381+
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
1382+
}
1383+
else {
1384+
write<JSON>::op<disable_write_unknown_on<Options>()>(value, ctx, b, ix);
1385+
}
13741386
}
13751387
else {
13761388
static_assert(false_v<T>, "unknown_write type not handled");

tests/json_test/json_test.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -7193,6 +7193,26 @@ struct glz::meta<unknown_fields_2>
71937193
static constexpr auto unknown_read{&T::extra};
71947194
};
71957195

7196+
struct my_unknown_struct
7197+
{
7198+
int i = 287;
7199+
std::unordered_map<std::string, std::string> unknown;
7200+
7201+
};
7202+
7203+
template <>
7204+
struct glz::meta<my_unknown_struct> {
7205+
using T = my_unknown_struct;
7206+
static constexpr auto value = glz::object(
7207+
"i", &T::i
7208+
);
7209+
7210+
static constexpr auto unknown_write{ &T::unknown };
7211+
//static constexpr auto unknown_read{
7212+
// &T::unknown
7213+
//};
7214+
};
7215+
71967216
suite unknown_fields_member_test = [] {
71977217
"decode_unknown"_test = [] {
71987218
unknown_fields_member obj{};
@@ -7229,6 +7249,18 @@ suite unknown_fields_member_test = [] {
72297249
expect(not glz::write_json(obj, out));
72307250
expect(out == R"({"unk":"zzz","unk2":{"sub":3,"sub2":[{"a":"b"}]},"unk3":[]})") << out;
72317251
};
7252+
7253+
"my_unknown_struct"_test = [] {
7254+
my_unknown_struct obj;
7255+
std::string buffer;
7256+
expect(not glz::write < glz::opts{ .prettify = true } > (obj, buffer));
7257+
expect(buffer == R"({
7258+
"i": 287
7259+
})") << buffer;
7260+
7261+
expect(not glz::write < glz::opts{ } > (obj, buffer));
7262+
expect(buffer == R"({"i":287})") << buffer;
7263+
};
72327264
};
72337265

72347266
struct unknown_fields_method

0 commit comments

Comments
 (0)