Skip to content

Commit ed3a618

Browse files
committed
direct parsing supports paths
1 parent 14dfadb commit ed3a618

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

include/boost/json/detail/parse_into.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,47 @@ class converting_handler<optional_conversion_tag, V, P>
16611661
#undef BOOST_JSON_INVOKE_INNER
16621662
};
16631663

1664+
// path handler
1665+
template< class V, class P >
1666+
class converting_handler<path_conversion_tag, V, P>
1667+
: public scalar_handler<P, error::not_string>
1668+
{
1669+
private:
1670+
V* value_;
1671+
bool cleared_ = false;
1672+
1673+
public:
1674+
converting_handler( V* v, P* p )
1675+
: converting_handler::scalar_handler(p)
1676+
, value_(v)
1677+
{}
1678+
1679+
bool on_string_part( error_code&, string_view sv )
1680+
{
1681+
if( !cleared_ )
1682+
{
1683+
cleared_ = true;
1684+
value_->clear();
1685+
}
1686+
1687+
value_->concat( sv.begin(), sv.end() );
1688+
return true;
1689+
}
1690+
1691+
bool on_string( error_code&, string_view sv )
1692+
{
1693+
if( !cleared_ )
1694+
value_->clear();
1695+
else
1696+
cleared_ = false;
1697+
1698+
value_->concat( sv.begin(), sv.end() );
1699+
1700+
this->parent_->signal_value();
1701+
return true;
1702+
}
1703+
};
1704+
16641705
// into_handler
16651706
template< class V >
16661707
class into_handler

test/parse_into.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
#include <climits>
1919
#include <map>
2020

21+
#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
22+
#include <filesystem>
23+
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM
24+
2125
#include "test.hpp"
2226
#include "test_suite.hpp"
2327

@@ -414,6 +418,15 @@ class parse_into_test
414418
#endif
415419
}
416420

421+
void testPath()
422+
{
423+
#ifndef BOOST_NO_CXX17_HDR_FILESYSTEM
424+
testParseInto< std::filesystem::path >( "c:/" );
425+
testParseInto< std::filesystem::path >( ".." );
426+
testParseInto< std::filesystem::path >( "../" );
427+
#endif // BOOST_NO_CXX17_HDR_FILESYSTEM
428+
}
429+
417430
void run()
418431
{
419432
testNull();
@@ -427,10 +440,12 @@ class parse_into_test
427440
testStruct();
428441
testEnum();
429442
testOptional();
443+
testPath();
430444
testVariant<variant2::variant, variant2::monostate>();
431445
#ifndef BOOST_NO_CXX17_HDR_VARIANT
432446
testVariant<std::variant, std::monostate>();
433447
#endif
448+
434449
{
435450
int n;
436451
BOOST_TEST_THROWS_WITH_LOCATION( parse_into( n, "null" ) );

0 commit comments

Comments
 (0)