Skip to content

Commit

Permalink
Fix extra comma when unknown writer is empty (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry authored Nov 8, 2024
1 parent 161d98d commit 3046a56
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
24 changes: 18 additions & 6 deletions include/glaze/json/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1363,14 +1363,26 @@ namespace glz

using WriterType = meta_unknown_write_t<ValueType>;
if constexpr (std::is_member_object_pointer_v<WriterType>) {
// TODO: This intermediate is added to get GCC 14 to build
decltype(auto) merged = glz::merge{value, value.*writer};
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
decltype(auto) unknown_writer = value.*writer;
if (unknown_writer.size() > 0) {
// TODO: This intermediate is added to get GCC 14 to build
decltype(auto) merged = glz::merge{value, unknown_writer};
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
}
else {
write<JSON>::op<disable_write_unknown_on<Options>()>(value, ctx, b, ix);
}
}
else if constexpr (std::is_member_function_pointer_v<WriterType>) {
// TODO: This intermediate is added to get GCC 14 to build
decltype(auto) merged = glz::merge{value, (value.*writer)()};
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
decltype(auto) unknown_writer = (value.*writer)();
if (unknown_writer.size() > 0) {
// TODO: This intermediate is added to get GCC 14 to build
decltype(auto) merged = glz::merge{value, unknown_writer};
write<JSON>::op<disable_write_unknown_on<Options>()>(std::move(merged), ctx, b, ix);
}
else {
write<JSON>::op<disable_write_unknown_on<Options>()>(value, ctx, b, ix);
}
}
else {
static_assert(false_v<T>, "unknown_write type not handled");
Expand Down
32 changes: 32 additions & 0 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7193,6 +7193,26 @@ struct glz::meta<unknown_fields_2>
static constexpr auto unknown_read{&T::extra};
};

struct my_unknown_struct
{
int i = 287;
std::unordered_map<std::string, std::string> unknown;

};

template <>
struct glz::meta<my_unknown_struct> {
using T = my_unknown_struct;
static constexpr auto value = glz::object(
"i", &T::i
);

static constexpr auto unknown_write{ &T::unknown };
//static constexpr auto unknown_read{
// &T::unknown
//};
};

suite unknown_fields_member_test = [] {
"decode_unknown"_test = [] {
unknown_fields_member obj{};
Expand Down Expand Up @@ -7229,6 +7249,18 @@ suite unknown_fields_member_test = [] {
expect(not glz::write_json(obj, out));
expect(out == R"({"unk":"zzz","unk2":{"sub":3,"sub2":[{"a":"b"}]},"unk3":[]})") << out;
};

"my_unknown_struct"_test = [] {
my_unknown_struct obj;
std::string buffer;
expect(not glz::write < glz::opts{ .prettify = true } > (obj, buffer));
expect(buffer == R"({
"i": 287
})") << buffer;

expect(not glz::write < glz::opts{ } > (obj, buffer));
expect(buffer == R"({"i":287})") << buffer;
};
};

struct unknown_fields_method
Expand Down

0 comments on commit 3046a56

Please sign in to comment.