Skip to content

Commit dca5cc9

Browse files
committed
Provide API to parse files or strings into SAX handlers
1 parent 1f4fbda commit dca5cc9

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

include/tao/json/from_string.hh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ namespace tao
1616
{
1717
namespace json
1818
{
19+
template< typename Handler >
20+
inline void from_string( const char * data, const std::size_t size, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
21+
{
22+
tao_json_pegtl::input input( line, column, data, data + size, source ? source : __PRETTY_FUNCTION__ );
23+
tao_json_pegtl::parse_input< internal::grammar, internal::action, internal::control >( input, handler );
24+
}
25+
26+
template< typename Handler >
27+
inline void from_string( const std::string & data, Handler & handler, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
28+
{
29+
json::from_string( data.data(), data.size(), handler, source, line, column );
30+
}
31+
1932
template< template< typename ... > class Traits >
2033
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
2134
{
2235
internal::value_builder< Traits > handler;
23-
tao_json_pegtl::input input( line, column, data, data + size, source ? source : __PRETTY_FUNCTION__ );
24-
tao_json_pegtl::parse_input< internal::grammar, internal::action, internal::control >( input, handler );
36+
json::from_string( data, size, handler, source, line, column );
2537
return std::move( handler.value_ );
2638
}
2739

include/tao/json/parse_file.hh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ namespace tao
1616
{
1717
namespace json
1818
{
19+
template< typename Handler >
20+
void parse_file( const std::string & filename, Handler & handler )
21+
{
22+
tao_json_pegtl::file_parser( filename ).parse< internal::grammar, internal::action, internal::control >( handler );
23+
}
24+
1925
template< template< typename ... > class Traits >
2026
basic_value< Traits > parse_file( const std::string & filename )
2127
{
2228
internal::value_builder< Traits > handler;
23-
tao_json_pegtl::file_parser( filename ).parse< internal::grammar, internal::action, internal::control >( handler );
29+
json::parse_file( filename, handler );
2430
return std::move( handler.value_ );
2531
}
2632

0 commit comments

Comments
 (0)