Skip to content

Commit 41380ba

Browse files
committed
Refactored SAX classes
1 parent dca5cc9 commit 41380ba

File tree

12 files changed

+305
-193
lines changed

12 files changed

+305
-193
lines changed

include/tao/json.hh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@
55
#define TAOCPP_JSON_INCLUDE_JSON_HH
66

77
#include "json/value.hh"
8-
98
#include "json/comparison_operators.hh"
109
#include "json/object_operators.hh"
1110

11+
#include "json/sax/to_stream.hh"
12+
#include "json/sax/to_pretty_stream.hh"
13+
#include "json/sax/to_value.hh"
1214
#include "json/stream.hh"
1315
#include "json/to_string.hh"
1416

17+
#include "json/sax/traverse_value.hh"
18+
#include "json/sax/from_string.hh"
19+
#include "json/sax/parse_file.hh"
1520
#include "json/from_string.hh"
1621
#include "json/parse_file.hh"
1722

1823
#include "json/self_contained.hh"
19-
2024
#include "json/patch.hh"
2125

2226
#endif

include/tao/json/from_string.hh

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,67 @@
44
#ifndef TAOCPP_JSON_INCLUDE_FROM_STRING_HH
55
#define TAOCPP_JSON_INCLUDE_FROM_STRING_HH
66

7-
#include "external/pegtl/parse.hh"
7+
#include <cstddef>
8+
#include <utility>
9+
#include <string>
810

9-
#include "internal/value_builder.hh"
10-
11-
#include "internal/grammar.hh"
12-
#include "internal/action.hh"
13-
#include "internal/control.hh"
11+
#include "sax/from_string.hh"
12+
#include "sax/to_value.hh"
1413

