19#include <initializer_list>
33inline std::string_view string_from_token(std::string_view token, std::false_type is_escaped
JSONV_UNUSED)
35 return std::string_view(token.data() + 1, token.size() - 2U);
39std::string string_from_token(std::string_view token, std::true_type is_escaped);
141 expected_key_delimiter,
174 template <
typename TSelf, ast_node_type KIndexToken>
189 return std::string_view(_token_begin,
static_cast<const TSelf&
>(*this).token_size());
197 explicit constexpr base(
const char* token_begin) :
198 _token_begin(token_begin)
202 const char* _token_begin;
205 template <
typename TSelf, ast_node_type KIndexToken, std::
size_t KTokenSize>
214 constexpr std::size_t token_size()
const
220 template <
typename TSelf, ast_node_type KIndexToken>
226 _token_size(token_size)
230 constexpr std::size_t token_size()
const
236 std::size_t _token_size;
266 return _element_count;
270 std::size_t _element_count;
293 return _element_count;
297 std::size_t _element_count;
307 template <
typename TSelf, ast_node_type KIndexToken,
bool KEscaped>
313 using value_type = std::conditional_t<KEscaped, std::string, std::string_view>;
318 _token_size(token_size)
339 constexpr std::size_t token_size()
const
347 return detail::string_from_token(this->
token_raw(), std::integral_constant<bool, KEscaped>());
351 std::size_t _token_size;
384 bool value()
const noexcept
396 bool value()
const noexcept
431 double value()
const;
439 _error_code(error_code)
475 template <
typename T,
typename... TArgs>
476 explicit ast_node(std::in_place_type_t<T>
type, TArgs&&... args) :
477 _impl(
type,
std::forward<TArgs>(args)...)
490 template <
typename FVisitor>
493 return std::visit(std::forward<FVisitor>(
visitor), _impl);
497 template <
typename FVisitor>
500 return std::visit(std::forward<FVisitor>(
visitor),
as_key());
507 return visit([](
const auto&
x) {
return x.
type(); });
538 template <
typename T>
542 return std::get<T>(_impl);
549 std::variant<key_canonical, key_escaped>
as_key()
const
551 if (
type() == ast_node_type::key_canonical)
563template <
typename TSelf, ast_node_type KIndexToken>
566 return ast_node(ast_node::storage_type(
static_cast<const TSelf&
>(*
this)));
The beginning of an kind::array ([).
constexpr std::size_t element_count() const
Get the number of elements in the array this token starts. This is useful for reserving memory.
The end of an kind::array (]).
static constexpr ast_node_type type()
Get the ast_node_type type.
std::string_view token_raw() const
constexpr bool escaped() const noexcept
Did the source JSON for this string contain escape sequences? If this is true, JSON escape sequences ...
constexpr bool canonical() const noexcept
Was the source JSON for this string encoded in the canonical UTF-8 representation?...
std::conditional_t< KEscaped, std::string, std::string_view > value_type
The return type of value is based on if the string is canonical or escaped.
std::int64_t value() const
Get the literal as an std::int64_t.
The beginning of an kind::object ({).
constexpr std::size_t element_count() const
Get the number of elements in the object this token starts. This is useful for reserving memory.
The end of an kind::object (}).
Represents an entry in a JSON AST.
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
Check that this node has the given type or is one of the expected types.
ast_node_type type() const
Get the ast_node_type that tells the underlying type of this instance.
auto visit_key(FVisitor &&visitor) const
Convenience function for calling std::visit on a key (see as_key).
auto visit(FVisitor &&visitor) const
Convenience function for calling std::visit on the underlying storage of this node.
std::string_view token_raw() const
Get a view of the raw token.
JSONV_PUBLIC std::expected< void, ast_node_type > expect(std::initializer_list< ast_node_type > types) const
Check that this node has the given type or is one of the expected types.
const storage_type & storage() const
Get the std::variant that backs this type.
const T & as() const
Get the underlying data of this node as T.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_UNUSED
Note that you know the variable is unused, but make the compiler stop complaining about it.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
ast_error
Error code encountered while building the AST.
JSONV_PUBLIC std::string to_string(const ast_node_type &type)
Get what operator<< writes for type as a std::string.
ast_node_type
Marker type for an encountered token type.
@ close_after_comma
A ] or } arrived where the structure still owed a value – after a , in either kind of structure,...
@ expected_comma
A value followed another inside an array or object with no , between them, as in [1 2].
Copyright (c) 2019-2020 by Travis Gockel.