23#include <daw/cpp_17.h>
24#include <daw/daw_attributes.h>
25#include <daw/daw_likely.h>
26#include <daw/daw_traits.h>
45 template<
json_options_t PolicyFlags = json_details::default_policy_flag,
46 typename Allocator = json_details::NoAllocator>
50 static constexpr bool is_default_parse_policy =
51 PolicyFlags == json_details::default_policy_flag and
52 std::is_same_v<Allocator, json_details::NoAllocator>;
64 static constexpr bool is_unchecked_input =
65 json_details::get_bits_for<options::CheckedParseMode>( PolicyFlags ) ==
66 options::CheckedParseMode::no;
72 switch_t<json_details::get_bits_for<options::ExecModeTypes,
73 std::size_t>( PolicyFlags ),
81 static constexpr bool allow_escaped_names =
82 json_details::get_bits_for<options::AllowEscapedNames>( PolicyFlags ) ==
83 options::AllowEscapedNames::yes;
88 static constexpr bool force_name_equal_check =
89 json_details::get_bits_for<options::ForceFullNameCheck>(
90 PolicyFlags ) == options::ForceFullNameCheck::yes;
96 json_details::get_bits_for<options::ZeroTerminatedString>(
97 PolicyFlags ) == options::ZeroTerminatedString::yes;
102 static constexpr bool precise_ieee754 =
103 json_details::get_bits_for<options::IEEE754Precise>( PolicyFlags ) ==
104 options::IEEE754Precise::yes;
109 static constexpr bool minified_document =
110 json_details::get_bits_for<options::MinifiedDocument>( PolicyFlags ) ==
111 options::MinifiedDocument::yes;
116 static constexpr bool exclude_special_escapes =
117 json_details::get_bits_for<options::ExcludeSpecialEscapes>(
118 PolicyFlags ) == options::ExcludeSpecialEscapes::yes;
121 static constexpr bool allow_leading_zero_plus =
true;
123 static constexpr bool use_exact_mappings_by_default =
124 json_details::get_bits_for<options::UseExactMappingsByDefault>(
125 PolicyFlags ) == options::UseExactMappingsByDefault::yes;
127 static constexpr bool must_verify_end_of_data_is_valid =
128 json_details::get_bits_for<options::MustVerifyEndOfDataIsValid>(
129 PolicyFlags ) == options::MustVerifyEndOfDataIsValid::yes;
131 static constexpr bool expect_long_strings =
132 json_details::get_bits_for<options::ExpectLongNames>( PolicyFlags ) ==
133 options::ExpectLongNames::yes;
136 switch_t<json_details::get_bits_for<options::PolicyCommentTypes,
137 std::size_t>( PolicyFlags ),
145 std::size_t counter = 0;
147 template<
auto... PolicyOptions>
149 json_details::set_bits( PolicyFlags, PolicyOptions... ), Allocator>;
161 : json_details::AllocatorWrapper<Allocator>( alloc )
172 , class_last( cl ) {}
175 iterator cl, Allocator
const &alloc )
176 : json_details::AllocatorWrapper<Allocator>( alloc )
180 , class_last( cl ) {}
184 iterator cf = iterator{ }, iterator cl = iterator{ } )
const {
185 BasicParsePolicy result = *
this;
193 result.class_first = cf;
196 result.class_last = cl;
202 return json_details::AllocatorWrapper<Allocator>::get_allocator( );
205 template<
typename Alloc>
208 template<
typename Alloc>
211 Alloc
const &alloc ) {
217 json_details::NoAllocator
const & ) {
221 template<
typename Alloc>
222 [[nodiscard]]
constexpr auto
224 if constexpr( std::is_same_v<Alloc, json_details::NoAllocator> ) {
227 auto result = with_allocator( first, last, class_first, class_last,
234 template<
typename Alloc>
235 [[nodiscard]]
constexpr with_allocator_type<Alloc>
238 with_allocator( first, last, class_first, class_last, alloc );
239 result.counter = counter;
243 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr auto
251 template<
typename Alloc>
254 return { f, l, f, l, alloc };
257 template<
typename Alloc>
260 json_details::NoAllocator
const & ) {
261 return { f, l, f, l };
280 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr bool empty( )
const {
285 return first >= last or *first ==
'\0';
287 return first >= last;
291 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr bool has_more( )
const {
295 template<std::
size_t N>
297 static_assert( N > 0 );
298 if( size( ) < ( N - 1 ) ) {
302 for( std::size_t n = 0; n < ( N - 1 ); ++n ) {
303 result = result & ( first[n] == rhs[n] );
311 json_details::memchr_unchecked<c, exec_tag_t, expect_long_strings>(
318 json_details::memchr_checked<c, exec_tag_t, expect_long_strings>(
324 if( is_unchecked_input ) {
325 move_to_next_of_unchecked<c>( );
327 move_to_next_of_checked<c>( );
331 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr char front( )
const {
341 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr std::size_t
size( )
const {
342 assert( last >= first );
343 return static_cast<std::size_t
>( last - first );
346 [[nodiscard]]
constexpr bool is_null( )
const {
347 return first ==
nullptr;
355 first +=
static_cast<std::ptrdiff_t
>( n );
369 class_first = new_pos.
f;
370 class_last = new_pos.
l;
374 return { class_first, class_last };
378 return CommentPolicy::trim_left_checked( *
this );
382 return CommentPolicy::trim_left_unchecked( *
this );
386 return CommentPolicy::is_literal_end( *first );
389 [[nodiscard]] DAW_ATTRIB_INLINE
constexpr bool
391 return (
static_cast<unsigned>(
static_cast<unsigned char>( *first ) ) -
396 return DAW_LIKELY( first < last ) and *first ==
'[';
400 return DAW_LIKELY( first < last ) and *first ==
'{';
404 return DAW_LIKELY( first < last ) and *first ==
'}';
408 return DAW_LIKELY( first < last ) and *first ==
'"';
412 if constexpr( is_unchecked_input ) {
413 trim_left_unchecked( );
415 trim_left_checked( );
420 trim_left_unchecked( );
421 if( *first ==
',' ) {
423 trim_left_unchecked( );
427 DAW_ATTRIB_FLATINLINE
inline constexpr void
429 trim_left_checked( );
431 if( *first ==
',' ) {
436 if( DAW_LIKELY( first < last ) and *first ==
',' ) {
444 if constexpr( is_unchecked_input ) {
445 move_next_member_or_end_unchecked( );
447 move_next_member_or_end_checked( );
452 if constexpr( is_unchecked_input ) {
453 CommentPolicy::move_next_member_unchecked( *
this );
456 move_next_member_or_end_checked( );
461 CommentPolicy::template move_to_next_of<
'"',
'}'>( *this );
465 return parse_policy_details::in<
'"',
'}'>( *first );
469 return parse_policy_details::in<',', ']'>( *first );
473 return parse_policy_details::in<',', '}', ']'>( *first );
476 template<
char PrimLeft>
479 return CommentPolicy::template skip_bracketed_item_checked<PrimLeft>(
483 template<
char PrimLeft>
486 return CommentPolicy::template skip_bracketed_item_unchecked<PrimLeft>(
491 if constexpr( is_unchecked_input ) {
492 return skip_bracketed_item_unchecked<
'{'>( );
494 return skip_bracketed_item_checked<
'{'>( );
499 if constexpr( is_unchecked_input ) {
500 return skip_bracketed_item_unchecked<'['>( );
502 return skip_bracketed_item_checked<'['>( );
511 template<
typename Allocator>
518 template<
typename Allocator>
525 json_details::NoAllocator> {
527 using BasicParsePolicy::BasicParsePolicy;
535 template<
json_options_t PolicyFlags = json_details::default_policy_flag,
536 typename Allocator = json_details::NoAllocator>
538 daw::conditional_t<(PolicyFlags == json_details::default_policy_flag and
539 std::is_same_v<Allocator, json_details::NoAllocator>),
542 template<
typename ParsePolicy>
544 daw::conditional_t<ParsePolicy::is_default_parse_policy,
552 template<
auto... PolicyFlags>
555 json_details::are_option_flags<
decltype( PolicyFlags )...>,
556 "Only registered policy types are allowed" );
562 json_details::default_policy_flag;
566 template<
typename... Ts>
569 template<
auto... PolicyFlags>
572 template<
auto... PolicyFlags>
574 if constexpr(
decltype( details::is_policy_flag(
575 PolicyFlags... ) )::value ) {
576 static_assert(
sizeof...( PolicyFlags ) == 1 );
578 return ( PolicyFlags, ... );
588 template<
auto... PolicyFlags>
590 details::make_parse_flags<PolicyFlags...>( );
593#define DAW_JSON_CONFORMANCE_FLAGS \
594 daw::json::options::AllowEscapedNames::yes, \
595 daw::json::options::MustVerifyEndOfDataIsValid::yes, \
596 daw::json::options::IEEE754Precise::yes, \
597 daw::json::options::ExcludeSpecialEscapes::yes
600 options::parse_flags<DAW_JSON_CONFORMANCE_FLAGS>;
#define daw_json_ensure(Bool,...)
Ensure that Bool is true. If false pass rest of args to daw_json_error.
std::uint32_t json_options_t
DAW_CONSTEVAL auto make_parse_flags()
std::false_type is_policy_flag(Ts...)
constexpr auto parse_flags
Specify parse policy flags in to_json calls. See cookbook item parse_options.md.
daw::conditional_t<(PolicyFlags==json_details::default_policy_flag and std::is_same_v< Allocator, json_details::NoAllocator >), DefaultParsePolicy, BasicParsePolicy< PolicyFlags, Allocator > > GetParsePolicy
daw::conditional_t< ParsePolicy::is_default_parse_policy, DefaultParsePolicy, ParsePolicy > TryDefaultParsePolicy
constexpr auto ConformancePolicy
std::bool_constant< is_zero_terminated_string_v< T > > is_zero_terminated_string
Customization point traits.
Handles the bounds and policy items for parsing execution and comments.
DAW_ATTRIB_INLINE constexpr iterator data_end() const
constexpr BasicParsePolicy(iterator f, iterator l)
constexpr bool is_null() const
constexpr bool is_opening_bracket_checked() const
static constexpr BasicParsePolicy with_allocator(iterator f, iterator l, iterator cf, iterator cl, json_details::NoAllocator const &)
DAW_ATTRIB_INLINE constexpr BasicParsePolicy skip_bracketed_item_checked()
DAW_ATTRIB_INLINE constexpr BasicParsePolicy skip_array()
constexpr bool is_literal_end() const
DAW_ATTRIB_INLINE constexpr BasicParsePolicy skip_bracketed_item_unchecked()
DAW_ATTRIB_INLINE constexpr bool has_more() const
switch_t< json_details::get_bits_for< options::ExecModeTypes, std::size_t >(PolicyFlags), constexpr_exec_tag, runtime_exec_tag, simd_exec_tag > exec_tag_t
constexpr bool is_opening_brace_checked() const
DAW_ATTRIB_INLINE constexpr iterator begin() const
DAW_ATTRIB_INLINE constexpr std::size_t size() const
constexpr BasicParsePolicy(iterator f, iterator l, iterator cf, iterator cl, Allocator const &alloc)
DAW_ATTRIB_INLINE constexpr void remove_prefix(std::size_t n)
DAW_ATTRIB_INLINE constexpr iterator data() const
constexpr void set_class_position(class_pos_t new_pos)
constexpr class_pos_t get_class_position() const
constexpr BasicParsePolicy(iterator f, iterator l, iterator cf, iterator cl)
DAW_ATTRIB_INLINE constexpr void move_next_member()
DAW_ATTRIB_INLINE constexpr auto with_allocator(json_details::NoAllocator const &) const
DAW_ATTRIB_INLINE constexpr bool empty() const
constexpr bool is_at_next_array_element() const
constexpr auto with_allocator(BasicParsePolicy< PolicyFlags, Alloc > p) const
constexpr void trim_left_checked()
constexpr bool is_at_next_class_member() const
static constexpr BasicParsePolicy with_allocator(iterator f, iterator l, json_details::NoAllocator const &)
DAW_ATTRIB_INLINE constexpr void move_to_next_of()
constexpr BasicParsePolicy copy(iterator f=iterator{ }, iterator l=iterator{ }, iterator cf=iterator{ }, iterator cl=iterator{ }) const
DAW_ATTRIB_INLINE constexpr void remove_prefix()
constexpr void move_to_next_class_member()
constexpr void trim_left_unchecked()
constexpr void set_class_position()
DAW_ATTRIB_INLINE constexpr void move_to_next_of_unchecked()
DAW_ATTRIB_INLINE constexpr iterator end() const
DAW_ATTRIB_INLINE constexpr decltype(auto) get_allocator() const
DAW_ATTRIB_INLINE constexpr void move_next_member_or_end()
constexpr BasicParsePolicy(iterator f, iterator l, Allocator &alloc)
constexpr bool is_at_token_after_value() const
static constexpr with_allocator_type< Alloc > with_allocator(iterator f, iterator l, Alloc const &alloc)
static constexpr with_allocator_type< Alloc > with_allocator(iterator f, iterator l, iterator cf, iterator cl, Alloc const &alloc)
DAW_ATTRIB_INLINE constexpr bool is_space_unchecked() const
DAW_ATTRIB_INLINE constexpr char front() const
constexpr bool starts_with(char const (&rhs)[N]) const
BasicParsePolicy()=default
switch_t< json_details::get_bits_for< options::PolicyCommentTypes, std::size_t >(PolicyFlags), NoCommentSkippingPolicy, CppCommentSkippingPolicy, HashCommentSkippingPolicy > CommentPolicy
constexpr bool is_quotes_checked() const
constexpr void move_next_member_or_end_checked()
DAW_ATTRIB_INLINE constexpr BasicParsePolicy skip_class()
DAW_ATTRIB_INLINE constexpr void trim_left()
constexpr with_allocator_type< Alloc > with_allocator(Alloc const &alloc) const
static DAW_CONSTEVAL json_options_t policy_flags()
constexpr void move_next_member_or_end_unchecked()
DAW_ATTRIB_INLINE constexpr char front_checked() const
DAW_ATTRIB_INLINE constexpr void move_to_next_of_checked()
constexpr bool is_closing_brace_checked() const
constexpr DefaultParsePolicy(BasicParsePolicy &&other) noexcept
constexpr DefaultParsePolicy(BasicParsePolicy const &other) noexcept
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.