17 #include <daw/daw_move.h>
18 #include <daw/daw_string_view.h>
21 #include <daw/stdinc/declval.h>
22 #include <daw/stdinc/move_fwd_exch.h>
38 namespace json_details {
39 struct handler_result_holder {
42 handler_result_holder( ) =
default;
44 constexpr handler_result_holder(
bool b )
50 constexpr
explicit operator bool( )
const {
55 namespace hnd_checks {
58 has_on_value_handler_impl,
59 std::declval<T>( ).handle_on_value( std::declval<U>( ) ) );
61 template<
typename Handler, json_options_t,
typename JPair>
62 inline constexpr
bool has_on_value_handler_v =
63 has_on_value_handler_impl<Handler, JPair>;
67 has_on_array_start_handler_impl,
68 std::declval<T>( ).handle_on_array_start( std::declval<U>( ) ) );
71 inline constexpr
bool has_on_array_start_handler_v =
72 has_on_array_start_handler_impl<Handler, JValue>;
76 std::declval<T>( ).handle_on_array_end( ) );
80 has_on_class_start_handler_impl,
81 std::declval<T>( ).handle_on_class_start( std::declval<U>( ) ) );
84 inline constexpr
bool has_on_class_start_handler_v =
85 has_on_class_start_handler_impl<Handler, JValue>;
89 std::declval<T>( ).handle_on_class_end( ) );
93 has_on_number_handler_jv_impl,
94 std::declval<T>( ).handle_on_number( std::declval<U>( ) ) );
96 template<
typename Handler, json_options_t P,
typename A>
97 inline constexpr
bool has_on_number_handler_jv_v =
98 has_on_number_handler_jv_impl<Handler, basic_json_value<P, A>>;
101 std::declval<T>( ).handle_on_number( 0.0 ) );
105 has_on_bool_handler_jv_impl,
106 std::declval<T>( ).handle_on_bool( std::declval<U>( ) ) );
108 template<
typename Handler, json_options_t P,
typename A>
109 inline constexpr
bool has_on_bool_handler_jv_v =
110 has_on_bool_handler_jv_impl<Handler, basic_json_value<P, A>>;
113 std::declval<T>( ).handle_on_bool(
true ) );
117 has_on_string_handler_impl,
118 std::declval<T>( ).handle_on_string( std::declval<U>( ) ) );
120 template<
typename Handler, json_options_t P,
typename A>
121 inline constexpr
bool has_on_string_handler_jv_v =
122 has_on_string_handler_impl<Handler, basic_json_value<P, A>>;
125 has_on_string_handler_str_v,
126 std::declval<T>( ).handle_on_string( std::declval<std::string>( ) ) );
130 has_on_null_handler_impl,
131 std::declval<T>( ).handle_on_null( std::declval<U>( ) ) );
133 template<
typename Handler, json_options_t P,
typename A>
134 inline constexpr
bool has_on_null_handler_jv_v =
135 has_on_null_handler_impl<Handler, basic_json_value<P, A>>;
138 std::declval<T>( ).handle_on_null( ) );
142 has_on_error_handler_impl,
143 std::declval<T>( ).handle_on_error( std::declval<U>( ) ) );
145 template<
typename Handler, json_options_t P,
typename A>
146 inline constexpr
bool has_on_error_handler_v =
147 has_on_error_handler_impl<Handler, basic_json_value<P, A>>;
151 constexpr daw::remove_cvref_t<T> as_copy( T &&value ) {
155 template<
typename Handler, json_options_t P,
typename A>
156 inline constexpr handler_result_holder
157 handle_on_value( Handler &&handler, basic_json_pair<P, A> p ) {
158 if constexpr( hnd_checks::has_on_value_handler_v<Handler, P, A> ) {
159 return handler.handle_on_value( std::move( p ) );
162 return handler_result_holder{ };
166 template<
typename Handler, json_options_t P,
typename A>
167 constexpr handler_result_holder
168 handle_on_array_start( Handler &&handler, basic_json_value<P, A> jv ) {
169 if constexpr( hnd_checks::has_on_array_start_handler_v<Handler, P,
171 return handler.handle_on_array_start( std::move( jv ) );
174 return handler_result_holder{ };
178 template<
typename Handler>
179 constexpr handler_result_holder handle_on_array_end( Handler &&handler ) {
180 if constexpr( hnd_checks::has_on_array_end_handler_v<Handler> ) {
181 return handler.handle_on_array_end( );
183 return handler_result_holder{ };
187 template<
typename Handler, json_options_t P,
typename A>
188 constexpr handler_result_holder
189 handle_on_class_start( Handler &&handler, basic_json_value<P, A> jv ) {
190 if constexpr( hnd_checks::has_on_class_start_handler_v<Handler, P,
192 return handler.handle_on_class_start( std::move( jv ) );
195 return handler_result_holder{ };
199 template<
typename Handler>
200 constexpr handler_result_holder handle_on_class_end( Handler &&handler ) {
201 if constexpr( hnd_checks::has_on_class_end_handler_v<Handler> ) {
202 return handler.handle_on_class_end( );
204 return handler_result_holder{ };
208 template<
typename Handler, json_options_t P,
typename A>
209 constexpr handler_result_holder
210 handle_on_number( Handler &&handler, basic_json_value<P, A> &jv ) {
211 if constexpr( hnd_checks::has_on_number_handler_jv_v<Handler, P, A> ) {
212 return handler.handle_on_number( as_copy( jv ) );
213 }
else if constexpr( hnd_checks::has_on_number_handler_dbl_v<
215 return handler.handle_on_number( from_json<double>( jv ) );
218 return handler_result_holder{ };
222 template<
typename Handler, json_options_t P,
typename A>
223 constexpr handler_result_holder
224 handle_on_bool( Handler &&handler, basic_json_value<P, A> jv ) {
225 if constexpr( hnd_checks::has_on_bool_handler_jv_v<Handler, P, A> ) {
226 return handler.handle_on_bool( as_copy( jv ) );
227 }
else if constexpr( hnd_checks::has_on_bool_handler_bl_v<Handler> ) {
228 return handler.handle_on_bool( from_json<bool>( jv ) );
231 return handler_result_holder{ };
235 template<
typename Handler, json_options_t P,
typename A>
236 constexpr handler_result_holder
237 handle_on_string( Handler &&handler, basic_json_value<P, A> &jv ) {
238 if constexpr( hnd_checks::has_on_string_handler_jv_v<Handler, P, A> ) {
239 return handler.handle_on_string( as_copy( jv ) );
240 }
else if constexpr( hnd_checks::has_on_string_handler_str_v<
242 return handler.handle_on_string( jv.get_string( ) );
245 return handler_result_holder{ };
249 template<
typename Handler, json_options_t P,
typename A>
250 constexpr handler_result_holder
251 handle_on_null( Handler &&handler, basic_json_value<P, A> &jv ) {
252 if constexpr( hnd_checks::has_on_null_handler_jv_v<Handler, P, A> ) {
253 return handler.handle_on_null( as_copy( jv ) );
254 }
else if constexpr( hnd_checks::has_on_null_handler_v<Handler> ) {
255 return handler.handle_on_null( );
257 return handler_result_holder{ };
261 template<
typename Handler, json_options_t P,
typename A>
262 constexpr handler_result_holder
263 handle_on_error( Handler &&handler, basic_json_value<P, A> jv ) {
264 if constexpr( hnd_checks::has_on_error_handler_v<Handler, P, A> ) {
265 return handler.handle_on_error( std::move( jv ) );
268 return handler_result_holder{ };
276 template<json_options_t P,
typename A>
280 std::pair<iterator, iterator>
value;
283 template<
typename StackValue>
285 std::vector<StackValue> m_stack{ };
296 m_stack.push_back( std::move( v ) );
300 return m_stack.back( );
312 return m_stack.empty( );
317 typename StackContainerPolicy = use_default,
typename Handler,
321 options::parse_flags_t<ParseFlags...> ) {
324 P, A>::template SetPolicyOptions<ParseFlags...>>;
329 using stack_value_t =
333 auto parent_stack = [] {
334 if constexpr( std::is_same_v<StackContainerPolicy, use_default> ) {
337 return StackContainerPolicy{ };
340 long long class_depth = 0;
341 long long array_depth = 0;
343 auto const move_to_last = [&]( ) {
344 parent_stack.
back( ).value.first = parent_stack.back( ).value.second;
347 auto const process_value = [&]( json_value_t p ) {
349 auto result = json_details::handle_on_value( handler, p );
350 switch( result.value ) {
352 parent_stack.clear( );
363 switch( jv.type( ) ) {
364 case JsonBaseParseTypes::Array: {
366 auto result = json_details::handle_on_array_start( handler, jv );
367 switch( result.value ) {
369 parent_stack.clear( );
377 parent_stack.push_back(
378 { StackParseStateType::Array,
379 std::pair<iterator, iterator>( jv.begin( ), jv.end( ) ) } );
381 case JsonBaseParseTypes::Class: {
383 auto result = json_details::handle_on_class_start( handler, jv );
384 switch( result.value ) {
386 parent_stack.clear( );
394 parent_stack.push_back(
395 { StackParseStateType::Class,
396 std::pair<iterator, iterator>( jv.begin( ), jv.end( ) ) } );
398 case JsonBaseParseTypes::Number: {
399 auto result = json_details::handle_on_number( handler, jv );
400 switch( result.value ) {
402 parent_stack.clear( );
411 case JsonBaseParseTypes::Bool: {
412 auto result = json_details::handle_on_bool( handler, jv );
413 switch( result.value ) {
415 parent_stack.clear( );
424 case JsonBaseParseTypes::String: {
425 auto result = json_details::handle_on_string( handler, jv );
426 switch( result.value ) {
428 parent_stack.clear( );
437 case JsonBaseParseTypes::Null: {
438 auto result = json_details::handle_on_null( handler, jv );
439 switch( result.value ) {
441 parent_stack.clear( );
450 case JsonBaseParseTypes::None:
452 auto result = json_details::handle_on_error( handler, jv );
453 switch( result.value ) {
455 parent_stack.clear( );
467 auto const process_range = [&]( stack_value_t v ) {
468 if( v.value.first != v.value.second ) {
469 auto jv = *v.value.first;
471 parent_stack.push_back( std::move( v ) );
472 process_value( std::move( jv ) );
475 case StackParseStateType::Class: {
477 ( class_depth > 0 ) &
478 ( v.value.first.get_raw_state( ).has_more( ) and
479 v.value.first.get_raw_state( ).front( ) ==
'}' ),
480 ErrorReason::InvalidEndOfValue );
482 auto result = json_details::handle_on_class_end( handler );
483 switch( result.value ) {
485 parent_stack.clear( );
492 case StackParseStateType::Array: {
494 ( array_depth > 0 ) &
495 ( v.value.first.get_raw_state( ).has_more( ) and
496 v.value.first.get_raw_state( ).front( ) ==
']' ),
497 ErrorReason::InvalidEndOfValue );
499 auto result = json_details::handle_on_array_end( handler );
500 switch( result.value ) {
502 parent_stack.clear( );
513 process_value( json_value_t{ std::nullopt, std::move( jvalue ) } );
515 while( not parent_stack.empty( ) ) {
516 auto v = std::move( parent_stack.back( ) );
517 parent_stack.pop_back( );
521 ErrorReason::InvalidEndOfValue );
525 typename StackContainerPolicy = use_default,
typename Handler>
526 DAW_ATTRIB_INLINE constexpr
void
529 options::parse_flags<> );
532 template<
typename Handler,
auto... ParseFlags>
533 DAW_ATTRIB_INLINE
void
535 options::parse_flags_t<ParseFlags...> pflags ) {
538 DAW_FWD2( Handler, handler ), pflags );
541 template<
typename Handler>
543 Handler &&handler ) {
546 DAW_FWD2( Handler, handler ),
547 options::parse_flags<> );
DefaultJsonEventParserStackPolicy()=default
CPP20CONSTEXPR void clear()
CPP20CONSTEXPR void pop_back()
CPP20CONSTEXPR bool empty() const
CPP20CONSTEXPR void push_back(value_type &&v)
std::ptrdiff_t difference_type
CPP20CONSTEXPR reference back()
#define daw_json_assert_weak(Bool,...)
Assert that Bool is true when in Checked Input mode If false pass rest of args to daw_json_error.
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
#define DAW_JSON_MAKE_REQ_TRAIT(Name,...)
Disable concepts on gcc < 13.3. See https://github.com/beached/daw_json_link/issues/454.
#define DAW_JSON_MAKE_REQ_TRAIT2(Name,...)
constexpr void json_event_parser(basic_json_value< P, A > bjv, Handler &&handler, options::parse_flags_t< ParseFlags... >)
json_parse_handler_result
@ SkipClassArray
Skip the rest of this class or array.
@ Continue
Continue parsing with next element/member.
@ Complete
We are completed and do not wish to see any more.
basic_json_pair<> json_pair
std::uint32_t json_options_t
daw::conditional_t< ParsePolicy::is_default_parse_policy, DefaultParsePolicy, ParsePolicy > TryDefaultParsePolicy
basic_json_value(BasicParsePolicy< PolicyFlags, Allocator >) -> basic_json_value< PolicyFlags, Allocator >
Customization point traits.
std::pair< iterator, iterator > value
Handles the bounds and policy items for parsing execution and comments.
Iterator for iterating over arbitrary JSON members and array elements.
A non-owning container for arbitrary JSON values that allows movement/iteration through.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.