1514
namespace tao
1615
{
1716
namespace json
1817
{
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-
3218
template< template< typename ... > class Traits >
3319
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 )
3420
{
35-
internal::value_builder< Traits > handler;
36-
json::from_string( data, size, handler, source, line, column );
37-
return std::move( handler.value_ );
21+
sax::to_basic_value< Traits > handler;
22+
sax::from_string( data, size, handler, source, line, column );
23+
return std::move( handler.result() );
3824
}
3925

4026
template< template< typename ... > class Traits >
41-
inline void from_string( basic_value< Traits > & output, const char * data, const std::size_t size, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
27+
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
4228
{
43-
output = from_string< Traits >( data, size, source, line, column );
29+
return from_string< Traits >( data, size, source.c_str(), line, column );
4430
}
4531

4632
inline value 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 )
4733
{
4834
return from_string< traits >( data, size, source, line, column );
4935
}
5036

51-
template< template< typename ... > class Traits >
52-
inline void from_string( basic_value< Traits > & output, const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
53-
{
54-
json::from_string( output, data, size, source.c_str(), line, column );
55-
}
56-
57-
template< template< typename ... > class Traits >
58-
inline basic_value< Traits > from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
59-
{
60-
return from_string< Traits >( data, size, source.c_str(), line, column );
61-
}
62-
6337
inline value from_string( const char * data, const std::size_t size, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
6438
{
65-
return json::from_string( data, size, source.c_str(), line, column );
39+
return from_string< traits >( data, size, source.c_str(), line, column );
6640
}
6741

6842
template< template< typename ... > class Traits >
69-
inline void from_string( basic_value< Traits > & output, const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
43+
inline basic_value< Traits > from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
7044
{
71-
json::from_string( output, data.data(), data.size(), source, line, column );
45+
return from_string< Traits >( data.data(), data.size(), source, line, column );
7246
}
7347

7448
template< template< typename ... > class Traits >
75-
inline basic_value< Traits > from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
49+
inline basic_value< Traits > from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
7650
{
77-
return from_string< Traits >( data.data(), data.size(), source, line, column );
51+
return from_string< Traits >( data.data(), data.size(), source.c_str(), line, column );
7852
}
7953

8054
inline value from_string( const std::string & data, const char * source = nullptr, const std::size_t line = 1, const std::size_t column = 0 )
8155
{
8256
return json::from_string( data.data(), data.size(), source, line, column );
8357
}
8458

85-
template< template< typename ... > class Traits >
86-
inline void from_string( basic_value< Traits > & output, const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
87-
{
88-
json::from_string( output, data.data(), data.size(), source.c_str(), line, column );
89-
}
90-
91-
template< template< typename ... > class Traits >
92-
inline basic_value< Traits > from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
59+
inline value from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
9360
{
94-
return from_string< Traits >( data.data(), data.size(), source.c_str(), line, column );
61+
return json::from_string( data.data(), data.size(), source.c_str(), line, column );
9562
}
9663

97-
inline value from_string( const std::string & data, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
64+
template< template< typename ... > class Traits, typename ... Ts >
65+
inline void from_string( basic_value< Traits > & output, Ts && ... ts )
9866
{
99-
return json::from_string( data.data(), data.size(), source.c_str(), line, column );
67+
output = from_string< Traits >( std::forward< Ts >( ts ) ... );
10068
}
10169

10270
inline namespace literals

include/tao/json/internal/to_stream.hh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#define TAOCPP_JSON_INCLUDE_INTERNAL_TO_STREAM_HH
66

77
#include <ostream>
8+
#include <cstddef>
89

910
#include "../value.hh"
10-
#include "../traverse.hh"
11-
12-
#include "value_writer.hh"
13-
#include "pretty_writer.hh"
11+
#include "../sax/to_stream.hh"
12+
#include "../sax/to_pretty_stream.hh"
13+
#include "../sax/traverse_value.hh"
1414

1515
namespace tao
1616
{
@@ -19,17 +19,17 @@ namespace tao
1919
namespace internal
2020
{
2121
template< template< typename ... > class Traits >
22-
void to_stream( std::ostream & o, const basic_value< Traits > & v )
22+
void to_stream( std::ostream & os, const basic_value< Traits > & v )
2323
{
24-
internal::value_writer writer( o );
25-
json::traverse( v, writer );
24+
sax::to_stream handler( os );
25+
sax::traverse_value( v, handler );
2626
}
2727

2828
template< template< typename ... > class Traits >
29-
void to_stream( std::ostream & o, const basic_value< Traits > & v, const unsigned indent )
29+
void to_stream( std::ostream & os, const basic_value< Traits > & v, const std::size_t indent )
3030
{
31-
internal::pretty_writer writer( o, indent );
32-
json::traverse( v, writer );
31+
sax::to_pretty_stream handler( os, indent );
32+
sax::traverse_value( v, handler );
3333
}
3434

3535
} // internal

include/tao/json/parse_file.hh

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,22 @@
44
#ifndef TAOCPP_JSON_INCLUDE_PARSE_FILE_HH
55
#define TAOCPP_JSON_INCLUDE_PARSE_FILE_HH
66

7-
#include "external/pegtl/file_parser.hh"
7+
#include <string>
8+
#include <utility>
89

9-
#include "internal/value_builder.hh"
10-
11-
#include "internal/grammar.hh"
12-
#include "internal/action.hh"
13-
#include "internal/control.hh"
10+
#include "sax/parse_file.hh"
11+
#include "sax/to_value.hh"
1412

1513
namespace tao
1614
{
1715
namespace json
1816
{
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-
2517
template< template< typename ... > class Traits >
2618
basic_value< Traits > parse_file( const std::string & filename )
2719
{
28-
internal::value_builder< Traits > handler;
29-
json::parse_file( filename, handler );
30-
return std::move( handler.value_ );
20+
sax::to_basic_value< Traits > handler;
21+
sax::parse_file( filename, handler );
22+
return std::move( handler.result() );
3123
}
3224

3325
template< template< typename ... > class Traits >
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#ifndef TAOCPP_JSON_INCLUDE_SAX_FROM_STRING_HH
5+
#define TAOCPP_JSON_INCLUDE_SAX_FROM_STRING_HH
6+
7+
#include "../external/pegtl/parse.hh"
8+
9+
#include "../internal/grammar.hh"
10+
#include "../internal/action.hh"
11+
#include "../internal/control.hh"
12+
13+
namespace tao
14+
{
15+
namespace json
16+
{
17+
namespace sax
18+
{
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 char * data, const std::size_t size, Handler & handler, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
28+
{
29+
sax::from_string( data, size, handler, source.c_str(), line, column );
30+
}
31+
32+
template< typename Handler >
33+
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 )
34+
{
35+
sax::from_string( data.data(), data.size(), handler, source, line, column );
36+
}
37+
38+
template< typename Handler >
39+
inline void from_string( const std::string & data, Handler & handler, const std::string & source, const std::size_t line = 1, const std::size_t column = 0 )
40+
{
41+
sax::from_string( data, handler, source.c_str(), line, column );
42+
}
43+
44+
} // sax
45+
46+
} // json
47+
48+
} // tao
49+
50+
#endif

include/tao/json/sax/parse_file.hh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey
2+
// Please see LICENSE for license or visit https://github.com/taocpp/json/
3+
4+
#ifndef TAOCPP_JSON_INCLUDE_SAX_PARSE_FILE_HH
5+
#define TAOCPP_JSON_INCLUDE_SAX_PARSE_FILE_HH
6+
7+
#include "../external/pegtl/file_parser.hh"
8+
9+
#include "../internal/grammar.hh"
10+
#include "../internal/action.hh"
11+
#include "../internal/control.hh"
12+
13+
namespace tao
14+
{
15+
namespace json
16+
{
17+
namespace sax
18+
{
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+
25+
} // sax
26+
27+
} // json
28+
29+
} // tao
30+
31+
#endif

0 commit comments

Comments
 (0)