25#include <daw/daw_algorithm.h>
26#include <daw/daw_move.h>
27#include <daw/daw_utility.h>
31#include <daw/stdinc/tuple_traits.h>
39 template<
json_options_t PolicyFlags = json_details::default_policy_flag,
40 typename Allocator = json_details::NoAllocator>
45 std::optional<std::string_view>
name;
49 template<std::
size_t Idx, json_options_t PolicyFlags,
typename Allocator>
50 constexpr decltype( auto )
54 "Invalid index. Valid values are 0 for name, and 1 for value" );
55 if constexpr( Idx == 0 ) {
56 return parse_state.
name;
58 return parse_state.
value;
62 template<std::
size_t Idx, json_options_t PolicyFlags,
typename Allocator>
63 constexpr decltype( auto )
67 "Invalid index. Valid values are 0 for name, and 1 for value" );
68 if constexpr( Idx == 0 ) {
69 return parse_state.
name;
71 return parse_state.
value;
75 template<std::
size_t Idx, json_options_t PolicyFlags,
typename Allocator>
76 constexpr decltype( auto )
80 "Invalid index. Valid values are 0 for name, and 1 for value" );
81 if constexpr( Idx == 0 ) {
82 return std::move( parse_state.
name );
84 return std::move( parse_state.
value );
91 template<daw::json::json_options_t PolicyFlags,
typename Allocator>
92 class tuple_element<0,
daw::json::basic_json_pair<PolicyFlags, Allocator>> {
94 using type = std::optional<std::string_view>;
97 template<daw::json::json_options_t PolicyFlags,
typename Allocator>
98 class tuple_element<1,
daw::json::basic_json_pair<PolicyFlags, Allocator>> {
103 template<daw::json::json_options_t PolicyFlags,
typename Allocator>
104 inline constexpr std::size_t
105 tuple_size_v<daw::json::basic_json_pair<PolicyFlags, Allocator>> = 2;
107 template<daw::json::json_options_t PolicyFlags,
typename Allocator>
108 class tuple_size<
daw::json::basic_json_pair<PolicyFlags, Allocator>> {
110 static constexpr std::size_t value = 2;
119 template<json_options_t PolicyFlags = json_details::default_policy_flag,
120 typename Allocator = json_details::NoAllocator>
128 using pointer = json_details::arrow_proxy<value_type>;
143 : m_state( parse_state ) {}
146 : m_state(
std::data( json_doc ),
daw::data_end( json_doc ) ) {}
149 Allocator
const &alloc )
150 : m_state(
std::data( json_doc ),
daw::data_end( json_doc ),
151 std::data( json_doc ),
daw::data_end( json_doc ), alloc ) {}
155 : m_state( jv.get_raw_state( ) ) {}
159 [[nodiscard]]
constexpr std::optional<std::string_view>
name( )
const {
163 auto parse_state = m_state;
164 auto result = json_details::parse_name( parse_state );
165 return std::string_view( std::data( result ), std::size( result ) );
176 auto parse_state = m_state;
177 (void)json_details::parse_name( parse_state );
182 parse_state.get_allocator( ) );
195 m_state.get_allocator( ) ) ) };
197 auto parse_state = m_state;
198 auto name = json_details::parse_name( parse_state );
200 std::string_view( std::data( name ), std::size( name ) ),
205 parse_state.get_allocator( ) ) ) };
213 return { operator*( ) };
221 (void)json_details::parse_name( m_state );
223 (void)json_details::skip_value( m_state );
224 m_state.move_next_member_or_end( );
237 return *m_state.class_first ==
'[';
243 return *m_state.class_first ==
'{';
248 [[nodiscard]]
constexpr bool good( )
const {
249 if( m_state.is_null( ) or not m_state.has_more( ) ) {
252 switch( m_state.front( ) ) {
276 daw_json_error(
true, ErrorReason::ExpectedTokenNotFound, m_state );
282 [[nodiscard]]
constexpr explicit operator bool( )
const {
295 template<json_options_t P,
typename A>
296 [[nodiscard]]
constexpr bool
300 return m_state.first == rhs.
m_state.first;
304 return not rhs.
good( );
310 template<json_options_t P,
typename A>
311 [[nodiscard]]
constexpr bool
313 return not operator==( rhs );
317 template<json_options_t PolicyFlags,
typename Allocator>
325 template<
typename Allocator>
330 template<json_options_t PolicyFlags,
typename Allocator>
337 template<
json_options_t PolicyFlags = json_details::default_policy_flag,
338 typename Allocator = json_details::NoAllocator>
352 template<json_options_t PolicyFlags,
typename Allocator>
361 template<json_options_t PolicyFlags,
typename Allocator>
375 template<json_options_t P,
typename A>
377 : m_parse_state(
std::move( parse_state ) ) {
379 m_parse_state.trim_left( );
384 : m_parse_state(
std::data( sv ),
daw::data_end( sv ) ) {
385 m_parse_state.trim_left( );
390 : m_parse_state( first, first + static_cast<
std::ptrdiff_t>( sz ) ) {
391 m_parse_state.trim_left( );
396 : m_parse_state( first, last ) {
397 m_parse_state.trim_left( );
403 return m_parse_state;
407 return std::string_view( m_parse_state.first, m_parse_state.size( ) );
414 auto parse_state =
ParseState( m_parse_state.first,
418 m_parse_state.get_allocator( ) );
419 parse_state.remove_prefix( );
420 parse_state.trim_left( );
436 if( type( ) != JsonBaseParseTypes::Class ) {
439 bool const has_escape = name.contains(
'\\' );
442 return daw::algorithm::find_if(
443 begin( ), end( ), [name](
auto const &jp ) {
445 auto f0 = std::data( name );
446 auto const l0 = daw::data_end( name );
447 auto f1 = std::data( *jp.name );
448 auto const l1 = daw::data_end( *jp.name );
449 while( f0 != l0 and f1 != l1 ) {
460 return f0 == l0 and f1 == l1;
463 return daw::algorithm::find_if(
464 begin( ), end( ), [name](
auto const &jp ) {
466 return jp.name == name;
471 if( pos == end( ) ) {
474 return ( *pos ).value;
482 while( not json_path.empty( ) and jv ) {
484 if( json_path.front( ) ==
'[' ) {
485 return json_path.pop_front_until(
']' );
487 return json_path.pop_front_until( escaped_any_of<
'.',
'['>{ },
490 if( not json_path.empty( ) and json_path.front( ) ==
'.' ) {
491 json_path.remove_prefix( );
493 if( member.front( ) ==
'[' ) {
494 member.remove_prefix( );
497 std::data( member ), daw::data_end( member ) )
498 .with_allocator( m_parse_state.get_allocator( ) );
500 json_details::unsigned_parser<std::size_t,
501 options::JsonRangeCheck::Never,
505 if( not json_path.empty( ) and json_path.front( ) ==
'.' ) {
506 json_path.remove_prefix( );
517 template<
typename Result>
518 [[nodiscard]]
constexpr auto as( )
const {
519 using result_t = json_details::json_deduced_type<Result>;
520 auto state = m_parse_state;
521 return json_details::
522 parse_value<result_t, false, result_t::expected_type>( state );
525 template<
typename Result>
526 [[nodiscard]]
explicit operator Result( )
const {
527 return as<Result>( );
536 return find_member( json_path );
544 auto first = begin( );
545 auto const last = end( );
546 while( nsc_and( index > 0, first != last ) ) {
551 return ( *first ).value;
560 assert( type( ) == JsonBaseParseTypes::Array );
561 return find_element( index );
569 return find_element( index );
576 if( m_parse_state.empty( ) ) {
577 return JsonBaseParseTypes::None;
579 switch( m_parse_state.front( ) ) {
581 return JsonBaseParseTypes::String;
583 return JsonBaseParseTypes::Class;
585 return JsonBaseParseTypes::Array;
597 return JsonBaseParseTypes::Number;
599 if constexpr( not ParseState::is_unchecked_input ) {
600 if( m_parse_state.starts_with(
"true" ) ) {
601 return JsonBaseParseTypes::Bool;
603 return JsonBaseParseTypes::None;
605 return JsonBaseParseTypes::Bool;
608 if constexpr( not ParseState::is_unchecked_input ) {
609 if( m_parse_state.starts_with(
"false" ) ) {
610 return JsonBaseParseTypes::Bool;
612 return JsonBaseParseTypes::None;
614 return JsonBaseParseTypes::Bool;
618 ErrorReason::InvalidNull,
620 return JsonBaseParseTypes::Null;
622 return JsonBaseParseTypes::None;
628 auto parse_state = m_parse_state;
629 auto result = json_details::skip_value( parse_state );
641 auto parse_state = m_parse_state;
642 auto result = json_details::skip_value( parse_state );
643 return { std::data( result ), std::size( result ) };
649 template<
typename Alloc = std::allocator<
char>,
650 typename Traits = std::
char_traits<
char>>
651 [[nodiscard]] std::basic_string<char, Traits, Alloc>
653 auto parse_state = m_parse_state;
654 auto result = json_details::skip_value( parse_state );
655 return { std::data( result ), std::size( result ), alloc };
660 [[nodiscard]]
constexpr bool is_null( )
const {
661 return type( ) == JsonBaseParseTypes::Null;
667 return type( ) == JsonBaseParseTypes::Class;
673 return type( ) == JsonBaseParseTypes::Array;
679 return type( ) == JsonBaseParseTypes::Number;
685 return type( ) == JsonBaseParseTypes::String;
690 [[nodiscard]]
constexpr bool is_bool( )
const {
691 return type( ) == JsonBaseParseTypes::Bool;
699 return type( ) == JsonBaseParseTypes::None;
703 template<json_options_t P,
typename A>
704 [[nodiscard]]
constexpr
708 new_range.class_first = m_parse_state.class_first;
709 new_range.class_last = m_parse_state.class_last;
714 [[nodiscard]]
explicit constexpr operator bool( )
const {
715 return type( ) != JsonBaseParseTypes::None;
719 template<json_options_t PolicyFlags,
typename Allocator>
730 template<
typename Result, json_options_t PolicyFlags,
typename Allocator>
731 [[nodiscard]]
constexpr Result
733 return jv.template as<Result>( );
736 namespace json_details {
739 inline constexpr bool is_json_value =
false;
741 template<json_options_t PolicyFlags,
typename Allocator>
742 inline constexpr bool
743 is_json_value<basic_json_value<PolicyFlags, Allocator>> =
true;
745 template<json_options_t PolicyFlags,
typename Allocator>
746 inline constexpr bool
747 is_string_view_like_v<basic_json_value<PolicyFlags, Allocator>> =
false;
std::optional< std::string_view > type
#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.
DAW_ATTRIB_NOINLINE void daw_json_error(bool b, ErrorReason reason)
JsonBaseParseTypes
The fundamental JSON types.
std::uint32_t json_options_t
daw::conditional_t< ParsePolicy::is_default_parse_policy, DefaultParsePolicy, ParsePolicy > TryDefaultParsePolicy
constexpr Result as(basic_json_value< PolicyFlags, Allocator > const &jv)
constexpr decltype(auto) get(basic_json_pair< PolicyFlags, Allocator > const &parse_state)
Customization point traits.
Handles the bounds and policy items for parsing execution and comments.
A name/value pair of string_view/json_value.
basic_json_value< PolicyFlags, Allocator > value
TryDefaultParsePolicy< BasicParsePolicy< PolicyFlags, Allocator > > ParseState
std::optional< std::string_view > name
a rudimentary range object for holding basic_json_value_iterator
constexpr iterator begin()
Iterator for iterating over arbitrary JSON members and array elements.
constexpr pointer operator->()
Return an arrow_proxy object containing the result of operator* Should not use this method unless you...
constexpr void operator++(int) &
Move parser to next value.
constexpr std::optional< std::string_view > name() const
Name of member.
constexpr basic_json_value_iterator(parse_policy const &parse_state)
constexpr parse_policy const & get_raw_state() const
Get access to the internal state. Should not be used as part of public API.
TryDefaultParsePolicy< BasicParsePolicy< PolicyFlags, Allocator > > parse_policy
basic_json_value_iterator()=default
basic_json_value_iterator(daw::string_view json_doc)
constexpr basic_json_value< PolicyFlags, Allocator > value() const
Get the value currently being referenced.
std::forward_iterator_tag iterator_category
constexpr basic_json_value_iterator & operator++()
Move the parser to the next value.
std::string_view key_type
std::ptrdiff_t difference_type
constexpr bool is_class() const
Is the value this iterator iterates over an class.
constexpr bool is_array() const
Is the value this iterator iterates over an array.
constexpr bool good() const
Can we increment more.
json_details::arrow_proxy< value_type > pointer
constexpr bool operator==(basic_json_value_iterator< P, A > const &rhs) const
Check for equivalence with rhs iterator.
basic_json_value_iterator(daw::string_view json_doc, Allocator const &alloc)
basic_json_value_iterator(basic_json_value< PolicyFlags, Allocator > const &jv)
constexpr basic_json_pair< PolicyFlags, Allocator > operator*()
Get the name/value pair of the currently referenced element.
constexpr bool operator!=(basic_json_value_iterator< P, A > const &rhs) const
Check if rhs is not equivalent to self.
A non-owning container for arbitrary JSON values that allows movement/iteration through.
constexpr bool is_null() const
Is the JSON value a null literal.
constexpr bool is_class() const
Is the JSON value a class.
constexpr std::string_view get_string_view() const
Construct a string range of the current value. Strings start inside the quotes.
constexpr std::string_view get_raw_json_document() const
constexpr bool is_bool() const
Is the JSON value a boolean.
constexpr iterator end() const
End of range over class/arrays members/items.
constexpr basic_json_value(char const *first, std::size_t sz)
Construct from char const *, std::size_t.
std::ptrdiff_t difference_type
constexpr basic_json_value(char const *first, char const *last)
Construct from char const *, char const *.
constexpr basic_json_value operator[](daw::string_view json_path) const
Query the current class for a named member.
TryDefaultParsePolicy< BasicParsePolicy< PolicyFlags, Allocator > > ParseState
constexpr ParseState get_state() const
Construct a string range of the current value.
constexpr basic_json_value(daw::string_view sv)
Construct from string_view.
constexpr auto as() const
Parse the current json member as a Result. The Result type must be supported or mapped via a json_dat...
constexpr basic_json_value find_array_element(std::size_t index) const
Find the nth element of the current json array.
constexpr iterator begin() const
Get the first member/item.
constexpr basic_json_value find_element(std::size_t index) const
Find the nth element/submember of the current json array or class.
constexpr basic_json_value find_member(daw::string_view json_path) const
find a class member/array element as specified by the json_path
constexpr bool is_array() const
Is the JSON value a array.
constexpr bool is_string() const
Is the JSON value a string.
constexpr bool is_number() const
Is the JSON value a number literal.
constexpr ParseState get_raw_state() const
Get a copy of the underlying parse state.
std::basic_string< char, Traits, Alloc > get_string(Alloc const &alloc=Alloc()) const
Construct a string range of the current value. Strings start inside the quotes.
basic_json_value()=default
constexpr basic_json_value(BasicParsePolicy< P, A > parse_state)
Construct from IteratorRange.
constexpr bool is_unknown() const
Is the JSON data unrecognizable. JSON members will start with one of ",[,{,0,1,2,3,...
constexpr JsonBaseParseTypes type() const
Get the type of JSON value.
constexpr basic_json_value operator[](std::size_t index) const
Find the nth element/submember of the current json array or class.
constexpr basic_json_value find_class_member(daw::string_view name) const
Query the current class for a named member.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.