Skip to content

Commit 7c3812c

Browse files
authored
Fix issue with variants of 1 type and tags (#1485)
* Fix issue with variants of 1 type and tags * bool cast
1 parent 1c065d7 commit 7c3812c

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

include/glaze/json/read.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,7 @@ namespace glz
22012201
ctx.error = error_code::no_matching_variant_type;
22022202
return;
22032203
}
2204-
else if constexpr ((type_counts::n_object + type_counts::n_nullable_object) == 1) {
2204+
else if constexpr ((type_counts::n_object + type_counts::n_nullable_object) == 1 && tag_v<T>.empty()) {
22052205
using V = glz::tuple_element_t<0, object_types>;
22062206
if (!std::holds_alternative<V>(value)) value = V{};
22072207
read<JSON>::op<opening_handled<Opts>()>(std::get<V>(value), ctx, it, end);

tests/json_test/json_test.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -9941,6 +9941,58 @@ suite custom_nullable_with_specialization = [] {
99419941
};
99429942
};
99439943

9944+
struct A1
9945+
{
9946+
int p{};
9947+
};
9948+
9949+
struct B1
9950+
{
9951+
float p{};
9952+
};
9953+
9954+
using X1 = std::variant<A1>;
9955+
using Y1 = std::variant<A1, B1>;
9956+
9957+
template <>
9958+
struct glz::meta<A1>
9959+
{
9960+
static constexpr auto value = object(
9961+
"p", &A1::p);
9962+
};
9963+
9964+
template <>
9965+
struct glz::meta<B1>
9966+
{
9967+
static constexpr auto value = object(
9968+
"p", &B1::p);
9969+
};
9970+
9971+
template <>
9972+
struct glz::meta<X1>
9973+
{
9974+
static constexpr std::string_view tag = "tag";
9975+
};
9976+
9977+
template <>
9978+
struct glz::meta<Y1>
9979+
{
9980+
static constexpr std::string_view tag = "tag";
9981+
};
9982+
9983+
suite variant_tag_tests = [] {
9984+
"variant tag"_test = [] {
9985+
auto xString = glz::write_json(X1(A1()));
9986+
expect(xString.has_value());
9987+
9988+
auto x = glz::read_json<X1>(*xString);
9989+
expect(bool(x));
9990+
if (not x.has_value()) {
9991+
std::cerr << glz::format_error(x.error(), *xString);
9992+
}
9993+
};
9994+
};
9995+
99449996
int main()
99459997
{
99469998
trace.end("json_test");

0 commit comments

Comments
 (0)