18 #include <daw/daw_data_end.h>
19 #include <daw/traits/daw_traits_conditional.h>
21 #include <daw/stdinc/data_access.h>
22 #include <daw/stdinc/move_fwd_exch.h>
23 #include <string_view>
36 template<
typename JsonMember,
bool KnownBounds,
typename String,
38 [[nodiscard]] constexpr
auto
39 from_json( String &&json_data, options::parse_flags_t<PolicyFlags...> ) {
41 json_details::is_string_view_like_v<String>,
42 "String type must have a be a contiguous range of Characters" );
44 ErrorReason::EmptyJSONDocument );
46 ErrorReason::EmptyJSONDocument );
49 json_details::has_json_deduced_type_v<JsonMember>,
50 "Missing specialization of daw::json::json_data_contract for class "
51 "mapping or specialization of daw::json::json_link_basic_type_map" );
52 using json_member = json_details::json_deduced_type<JsonMember>;
58 using policy_zstring_t = json_details::apply_zstring_policy_option_t<
59 ParsePolicy, String, options::ZeroTerminatedString::yes>;
62 daw::conditional_t<policy_zstring_t::is_default_parse_policy,
64 auto first = std::data( json_data );
65 auto last = daw::data_end( json_data );
66 if( first != last and last[-1] == 0 ) {
69 auto parse_state = ParseState( first, last );
71 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
73 json_details::parse_value<json_member, KnownBounds,
74 json_member::expected_type>( parse_state );
75 parse_state.trim_left( );
80 return json_details::parse_value<json_member, KnownBounds,
81 json_member::expected_type>(
94 template<
typename JsonMember,
bool KnownBounds,
typename String>
95 [[nodiscard]] constexpr
auto from_json( String &&json_data ) {
97 json_details::is_string_view_like_v<String>,
98 "String type must have a be a contiguous range of Characters" );
99 return from_json<JsonMember, KnownBounds>( DAW_FWD( json_data ),
100 options::parse_flags<> );
111 template<
typename JsonMember,
bool KnownBounds,
typename String,
112 typename Allocator,
auto... PolicyFlags>
113 [[nodiscard]] constexpr
auto
115 options::parse_flags_t<PolicyFlags...> ) {
117 json_details::is_string_view_like_v<String>,
118 "String type must have a be a contiguous range of Characters" );
120 ErrorReason::EmptyJSONDocument );
122 ErrorReason::EmptyJSONPath );
124 using json_member = json_details::json_deduced_type<JsonMember>;
127 json_details::has_unnamed_default_type_mapping_v<JsonMember>,
128 "Missing specialization of daw::json::json_data_contract for class "
129 "mapping or specialization of daw::json::json_link_basic_type_map" );
131 char const *f = std::data( json_data );
132 char const *l = daw::data_end( json_data );
140 using ParseState = json_details::apply_zstring_policy_option_t<
141 ParsePolicy, String, options::ZeroTerminatedString::yes>;
143 auto parse_state = ParseState::with_allocator( f, l, a );
144 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
146 json_details::parse_value<json_member, KnownBounds,
147 json_member::expected_type>( parse_state );
148 parse_state.trim_left( );
153 return json_details::parse_value<json_member, KnownBounds,
154 json_member::expected_type>(
167 template<
typename JsonMember,
bool KnownBounds,
typename String,
170 Allocator
const &alloc ) {
172 json_details::is_string_view_like_v<String>,
173 "String type must have a be a contiguous range of Characters" );
174 return from_json_alloc<JsonMember, KnownBounds>(
175 DAW_FWD( json_data ), alloc, options::parse_flags<> );
188 template<
typename JsonMember,
bool KnownBounds,
typename String,
190 [[nodiscard]] constexpr
auto
191 from_json( String &&json_data, std::string_view member_path,
192 options::parse_flags_t<PolicyFlags...> ) {
194 json_details::is_string_view_like_v<String>,
195 "String type must have a be a contiguous range of Characters" );
198 ErrorReason::EmptyJSONDocument );
200 ErrorReason::EmptyJSONPath );
202 ErrorReason::EmptyJSONPath );
204 using json_member = json_details::json_deduced_type<JsonMember>;
206 json_details::has_unnamed_default_type_mapping_v<JsonMember>,
207 "Missing specialization of daw::json::json_data_contract for class "
208 "mapping or specialization of daw::json::json_link_basic_type_map" );
215 using policy_zstring_t = json_details::apply_zstring_policy_option_t<
216 ParsePolicy, String, options::ZeroTerminatedString::yes>;
219 daw::conditional_t<policy_zstring_t::is_default_parse_policy,
221 auto first = std::data( json_data );
222 auto last = daw::data_end( json_data );
223 if( first != last and last[-1] == 0 ) {
227 jv = jv.find_member( member_path );
229 if constexpr( json_details::is_json_nullable_v<json_member> ) {
231 return json_details::construct_nullable_empty<
232 json_details::json_constructor_t<json_member>>( );
237 auto parse_state = jv.get_raw_state( );
238 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
240 json_details::parse_value<json_member, KnownBounds,
241 json_member::expected_type>( parse_state );
242 parse_state.trim_left( );
247 return json_details::parse_value<json_member, KnownBounds,
248 json_member::expected_type>(
263 template<
typename JsonMember,
bool KnownBounds,
typename String>
264 [[nodiscard]] constexpr
auto from_json( String &&json_data,
265 std::string_view member_path ) {
267 json_details::is_string_view_like_v<String>,
268 "String type must have a be a contiguous range of Characters" );
270 return from_json<JsonMember, KnownBounds>(
271 DAW_FWD( json_data ), member_path, options::parse_flags<> );
284 template<
typename JsonMember,
bool KnownBounds,
typename String,
285 typename Allocator,
auto... PolicyFlags>
286 [[nodiscard]] constexpr
auto
288 Allocator
const &alloc,
289 options::parse_flags_t<PolicyFlags...> ) {
292 json_details::is_string_view_like_v<String>,
293 "String type must have a be a contiguous range of Characters" );
295 ErrorReason::EmptyJSONDocument );
297 ErrorReason::EmptyJSONDocument );
299 ErrorReason::EmptyJSONPath );
301 using json_member = json_details::json_deduced_type<JsonMember>;
303 json_details::has_unnamed_default_type_mapping_v<JsonMember>,
304 "Missing specialization of daw::json::json_data_contract for class "
305 "mapping or specialization of daw::json::json_link_basic_type_map" );
312 using ParseState = json_details::apply_zstring_policy_option_t<
313 ParsePolicy, String, options::ZeroTerminatedString::yes>;
315 auto first = std::data( json_data );
316 auto last = daw::data_end( json_data );
317 if( first != last and last[-1] == 0 ) {
321 ParseState( first, last, first, last ).with_allocator( alloc ) );
322 jv = jv.find_member( member_path );
324 if constexpr( json_details::is_json_nullable_v<json_member> ) {
326 return json_details::construct_nullable_empty<
327 json_details::json_constructor_t<json_member>>( );
332 auto parse_state = jv.get_raw_state( );
333 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
335 json_details::parse_value<json_member, KnownBounds,
336 json_member::expected_type>( parse_state );
337 parse_state.trim_left( );
342 return json_details::parse_value<json_member, KnownBounds,
343 json_member::expected_type>(
358 template<
typename JsonMember,
bool KnownBounds,
typename String,
361 std::string_view member_path,
362 Allocator
const &alloc ) {
364 json_details::is_string_view_like_v<String>,
365 "String type must have a be a contiguous range of Characters" );
367 return from_json_alloc<JsonMember, KnownBounds>(
368 DAW_FWD( json_data ), member_path, alloc, options::parse_flags<> );
379 typename Allocator,
auto... PolicyFlags>
380 [[nodiscard]]
inline constexpr
auto
382 options::parse_flags_t<PolicyFlags...> ) {
383 using json_member = json_details::json_deduced_type<JsonMember>;
385 json_details::has_unnamed_default_type_mapping_v<JsonMember>,
386 "Missing specialization of daw::json::json_data_contract for class "
387 "mapping or specialization of daw::json::json_link_basic_type_map" );
389 P, Allocator>::template SetPolicyOptions<PolicyFlags...>;
391 daw::conditional_t<ParsePolicy::is_default_parse_policy,
393 auto const old_parse_state = value.get_raw_state( );
395 ParseState( old_parse_state.first, old_parse_state.last,
396 old_parse_state.class_first, old_parse_state.class_last,
397 old_parse_state.get_allocator( ) );
399 return json_details::parse_value<json_member, KnownBounds,
400 json_member::expected_type>(
411 template<
typename JsonMember,
bool KnownBounds,
json_options_t PolicyFlags,
413 [[nodiscard]]
inline constexpr
auto
416 return from_json<JsonMember, KnownBounds>( std::move( value ),
417 options::parse_flags<> );
431 typename Allocator,
auto... PolicyFlags>
432 [[nodiscard]] constexpr
auto
434 std::string_view member_path,
435 options::parse_flags_t<PolicyFlags...> ) {
436 using json_member = json_details::json_deduced_type<JsonMember>;
438 json_details::has_unnamed_default_type_mapping_v<JsonMember>,
439 "Missing specialization of daw::json::json_data_contract for class "
440 "mapping or specialization of daw::json::json_link_basic_type_map" );
443 auto const old_parse_state = value.get_raw_state( );
445 daw::conditional_t<ParsePolicy::is_default_parse_policy,
448 ParseState( old_parse_state.first, old_parse_state.last,
449 old_parse_state.class_first, old_parse_state.class_last,
450 old_parse_state.get_allocator( ) ) );
452 jv = jv.find_member( member_path );
454 if constexpr( json_details::is_json_nullable_v<json_member> ) {
456 return json_details::construct_nullable_empty<
457 json_details::json_constructor_t<json_member>>( );
462 auto parse_state = jv.get_raw_state( );
463 return json_details::parse_value<json_member, KnownBounds,
464 json_member::expected_type>(
478 template<
typename JsonMember,
bool KnownBounds,
json_options_t PolicyFlags,
481 std::string_view member_path ) {
482 return from_json<JsonMember, KnownBounds>(
483 std::move( value ), member_path, options::parse_flags<> );
496 template<
typename JsonElement,
typename Container,
typename Constructor,
497 bool KnownBounds,
typename String,
auto... PolicyFlags>
498 [[nodiscard]] constexpr Container
500 options::parse_flags_t<PolicyFlags...> ) {
502 json_details::is_string_view_like_v<String>,
503 "String type must have a be a contiguous range of Characters" );
506 ErrorReason::EmptyJSONDocument );
508 ErrorReason::EmptyJSONPath );
510 json_details::has_unnamed_default_type_mapping_v<JsonElement>,
511 "Missing specialization of daw::json::json_data_contract for class "
512 "mapping or specialization of daw::json::json_link_basic_type_map" );
513 using element_type = json_details::json_deduced_type<JsonElement>;
514 static_assert( not std::is_same_v<element_type, void>,
515 "Unknown JsonElement type." );
518 json_base::json_array<JsonElement, Container, Constructor>;
525 using policy_zstring_t = json_details::apply_zstring_policy_option_t<
526 ParsePolicy, String, options::ZeroTerminatedString::yes>;
529 daw::conditional_t<policy_zstring_t::is_default_parse_policy,
532 ParseState{ std::data( json_data ), daw::data_end( json_data ) };
534 parse_state.trim_left_unchecked( );
535 #if defined( DAW_JSON_BUGFIX_FROM_JSON_001 )
537 ErrorReason::InvalidArrayStart, parse_state );
540 ErrorReason::InvalidArrayStart, parse_state );
542 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
544 json_details::parse_value_array<parser_t, KnownBounds>( parse_state );
545 parse_state.trim_left( );
550 return json_details::parse_value_array<parser_t, KnownBounds>(
565 template<
typename JsonElement,
typename Container,
typename Constructor,
566 bool KnownBounds,
typename String>
569 json_details::is_string_view_like_v<String>,
570 "String type must have a be a contiguous range of Characters" );
571 return from_json_array<JsonElement, Container, Constructor, KnownBounds>(
572 DAW_FWD( json_data ), options::parse_flags<> );
588 template<
typename JsonElement,
typename Container,
typename Constructor,
589 bool KnownBounds,
typename String,
auto... PolicyFlags>
590 [[nodiscard]] constexpr Container
592 options::parse_flags_t<PolicyFlags...> ) {
594 json_details::is_string_view_like_v<String>,
595 "String type must have a be a contiguous range of Characters" );
598 ErrorReason::EmptyJSONDocument );
600 ErrorReason::EmptyJSONPath );
602 ErrorReason::EmptyJSONPath );
604 json_details::has_unnamed_default_type_mapping_v<JsonElement>,
605 "Missing specialization of daw::json::json_data_contract for class "
606 "mapping or specialization of daw::json::json_link_basic_type_map" );
607 using element_type = json_details::json_deduced_type<JsonElement>;
608 static_assert( not std::is_same_v<element_type, void>,
609 "Unknown JsonElement type." );
612 json_base::json_array<JsonElement, Container, Constructor>;
619 using policy_zstring_t = json_details::apply_zstring_policy_option_t<
620 ParsePolicy, String, options::ZeroTerminatedString::yes>;
623 daw::conditional_t<policy_zstring_t::is_default_parse_policy,
625 auto first = std::data( json_data );
626 auto last = daw::data_end( json_data );
627 if( first != last and last[-1] == 0 ) {
631 jv = jv.find_member( member_path );
633 if constexpr( json_details::is_json_nullable_v<parser_t> ) {
635 return json_details::construct_nullable_empty<
636 json_details::json_constructor_t<parser_t>>( );
641 auto parse_state = jv.get_raw_state( );
642 parse_state.trim_left_unchecked( );
643 #if defined( DAW_JSON_BUGFIX_FROM_JSON_001 )
645 ErrorReason::InvalidArrayStart, parse_state );
648 ErrorReason::InvalidArrayStart, parse_state );
650 if constexpr( ParseState::must_verify_end_of_data_is_valid ) {
652 json_details::parse_value_array<parser_t, KnownBounds>( parse_state );
653 parse_state.trim_left( );
658 return json_details::parse_value_array<parser_t, KnownBounds>(
676 template<
typename JsonElement,
typename Container,
typename Constructor,
677 bool KnownBounds,
typename String>
678 [[nodiscard]] constexpr Container
681 json_details::is_string_view_like_v<String>,
682 "String type must have a be a contiguous range of Characters" );
684 return from_json_array<JsonElement, Container, Constructor, KnownBounds>(
685 DAW_FWD( json_data ), member_path, options::parse_flags<> );
#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.
constexpr Container from_json_array(String &&json_data, options::parse_flags_t< PolicyFlags... >)
Parse JSON data where the root item is an array.
constexpr auto from_json(String &&json_data, options::parse_flags_t< PolicyFlags... >)
Construct the JSONMember from the JSON document argument.
constexpr auto from_json_alloc(String &&json_data, Allocator const &alloc, options::parse_flags_t< PolicyFlags... >)
Construct the JSONMember from the JSON document argument.
std::uint32_t json_options_t
basic_json_value(BasicParsePolicy< PolicyFlags, Allocator >) -> basic_json_value< PolicyFlags, Allocator >
Customization point traits.
Handles the bounds and policy items for parsing execution and comments.
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.