19 #include <daw/daw_move.h>
20 #include <daw/daw_string_view.h>
21 #include <daw/daw_uint_buffer.h>
24 #include <string_view>
30 namespace json_details {
32 typename Allocator = NoAllocator>
33 class basic_stateful_json_value_state {
38 daw::string_view name;
39 daw::UInt32 hash_value;
42 explicit constexpr basic_stateful_json_value_state(
43 daw::string_view Name,
47 daw::name_hash<ParseState::expect_long_strings>( Name ) )
48 , location( std::move( val ) ) {}
50 [[nodiscard]] constexpr
bool is_match( daw::string_view Name )
const {
54 [[nodiscard]] constexpr
bool is_match( daw::string_view Name,
55 daw::UInt32 hash )
const {
56 if( hash != hash_value ) {
69 : name(
std::data( Name ),
std::size( Name ) )
78 template<
json_options_t PolicyFlags = json_details::default_policy_flag,
79 typename Allocator = json_details::NoAllocator>
86 json_details::basic_stateful_json_value_state<PolicyFlags, Allocator>>
96 std::size_t
const Sz = std::size( m_locs );
97 for( ; pos < Sz; ++pos ) {
104 if( m_locs.empty( ) ) {
105 return m_value.
begin( );
107 auto res = m_locs.back( ).location;
111 auto const last = m_value.
end( );
112 while( it != last ) {
113 auto name = it.
name( );
115 auto const &new_loc = m_locs.emplace_back(
116 daw::string_view( std::data( *name ), std::size( *name ) ), it );
117 if( new_loc.is_match( member.
name ) ) {
123 return std::size( m_locs );
131 [[nodiscard]] constexpr std::size_t move_to( std::size_t index ) {
132 if( index < std::size( m_locs ) ) {
136 if( m_locs.empty( ) ) {
137 return m_value.
begin( );
139 auto res = m_locs.back( ).location;
143 auto last = m_value.
end( );
144 std::size_t pos = std::size( m_locs );
145 while( it != last ) {
146 auto name = it.name( );
149 daw::string_view( std::data( *name ), std::size( *name ) ), it );
151 m_locs.emplace_back( daw::string_view( ), it );
159 return std::size( m_locs );
165 : m_value(
std::move( val ) ) {
168 auto t = m_value.
type( );
169 return ( t == JsonBaseParseTypes::Class ) |
170 ( t == JsonBaseParseTypes::Array );
172 ErrorReason::ExpectedArrayOrClassStart,
188 m_value = std::move( val );
201 ErrorReason::UnknownMember );
202 return m_locs[pos].location->value;
212 std::size_t pos = move_to( member );
214 ErrorReason::UnknownMember );
215 return m_locs[pos].location->value;
224 at( std::string_view key ) {
225 auto const k = std::string_view( std::data( key ), std::size( key ) );
226 std::size_t pos = move_to( k );
227 if( pos < std::size( m_locs ) ) {
228 return m_locs[pos].location->value;
238 [[nodiscard]] std::size_t
size( ) {
240 switch( current_type ) {
241 case JsonBaseParseTypes::Array:
242 case JsonBaseParseTypes::Class:
243 return move_to( ( daw::numeric_limits<std::size_t>::max )( ) );
244 case JsonBaseParseTypes::Number:
245 case JsonBaseParseTypes::Bool:
246 case JsonBaseParseTypes::String:
247 case JsonBaseParseTypes::Null:
248 case JsonBaseParseTypes::None:
260 [[nodiscard]] std::size_t
index_of( std::string_view key ) {
270 [[nodiscard]] constexpr
bool contains( std::string_view key ) {
271 auto const k = std::string_view( std::data( key ), std::size( key ) );
272 return move_to( k ) < std::size( m_locs );
281 [[nodiscard]] constexpr
bool contains( std::size_t index ) {
282 return move_to( index ) < size( m_locs );
292 template<
typename Integer>
293 [[nodiscard]] std::optional<std::string_view>
name_of( Integer index ) {
294 static_assert( std::is_integral_v<Integer>,
295 "Only integer indices are allowed" );
296 if constexpr( std::is_signed_v<Integer> ) {
300 if(
static_cast<std::size_t
>( index ) >= sz ) {
303 sz -=
static_cast<std::size_t
>( index );
304 return std::string_view( std::data( m_locs[sz].name( ) ),
305 std::size( m_locs[sz].name( ) ) );
308 std::size_t pos = move_to(
static_cast<std::size_t
>( index ) );
309 if( pos < std::size( m_locs ) ) {
310 return std::string_view( std::data( m_locs[pos].name( ) ),
311 std::size( m_locs[pos].name( ) ) );
329 if constexpr( std::is_signed_v<Integer> ) {
334 ErrorReason::UnknownMember );
335 sz -=
static_cast<std::size_t
>( index );
336 return m_locs[sz].location->value;
339 std::size_t pos = move_to(
static_cast<std::size_t
>( index ) );
341 ErrorReason::UnknownMember );
342 return m_locs[pos].location->value;
355 [[nodiscard]] constexpr std::optional<
357 if constexpr( std::is_signed_v<Integer> ) {
361 if(
static_cast<std::size_t
>( index ) >= sz ) {
364 sz -=
static_cast<std::size_t
>( index );
365 return m_locs[sz].location->value( );
368 std::size_t pos = move_to(
static_cast<std::size_t
>( index ) );
369 if( pos < std::size( m_locs ) ) {
370 return m_locs[pos].location->value( );
386 template<json_options_t PolicyFlags,
typename Allocator>
constexpr basic_json_value< PolicyFlags, Allocator > operator[](std::string_view key)
Create a basic_json_member for the named member.
std::size_t index_of(std::string_view key)
Return the index of named member. This method is O(N) worst case and O(1) if the locations have alrea...
constexpr basic_json_value< PolicyFlags, Allocator > at(std::string_view key)
Create a basic_json_member for the named member.
constexpr basic_json_value< PolicyFlags, Allocator > operator[](json_member_name member)
Create a basic_json_member for the named member.
std::optional< std::string_view > name_of(Integer index)
basic_json_value< PolicyFlags, Allocator > m_value
constexpr std::optional< basic_json_value< PolicyFlags, Allocator > > at(Integer index)
std::size_t size()
Count the number of elements/members in the JSON class or array This method is O(N) worst case and O(...
constexpr basic_json_value< PolicyFlags, Allocator > get_json_value() const
TryDefaultParsePolicy< BasicParsePolicy< PolicyFlags, Allocator > > ParseState
constexpr basic_json_value< PolicyFlags, Allocator > operator[](Integer index)
#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_ENABLEIF(...)
JsonBaseParseTypes
The fundamental JSON types.
std::uint32_t json_options_t
daw::conditional_t< ParsePolicy::is_default_parse_policy, DefaultParsePolicy, ParsePolicy > TryDefaultParsePolicy
basic_stateful_json_value() -> basic_stateful_json_value<>
Customization point traits.
DAW_JSON_REQUIRES(boost::describe::has_describe_members< T >::value and use_boost_describe_v< T >) struct json_data_contract< T >
static constexpr DAW_ATTRIB_INLINE UInt32 name_hash(daw::string_view key)
Iterator for iterating over arbitrary JSON members and array elements.
constexpr std::optional< std::string_view > name() const
Name of member.
A non-owning container for arbitrary JSON values that allows movement/iteration through.
constexpr iterator end() const
End of range over class/arrays members/items.
constexpr iterator begin() const
Get the first member/item.
constexpr ParseState get_raw_state() const
Get a copy of the underlying parse state.
constexpr JsonBaseParseTypes type() const
Get the type of JSON value.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.