JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
jsonv::ast_node Class Referencefinal

Represents an entry in a JSON AST. More...

#include <jsonv/ast.hpp>

Classes

class  array_begin
 The beginning of an kind::array ([). More...
 
class  array_end
 The end of an kind::array (]). More...
 
class  base
 
class  basic_dynamic_size_token
 
class  basic_fixed_size_token
 
class  basic_string_token
 
class  decimal
 
class  document_end
 The end of a document. More...
 
class  document_start
 The start of a document. More...
 
class  error
 
class  integer
 
class  key_canonical
 
class  key_escaped
 
class  literal_false
 
class  literal_null
 
class  literal_true
 
class  object_begin
 The beginning of an kind::object ({). More...
 
class  object_end
 The end of an kind::object (}). More...
 
class  string_canonical
 
class  string_escaped
 

Public Types

using storage_type = std::variant< document_end, document_start, object_begin, object_end, array_begin, array_end, string_canonical, string_escaped, key_canonical, key_escaped, literal_true, literal_false, literal_null, integer, decimal, error >
 

Public Member Functions

 ast_node (const storage_type &value)
 
template<typename T , typename... TArgs>
 ast_node (std::in_place_type_t< T > type, TArgs &&... args)
 
const storage_type & storage () const
 Get the std::variant that backs this type.
 
template<typename FVisitor >
auto visit (FVisitor &&visitor) const
 Convenience function for calling std::visit on the underlying storage of this node.
 
template<typename FVisitor >
auto visit_key (FVisitor &&visitor) const
 Convenience function for calling std::visit on a key (see as_key).
 
ast_node_type type () const
 Get the ast_node_type that tells the underlying type of this instance.
 
std::string_view token_raw () const
 Get a view of the raw token.
 
template<typename T >
const T & as () const
 Get the underlying data of this node as T.
 
std::variant< key_canonical, key_escaped > as_key () const
 Get the underlying data of this node as one of the key types: key_canonical or key_escaped.
 
JSONV_PUBLIC std::expected< void, ast_node_type > expect (ast_node_type type) const
 
JSONV_PUBLIC std::expected< void, ast_node_type > expect (std::initializer_list< ast_node_type > types) const
 

Detailed Description

Represents an entry in a JSON AST.

See also
parse_index

Definition at line 164 of file ast.hpp.

Member Typedef Documentation

◆ storage_type

Constructor & Destructor Documentation

◆ ast_node() [1/2]

jsonv::ast_node::ast_node ( const storage_type &  value)
inline

Definition at line 459 of file ast.hpp.

◆ ast_node() [2/2]

template<typename T , typename... TArgs>
jsonv::ast_node::ast_node ( std::in_place_type_t< T >  type,
TArgs &&...  args 
)
inlineexplicit

Definition at line 464 of file ast.hpp.

Member Function Documentation

◆ as()

template<typename T >
const T & jsonv::ast_node::as ( ) const
inline

Get the underlying data of this node as T.

Exceptions
std::bad_variant_accessif the requested T is different from the type of this instance.

Definition at line 527 of file ast.hpp.

◆ as_key()

std::variant< key_canonical, key_escaped > jsonv::ast_node::as_key ( ) const
inline

Get the underlying data of this node as one of the key types: key_canonical or key_escaped.

Exceptions
std::bad_variant_accessif the type of this instance is neither key_canonical nor key_escaped.

Definition at line 536 of file ast.hpp.

+ Here is the call graph for this function:

◆ expect()

JSONV_PUBLIC std::expected< void, ast_node_type > jsonv::ast_node::expect ( ast_node_type  type) const

Check that this node has the given type or is one of the expected types.

A mismatch is returned rather than thrown, since which node types are acceptable is a question about the JSON source and not about the correctness of the program asking. The caller decides whether it is an error.

Returns
Nothing if this instance has type or one of the given types; otherwise the ast_node_type this instance actually has.
Exceptions
std::invalid_argumentif types is empty. Unlike a type mismatch, expecting nothing at all is a mistake in the calling code.

◆ storage()

const storage_type & jsonv::ast_node::storage ( ) const
inline

Get the std::variant that backs this type.

See also
visit

Definition at line 472 of file ast.hpp.

◆ token_raw()

std::string_view jsonv::ast_node::token_raw ( ) const
inline

Get a view of the raw token.

For example, "true", "{", or "1234". Note that this includes the complete source, so string types such as ast_node_type::string_canonical include the opening and closing quotations.

Definition at line 517 of file ast.hpp.

+ Here is the call graph for this function:

◆ type()

ast_node_type jsonv::ast_node::type ( ) const
inline

Get the ast_node_type that tells the underlying type of this instance.

Definition at line 493 of file ast.hpp.

+ Here is the call graph for this function:

◆ visit()

template<typename FVisitor >
auto jsonv::ast_node::visit ( FVisitor &&  visitor) const
inline

Convenience function for calling std::visit on the underlying storage of this node.

Definition at line 479 of file ast.hpp.

◆ visit_key()

template<typename FVisitor >
auto jsonv::ast_node::visit_key ( FVisitor &&  visitor) const
inline

Convenience function for calling std::visit on a key (see as_key).

Definition at line 486 of file ast.hpp.

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: