Skip to content

Commit 663b2e4

Browse files
committed
Simplify
1 parent e6f7559 commit 663b2e4

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

include/tao/json/internal/value_builder.hh

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,66 +37,56 @@ namespace tao
3737
struct value_builder
3838
{
3939
basic_value< Traits > value;
40+
std::vector< basic_value< Traits > > stack;
41+
std::vector< std::string > keys;
4042

41-
basic_value< Traits > * current;
42-
std::vector< basic_value< Traits > * > stack;
43-
44-
value_builder() : current( & value ) {}
45-
46-
void null() { current->unsafe_assign_null(); }
47-
void true_() { current->unsafe_assign_bool( true ); }
48-
void false_() { current->unsafe_assign_bool( false ); }
49-
void number( const std::int64_t v ) { current->unsafe_assign_signed( v ); }
50-
void number( const std::uint64_t v ) { current->unsafe_assign_unsigned( v ); }
51-
void number( const double v ) { current->unsafe_assign_double( v ); }
52-
void string( std::string && v ) { current->unsafe_emplace_string( std::move( v ) ); }
43+
void null() { value.unsafe_assign_null(); }
44+
void true_() { value.unsafe_assign_bool( true ); }
45+
void false_() { value.unsafe_assign_bool( false ); }
46+
void number( const std::int64_t v ) { value.unsafe_assign_signed( v ); }
47+
void number( const std::uint64_t v ) { value.unsafe_assign_unsigned( v ); }
48+
void number( const double v ) { value.unsafe_assign_double( v ); }
49+
void string( std::string && v ) { value.unsafe_emplace_string( std::move( v ) ); }
5350

5451
// array
5552
void begin_array()
5653
{
57-
current->unsafe_emplace_array();
58-
current->unsafe_emplace_back( json::null );
59-
stack.push_back( current );
60-
current = & current->unsafe_get_array().back();
54+
stack.push_back( empty_array );
6155
}
6256

6357
void commit_element()
6458
{
65-
stack.back()->unsafe_emplace_back( json::null );
66-
current = & stack.back()->unsafe_get_array().back();
59+
stack.back().unsafe_emplace_back( std::move( value ) );
6760
}
6861

6962
void end_array()
7063
{
71-
current = stack.back();
72-
current->unsafe_get_array().pop_back();
64+
value = std::move( stack.back() );
7365
stack.pop_back();
7466
}
7567

7668
// object
7769
void begin_object()
7870
{
79-
current->unsafe_emplace_object();
71+
stack.push_back( empty_object );
8072
}
8173

8274
void commit_key( std::string && v )
8375
{
84-
const auto r = current->unsafe_emplace( std::move( v ), json::null );
85-
if ( ! r.second ) {
86-
// TODO: throw on duplicate key? offer a choice?
87-
r.first->second = json::null;
88-
}
89-
stack.push_back( current );
90-
current = & r.first->second;
76+
keys.push_back( std::move( v ) );
9177
}
9278

9379
void commit_member()
9480
{
95-
current = stack.back();
96-
stack.pop_back();
81+
stack.back().unsafe_emplace( std::move( keys.back() ), std::move( value ) );
82+
keys.pop_back();
9783
}
9884

99-
void end_object() {}
85+
void end_object()
86+
{
87+
value = std::move( stack.back() );
88+
stack.pop_back();
89+
}
10090
};
10191

10292
} // internal

0 commit comments

Comments
 (0)