Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions include/boost/json/detail/parse_into.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <boost/json/value.hpp>
#include <boost/describe/enum_from_string.hpp>

#include <bitset>
#include <vector>

/*
Expand Down Expand Up @@ -1152,7 +1153,7 @@ class converting_handler<described_class_conversion_tag, V, P>

handler_tuple<converting_handler, InnerHandlers> handlers_;
int inner_active_ = -1;
std::size_t activated_ = 0;
std::bitset<mp11::mp_size<Dt>::value> seen_;

public:
converting_handler( converting_handler const& ) = delete;
Expand Down Expand Up @@ -1184,7 +1185,7 @@ class converting_handler<described_class_conversion_tag, V, P>
inner_active_,
is_required_checker{});
if( required_member )
++activated_;
seen_[inner_active_] = true;

key_ = {};
inner_active_ = -1;
Expand Down Expand Up @@ -1214,7 +1215,17 @@ class converting_handler<described_class_conversion_tag, V, P>
bool on_object_begin( system::error_code& ec )
{
if( inner_active_ < 0 )
{
// optional members count as seen from the start, so that at the
// end of the object seen_.all() means no required member is
// missing
mp11::mp_for_each< mp11::mp_iota< mp11::mp_size<Dt> > >(
[&](auto I) {
using T = mp11::mp_at< Dt, decltype(I) >;
seen_[I] = is_optional_like<T>::value;
});
return true;
}

BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
}
Expand All @@ -1223,9 +1234,7 @@ class converting_handler<described_class_conversion_tag, V, P>
{
if( inner_active_ < 0 )
{
using C = mp11::mp_count_if<Dt, is_optional_like>;
constexpr int N = mp11::mp_size<Dt>::value - C::value;
if( activated_ < N )
if( !seen_.all() )
{
BOOST_JSON_FAIL( ec, error::size_mismatch );
return false;
Expand Down
18 changes: 18 additions & 0 deletions test/parse_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,24 @@ class parse_into_test
{"b", 3.14f},
{"c", "hello"}}}};
testParseIntoValue<W>(unexpected_jo);

system::error_code ec;
X x{};
parse_into(x, R"( {"a": 1, "a": 2, "a": 3} )", ec);
BOOST_TEST( ec == error::size_mismatch );

std::vector<X> v;
ec = {};
parse_into(
v, R"( [{"a": 1, "b": 1, "c": "one"}, {"a": 2}] )", ec);
BOOST_TEST( ec == error::size_mismatch );

ec = {};
parse_into(
v,
R"( [{"a": 1, "b": 1, "c": "one"}, {"a": 2, "b": 2, "c": "two"}] )",
ec);
BOOST_TEST( !ec.failed() ) && BOOST_TEST( v.size() == 2 );
#endif
}

Expand Down
Loading