Skip to content

Commit 6c6805b

Browse files
committed
Simplify SAX handler API
1 parent 77ecf11 commit 6c6805b

File tree

7 files changed

+159
-126
lines changed

7 files changed

+159
-126
lines changed

include/tao/json/from_string.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace tao
2222
internal::value_builder< Traits > handler;
2323
tao_json_pegtl::input input( line, column, data, data + size, source ? source : __PRETTY_FUNCTION__ );
2424
tao_json_pegtl::parse_input< internal::grammar, internal::action, internal::control >( input, handler );
25-
return std::move( handler.value );
25+
return std::move( handler.value_ );
2626
}
2727

2828
template< template< typename ... > class Traits >

include/tao/json/internal/action.hh

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace tao
3636
template< typename State >
3737
static void apply( const tao_json_pegtl::input &, State & handler )
3838
{
39-
handler.true_();
39+
handler.boolean( true );
4040
}
4141
};
4242

@@ -46,7 +46,7 @@ namespace tao
4646
template< typename State >
4747
static void apply( const tao_json_pegtl::input &, State & handler )
4848
{
49-
handler.false_();
49+
handler.boolean( false );
5050
}
5151
};
5252

@@ -66,17 +66,7 @@ namespace tao
6666
template< typename State >
6767
static void apply( const tao_json_pegtl::input &, State & handler )
6868
{
69-
handler.commit_element();
70-
}
71-
};
72-
73-
template<>
74-
struct action< rules::element_separator >
75-
{
76-
template< typename State >
77-
static void apply( const tao_json_pegtl::input &, State & handler )
78-
{
79-
handler.element_separator();
69+
handler.element();
8070
}
8171
};
8272

@@ -106,27 +96,7 @@ namespace tao
10696
template< typename State >
10797
static void apply( const tao_json_pegtl::input &, State & handler )
10898
{
109-
handler.commit_member();
110-
}
111-
};
112-
113-
template<>
114-
struct action< rules::name_separator >
115-
{
116-
template< typename State >
117-
static void apply( const tao_json_pegtl::input &, State & handler )
118-
{
119-
handler.name_separator();
120-
}
121-
};
122-
123-
template<>
124-
struct action< rules::value_separator >
125-
{
126-
template< typename State >
127-
static void apply( const tao_json_pegtl::input &, State & handler )
128-
{
129-
handler.value_separator();
99+
handler.value();
130100
}
131101
};
132102

include/tao/json/internal/key_state.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace tao
2222
template< typename Handler >
2323
void success( Handler & handler )
2424
{
25-
handler.commit_key( std::move( unescaped ) );
25+
handler.key( std::move( unescaped ) );
2626
}
2727

2828
std::string unescaped;

include/tao/json/internal/value_builder.hh

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,83 +17,99 @@ namespace tao
1717
// class value_handler
1818
// {
1919
// void null() {}
20-
// void true_() {}
21-
// void false_() {}
20+
// void boolean( const bool v ) {}
2221
// void number( const std::int64_t v ) {}
2322
// void number( const std::uint64_t v ) {}
2423
// void number( const double v ) {}
2524
// void string( std::string v ) {}
2625
// void begin_array() {}
27-
// void commit_element() {}
28-
// void element_separator() {}
26+
// void element() {}
2927
// void end_array() {}
3028
// void begin_object() {}
31-
// void commit_key( std::string v ) {}
32-
// void name_separator() {}
33-
// void commit_member() {}
34-
// void value_separator() {}
29+
// void key( std::string v ) {}
30+
// void value() {}
3531
// void end_object() {}
3632
// };
3733

3834
template< template< typename ... > class Traits >
3935
struct value_builder
4036
{
41-
basic_value< Traits > value;
42-
std::vector< basic_value< Traits > > stack;
43-
std::vector< std::string > keys;
44-
45-
void null() { value.unsafe_assign_null(); }
46-
void true_() { value.unsafe_assign_bool( true ); }
47-
void false_() { value.unsafe_assign_bool( false ); }
48-
void number( const std::int64_t v ) { value.unsafe_assign_signed( v ); }
49-
void number( const std::uint64_t v ) { value.unsafe_assign_unsigned( v ); }
50-
void number( const double v ) { value.unsafe_assign_double( v ); }
51-
void string( std::string v ) { value.unsafe_emplace_string( std::move( v ) ); }
37+
public:
38+
basic_value< Traits > value_;
39+
40+
private:
41+
std::vector< basic_value< Traits > > stack_;
42+
std::vector< std::string > keys_;
43+
44+
public:
45+
void null()
46+
{
47+
value_.unsafe_assign_null();
48+
}
49+
50+
void boolean( const bool v )
51+
{
52+
value_.unsafe_assign_bool( v );
53+
}
54+
55+
void number( const std::int64_t v )
56+
{
57+
value_.unsafe_assign_signed( v );
58+
}
59+
60+
void number( const std::uint64_t v )
61+
{
62+
value_.unsafe_assign_unsigned( v );
63+
}
64+
65+
void number( const double v )
66+
{
67+
value_.unsafe_assign_double( v );
68+
}
69+
70+
void string( std::string v )
71+
{
72+
value_.unsafe_emplace_string( std::move( v ) );
73+
}
5274

5375
// array
5476
void begin_array()
5577
{
56-
stack.push_back( empty_array );
78+
stack_.push_back( empty_array );
5779
}
5880

59-
void commit_element()
81+
void element()
6082
{
61-
stack.back().unsafe_emplace_back( std::move( value ) );
83+
stack_.back().unsafe_emplace_back( std::move( value_ ) );
6284
}
6385

64-
void element_separator() {}
65-
6686
void end_array()
6787
{
68-
value = std::move( stack.back() );
69-
stack.pop_back();
88+
value_ = std::move( stack_.back() );
89+
stack_.pop_back();
7090
}
7191

7292
// object
7393
void begin_object()
7494
{
75-
stack.push_back( empty_object );
95+
stack_.push_back( empty_object );
7696
}
7797

78-
void commit_key( std::string v )
98+
void key( std::string v )
7999
{
80-
keys.push_back( std::move( v ) );
100+
keys_.push_back( std::move( v ) );
81101
}
82102

83-
void name_separator() {}
84-
85-
void commit_member()
103+
void value()
86104
{
87-
stack.back().unsafe_emplace( std::move( keys.back() ), std::move( value ) );
88-
keys.pop_back();
105+
stack_.back().unsafe_emplace( std::move( keys_.back() ), std::move( value_ ) );
106+
keys_.pop_back();
89107
}
90108

91-
void value_separator() {}
92-
93109
void end_object()
94110
{
95-
value = std::move( stack.back() );
96-
stack.pop_back();
111+
value_ = std::move( stack_.back() );
112+
stack_.pop_back();
97113
}
98114
};
99115

include/tao/json/internal/value_writer.hh

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#ifndef TAOCPP_JSON_INCLUDE_INTERNAL_VALUE_WRITER_HH
55
#define TAOCPP_JSON_INCLUDE_INTERNAL_VALUE_WRITER_HH
66

7+
#include <ostream>
8+
79
#include "escape.hh"
810

911
#include "../external/double.hh"
@@ -18,27 +20,96 @@ namespace tao
1820
{
1921
private:
2022
std::ostream & os;
23+
bool first;
2124

2225
public:
23-
explicit value_writer( std::ostream & os ) noexcept : os( os ) {}
24-
25-
void null() { os.write( "null", 4 ); }
26-
void true_() { os.write( "true", 4 ); }
27-
void false_() { os.write( "false", 5 ); }
28-
void number( const std::int64_t v ) { os << v; }
29-
void number( const std::uint64_t v ) { os << v; }
30-
void number( const double v ) { json_double_conversion::Dtostr( os, v ); }
31-
void string( const std::string & v ) { internal::escape( os, v ); }
32-
void begin_array() { os.put( '[' ); }
33-
void commit_element() noexcept {}
34-
void element_separator() { os.put( ',' ); }
35-
void end_array() { os.put( ']' ); }
36-
void begin_object() { os.put( '{' ); }
37-
void commit_key( const std::string & v ) { internal::escape( os, v ); }
38-
void name_separator() { os.put( ':' ); }
39-
void commit_member() noexcept {}
40-
void value_separator() { os.put( ',' ); }
41-
void end_object() { os.put( '}' ); }
26+
explicit value_writer( std::ostream & os ) noexcept
27+
: os( os )
28+
{ }
29+
30+
void null()
31+
{
32+
os.write( "null", 4 );
33+
}
34+
35+
void boolean( const bool v )
36+
{
37+
if ( v ) {
38+
os.write( "true", 4 );
39+
}
40+
else {
41+
os.write( "false", 5 );
42+
}
43+
}
44+
45+
void number( const std::int64_t v )
46+
{
47+
os << v;
48+
}
49+
50+
void number( const std::uint64_t v )
51+
{
52+
os << v;
53+
}
54+
55+
void number( const double v )
56+
{
57+
json_double_conversion::Dtostr( os, v );
58+
}
59+
60+
void string( const std::string & v )
61+
{
62+
internal::escape( os, v );
63+
}
64+
65+
// array
66+
void begin_array()
67+
{
68+
os.put( '[' );
69+
first = true;
70+
}
71+
72+
void element()
73+
{
74+
os.put( ',' );
75+
first = false;
76+
}
77+
78+
void end_array()
79+
{
80+
if ( ! first ) {
81+
os.seekp( -1, std::ios_base::cur );
82+
}
83+
os.put( ']' );
84+
first = false;
85+
}
86+
87+
// object
88+
void begin_object()
89+
{
90+
os.put( '{' );
91+
first = true;
92+
}
93+
94+
void key( const std::string & v )
95+
{
96+
if ( ! first ) {
97+
os.put( ',' );
98+
}
99+
internal::escape( os, v );
100+
os.put( ':' );
101+
}
102+
103+
void value()
104+
{
105+
first = false;
106+
}
107+
108+
void end_object()
109+
{
110+
os.put( '}' );
111+
first = false;
112+
}
42113
};
43114

44115
} // internal

0 commit comments

Comments
 (0)