diff --git a/example/parse_into.cpp b/example/parse_into.cpp index 835000c84..4edf51607 100644 --- a/example/parse_into.cpp +++ b/example/parse_into.cpp @@ -65,6 +65,10 @@ struct options iterator end(); + + void + clear() + { } }; struct coordinate @@ -112,6 +116,10 @@ struct accumulator iterator end() { return nullptr; } + + void + clear() + { } }; struct coordinates2 diff --git a/include/boost/json/detail/parse_into.hpp b/include/boost/json/detail/parse_into.hpp index 2da98c01f..94d45ead4 100644 --- a/include/boost/json/detail/parse_into.hpp +++ b/include/boost/json/detail/parse_into.hpp @@ -502,6 +502,32 @@ std::true_type check_inserter( It1, It2 ) return {}; } +template +void +clear_container( + T&, + mp11::mp_int<2>) +{ +} + +template +void +clear_container( + T& target, + mp11::mp_int<1>) +{ + target.clear(); +} + +template +void +clear_container( + T& target, + mp11::mp_int<0>) +{ + target.clear(); +} + template< class V, class P > class converting_handler : public composite_handler< @@ -553,14 +579,11 @@ class converting_handler bool on_array_begin( error_code& ec ) { if( this->inner_active_ ) - { return this->inner_.on_array_begin( ec ); - } - else - { - this->inner_active_ = true; - return true; - } + + this->inner_active_ = true; + clear_container( *value_, inserter_implementation() ); + return true; } bool on_array_end( error_code& ec ) @@ -605,6 +628,7 @@ class converting_handler if( this->inner_active_ ) return this->inner_.on_object_begin(ec); + clear_container( *value_, inserter_implementation() ); return true; } diff --git a/test/parse_into.cpp b/test/parse_into.cpp index c8ecc23da..ba837ca50 100644 --- a/test/parse_into.cpp +++ b/test/parse_into.cpp @@ -252,6 +252,13 @@ class parse_into_test error::size_mismatch, {1, 2, 3} ); testParseInto< std::vector> >( {arr,arr,arr} ); + + std::vector v; + parse_into(v, "[1,2,3,4]"); + BOOST_TEST( v.size() == 4 ); + + parse_into(v, "[5,6,7]"); + BOOST_TEST( v.size() == 3 ); } void testMap() @@ -272,6 +279,13 @@ class parse_into_test error::not_object, { "1", 1, "2", 2} ); testParseIntoErrors< std::map> >( error::not_object, { {"1", {}}, {"2", {"3", 4}} } ); + + std::map m; + parse_into(m, R"( {"1": 1, "2": 2, "3": 3} )"); + BOOST_TEST( m.size() == 3 ); + + parse_into(m, R"( {"4": 4, "5": 5} )"); + BOOST_TEST( m.size() == 2 ); } void testTuple()