Skip to content

Commit 7890c5a

Browse files
committed
Finalize SAX API naming
1 parent 41380ba commit 7890c5a

File tree

9 files changed

+55
-34
lines changed

9 files changed

+55
-34
lines changed

include/tao/json/from_string.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace tao
2020
{
2121
sax::to_basic_value< Traits > handler;
2222
sax::from_string( data, size, handler, source, line, column );
23-
return std::move( handler.result() );
23+
return std::move( handler.value );
2424
}
2525

2626
template< template< typename ... > class Traits >

include/tao/json/internal/action.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace tao
9696
template< typename State >
9797
static void apply( const tao_json_pegtl::input &, State & handler )
9898
{
99-
handler.value();
99+
handler.member();
100100
}
101101
};
102102

include/tao/json/parse_file.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace tao
1919
{
2020
sax::to_basic_value< Traits > handler;
2121
sax::parse_file( filename, handler );
22-
return std::move( handler.result() );
22+
return std::move( handler.value );
2323
}
2424

2525
template< template< typename ... > class Traits >

include/tao/json/sax/from_string.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace tao
1616
{
1717
namespace sax
1818
{
19+
// SAX producer to parse a JSON string representation
1920
template< typename Handler >
2021
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 )
2122
{

include/tao/json/sax/parse_file.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace tao
1616
{
1717
namespace sax
1818
{
19+
// SAX producer to parse a file containing a JSON string representation
1920
template< typename Handler >
2021
void parse_file( const std::string & filename, Handler & handler )
2122
{

include/tao/json/sax/to_pretty_stream.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace tao
1919
{
2020
namespace sax
2121
{
22+
// SAX consumer to build a JSON pretty string representation
2223
class to_pretty_stream
2324
{
2425
private:
@@ -129,7 +130,7 @@ namespace tao
129130
after_key = true;
130131
}
131132

132-
void value()
133+
void member()
133134
{
134135
os.put( ',' );
135136
first_value = false;

include/tao/json/sax/to_stream.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace tao
1717
{
1818
namespace sax
1919
{
20+
// SAX consumer to build a JSON string representation
2021
class to_stream
2122
{
2223
private:
@@ -101,7 +102,7 @@ namespace tao
101102
os.put( ':' );
102103
}
103104

104-
void value()
105+
void member()
105106
{
106107
first = false;
107108
}

include/tao/json/sax/to_value.hh

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,77 @@ namespace tao
1717
// class value_handler
1818
// {
1919
// void null() {}
20+
//
2021
// void boolean( const bool v ) {}
22+
//
2123
// void number( const std::int64_t v ) {}
2224
// void number( const std::uint64_t v ) {}
2325
// void number( const double v ) {}
24-
// void string( std::string v ) {}
26+
//
27+
// // the producer may call either one of the next two for a string,
28+
// // only implement the first one if you don't need the second.
29+
// void string( const std::string & v ) {}
30+
// void string( std::string && v ) {}
31+
//
32+
// // array
2533
// void begin_array() {}
2634
// void element() {}
2735
// void end_array() {}
36+
//
37+
// // object
2838
// void begin_object() {}
29-
// void key( std::string v ) {}
30-
// void value() {}
39+
// // the producer may call either one of the next two for a key,
40+
// // only implement the first one if you don't need the second.
41+
// void key( const std::string & v ) {}
42+
// void key( std::string && v ) {}
43+
// void member() {}
3144
// void end_object() {}
3245
// };
3346

47+
// SAX consumer to build a JSON value
3448
template< template< typename ... > class Traits >
3549
class to_basic_value
3650
{
3751
private:
38-
basic_value< Traits > value_;
39-
4052
std::vector< basic_value< Traits > > stack_;
4153
std::vector< std::string > keys_;
4254

4355
public:
44-
basic_value< Traits > & result() noexcept
45-
{
46-
return value_;
47-
}
48-
49-
const basic_value< Traits > & result() const noexcept
50-
{
51-
return value_;
52-
}
56+
basic_value< Traits > value;
5357

5458
void null()
5559
{
56-
value_.unsafe_assign_null();
60+
value.unsafe_assign_null();
5761
}
5862

5963
void boolean( const bool v )
6064
{
61-
value_.unsafe_assign_bool( v );
65+
value.unsafe_assign_bool( v );
6266
}
6367

6468
void number( const std::int64_t v )
6569
{
66-
value_.unsafe_assign_signed( v );
70+
value.unsafe_assign_signed( v );
6771
}
6872

6973
void number( const std::uint64_t v )
7074
{
71-
value_.unsafe_assign_unsigned( v );
75+
value.unsafe_assign_unsigned( v );
7276
}
7377

7478
void number( const double v )
7579
{
76-
value_.unsafe_assign_double( v );
80+
value.unsafe_assign_double( v );
81+
}
82+
83+
void string( const std::string & v )
84+
{
85+
value.unsafe_emplace_string( v );
7786
}
7887

79-
void string( std::string v )
88+
void string( std::string && v )
8089
{
81-
value_.unsafe_emplace_string( std::move( v ) );
90+
value.unsafe_emplace_string( std::move( v ) );
8291
}
8392

8493
// array
@@ -89,12 +98,12 @@ namespace tao
8998

9099
void element()
91100
{
92-
stack_.back().unsafe_emplace_back( std::move( value_ ) );
101+
stack_.back().unsafe_emplace_back( std::move( value ) );
93102
}
94103

95104
void end_array()
96105
{
97-
value_ = std::move( stack_.back() );
106+
value = std::move( stack_.back() );
98107
stack_.pop_back();
99108
}
100109

@@ -104,20 +113,25 @@ namespace tao
104113
stack_.push_back( empty_object );
105114
}
106115

107-
void key( std::string v )
116+
void key( const std::string & v )
117+
{
118+
keys_.push_back( v );
119+
}
120+
121+
void key( std::string && v )
108122
{
109123
keys_.push_back( std::move( v ) );
110124
}
111125

112-
void value()
126+
void member()
113127
{
114-
stack_.back().unsafe_emplace( std::move( keys_.back() ), std::move( value_ ) );
128+
stack_.back().unsafe_emplace( std::move( keys_.back() ), std::move( value ) );
115129
keys_.pop_back();
116130
}
117131

118132
void end_object()
119133
{
120-
value_ = std::move( stack_.back() );
134+
value = std::move( stack_.back() );
121135
stack_.pop_back();
122136
}
123137
};

include/tao/json/sax/traverse_value.hh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace tao
1414
{
1515
namespace sax
1616
{
17+
// SAX producer to generate events from a JSON value
1718
template< template< typename ... > class Traits, typename Handler >
1819
void traverse_value( const basic_value< Traits > & v, Handler & handler )
1920
{
@@ -49,7 +50,7 @@ namespace tao
4950
for( const auto & e : v.unsafe_get_object() ) {
5051
handler.key( e.first );
5152
sax::traverse_value( e.second, handler );
52-
handler.value();
53+
handler.member();
5354
}
5455
handler.end_object();
5556
break;
@@ -66,6 +67,8 @@ namespace tao
6667
}
6768
}
6869

70+
// SAX producer to generate events from an rvalue JSON value
71+
// note: strings from the source might be moved in the handler
6972
template< template< typename ... > class Traits, typename Handler >
7073
void traverse_value( basic_value< Traits > && v, Handler & handler )
7174
{
@@ -101,7 +104,7 @@ namespace tao
101104
for( const auto & e : v.unsafe_get_object() ) {
102105
handler.key( e.first );
103106
sax::traverse_value( e.second, handler );
104-
handler.value();
107+
handler.member();
105108
}
106109
handler.end_object();
107110
break;

0 commit comments

Comments
 (0)