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

A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON ast_node sequence. More...

#include <jsonv/reader.hpp>

Public Member Functions

 reader (parse_index index)
 Create a reader which reads from the given index.
 
 reader (const value *value)
 Create a reader which reads from an in-memory value.
 
 reader (const reader &)=delete
 
readeroperator= (const reader &)=delete
 
 reader (reader &&) noexcept=default
 
readeroperator= (reader &&) noexcept=default
 
bool good () const
 Check if this reader is still good to read from.
 
const ast_nodecurrent () const
 Get the current AST node this reader is pointing at.
 
template<typename TAstNode >
TAstNode current_as () const
 Get the current AST node as a specific TAstNode subtype, calling expect beforehand.
 
const pathcurrent_path () const
 Get the path to the current node this reader is pointing at.
 
bool next_token () noexcept
 Go to the next token.
 
bool next_structure () noexcept
 Go to one past the end of the current structure.
 
bool next_key () noexcept
 Go to the next object key or end-of-object.
 
 reader (string_view source)
 
 reader (string_view source, const parse_options &parse_options)
 
 reader (const char *source)
 
 reader (const char *source, const parse_options &parse_options)
 
 reader (std::string &&source)
 
 reader (std::string &&source, const parse_options &parse_options)
 
void expect (ast_node_type type)
 
void expect (std::initializer_list< ast_node_type > types)
 

Detailed Description

A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON ast_node sequence.

Readers normalize access to JSON source for conversion to some other format. They can be provided with pre-parsed JSON through a parse_index or value. They can be provided with a std::string or string_view directly. This allows extractor implementations to operate on all forms of JSON without worrying about the implementation.

Definition at line 39 of file reader.hpp.

Constructor & Destructor Documentation

◆ reader() [1/2]

jsonv::reader::reader ( const value value)
explicit

Create a reader which reads from an in-memory value.

Parameters
valueThe value to read from. This must remain valid for the lifetime of the reader.

◆ reader() [2/2]

jsonv::reader::reader ( string_view  source)
explicit

Create a reader which reads from JSON source.

Parameters
sourceThe JSON source code to parse from. This must stay in memory for the duration of this instance's use. If source is an rvalue reference to a std::string instance, the source is moved to the reader's implementation to keep alive.
parse_optionsIf specified, use these options to parse source. If unspecified, use the default values of parse_options for parse_index::parse.

Member Function Documentation

◆ current()

const ast_node & jsonv::reader::current ( ) const

Get the current AST node this reader is pointing at.

Exceptions
std::invalid_argumentif this instance is not good.

◆ current_as()

template<typename TAstNode >
TAstNode jsonv::reader::current_as ( ) const
inline

Get the current AST node as a specific TAstNode subtype, calling expect beforehand.

Exceptions
std::invalid_argumentif this instance is not good.
extraction_errorif the current node does not match TAstNode::type().

Definition at line 100 of file reader.hpp.

◆ current_path()

const path & jsonv::reader::current_path ( ) const

Get the path to the current node this reader is pointing at.

This is used in the generation of error messages to describe the location of something that could not be extracted.

^ /* "." -- start of document is the empty path */
{ /* "." -- opening { is still an empty path */
"a": /* ".a" -- the key starts the path */
[ /* ".a" -- the path refers to the entire array */
1, /* ".a[0]" */
2, /* ".a[1]" */
3, /* ".a[2]" */
], /* ".a" -- the path at the end of the array refers to the entire array again */
"b": /* ".b" */
{ /* ".b" -- the path refers to the entire object */
"x": /* ".b.x" */
"taco" /* ".b.x" */
}, /* ".b" */
"c": /* ".c" */
4 /* ".c" */
} /* "." */
$ /* "." */
Exceptions
std::invalid_argumentif this instance is not good.

◆ expect()

void jsonv::reader::expect ( ast_node_type  type)

Check that the current AST node has the given type or is one of the expected types. Using this leads to a slightly more informative error message than current.as<T>() call.

Exceptions
extraction_errorif the current node does not match the expected type or types.
std::invalid_argumentif this instance is not good.

◆ good()

bool jsonv::reader::good ( ) const

Check if this reader is still good to read from.

This will be true if this instance has not been moved-from and has not reached EOF. If this is false, current or current_path will throw an exception.

◆ next_key()

bool jsonv::reader::next_key ( )
noexcept

Go to the next object key or end-of-object.

^
{
"a": /* <- calling here goes to "b" */
[ /* <- calling when not on an object key throws */
1,
2,
3,
],
"b": /* <- calling here goes to "c" */
{
},
"c": /* <- calling here goes to end of object */
4
}
$
Returns
true if the reader is still good to read from current.
Exceptions
std::invalid_argumentif the reader is not currently at the start of a key.

◆ next_structure()

bool jsonv::reader::next_structure ( )
noexcept

Go to one past the end of the current structure.

^
{
"a": /* <- go to end of document */
[ /* <- go to "b" */
1, /* <- go to "b", too */
2,
3,
], /* <- go to "b" */
"b": /* <- go to end of document */
{ /* <- go to "c" */
}, /* <- go to "c" */
"c":
4
} /* <- go to end of document */
$

This is meant to be used when parsing an object or array

{
from.expect(jsonv::ast_node_type::object_begin);
from.next_token();
// Use this helper macro to always execute code
JSONV_SCOPE_EXIT { from.next_structure(); };
std::optional<std::int64_t> a;
while (from.good())
{
auto token = from.current();
if (token.type() == jsonv::ast_node_type::object_end)
{
return my_object(a.value_or(0));
}
else if (token.type() == jsonv::ast_node_type::key_canonical)
{
if (token.value() == "a")
{
reader.next_token();
a = reader.current_as<jsonv::ast_node::integer>().value();
reader.next_token();
}
else
{
// ignore
reader.next_key();
}
}
else
{
throw jsonv::extraction_error(from.current_path(), "Did not handle escaped keys");
}
}
}
An adapter for enumeration types.
Exception thrown if there is any problem running extract.
A reader instance reads from some form of JSON source (probably a string) and converts it into a JSON...
Definition reader.hpp:40
reader(parse_index index)
Create a reader which reads from the given index.
const path & current_path() const
Get the path to the current node this reader is pointing at.
Returns
true if the reader is still good to read from current.

◆ next_token()

bool jsonv::reader::next_token ( )
noexcept

Go to the next token.

^
{ /* <- go to "a" */
"a": /* <- go to [ */
[ /* <- go to 1 */
1, /* <- go to 2 */
2, /* ...and so on */
3,
],
"b":
{
},
"c":
4
}
$
Returns
true if the reader is still good to read from current.

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