Skip to content

Commit

Permalink
Creates reader type for converting from JSON source to AST node seq…
Browse files Browse the repository at this point in the history
…uence

A `jsonv::reader` instance reads from some form of JSON source (probably
a string, but the `jsonv::ast_index` type can theoretically read from
anything JSON-like) and converts it into `jsonv::ast_node`s. These
abstract reading from JSON source to normalize `extract` implementations,
which is needed for #150.

- Resolves #171
  • Loading branch information
tgockel committed Apr 23, 2022
1 parent 4a4aac0 commit 7567f7a
Show file tree
Hide file tree
Showing 18 changed files with 1,382 additions and 61 deletions.
1 change: 1 addition & 0 deletions include/jsonv/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ namespace jsonv
#include "parse.hpp"
#include "parse_index.hpp"
#include "path.hpp"
#include "reader.hpp"
#include "result.hpp"
#include "serialization.hpp"
#include "serialization_builder.hpp"
Expand Down
25 changes: 22 additions & 3 deletions include/jsonv/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ enum class ast_error : std::uint64_t
expected_key_delimiter,
unexpected_token,
unexpected_comma,
eof,
unexpected_eof,
expected_eof,
depth_exceeded,
extra_close,
Expand Down Expand Up @@ -164,7 +164,7 @@ class ast_node final
{
public:
/// Get the \c ast_node_type type.
constexpr ast_node_type type() const
static constexpr ast_node_type type()
{
return KIndexToken;
}
Expand Down Expand Up @@ -430,7 +430,8 @@ class ast_node final
literal_null,
integer,
decimal,
error>;
error
>;

public:
ast_node(const storage_type& value) :
Expand All @@ -457,6 +458,13 @@ class ast_node final
return std::visit(std::forward<FVisitor>(visitor), _impl);
}

/// Convenience function for calling \c std::visit on a key (see \c as_key).
template <typename FVisitor>
auto visit_key(FVisitor&& visitor) const
{
return std::visit(std::forward<FVisitor>(visitor), as_key());
}

/// Get the \c ast_node_type that tells the underlying type of this instance.
ast_node_type type() const
{
Expand All @@ -479,6 +487,17 @@ class ast_node final
return std::get<T>(_impl);
}

/// Get the underlying data of this node as one of the key types: \c key_canonical or \c key_escaped.
///
/// \throw std::bad_variant_access if the \c type of this instance is neither \c key_canonical nor \c key_escaped.
std::variant<key_canonical, key_escaped> as_key() const
{
if (type() == ast_node_type::key_canonical)
return as<key_canonical>();
else
return as<key_escaped>();
}

private:
storage_type _impl;
};
Expand Down
41 changes: 17 additions & 24 deletions include/jsonv/detail/scope_exit.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
/** \file jsonv/detail/scope_exit.hpp
* Definition of the \c on_scope_exit utility.
*
* Copyright (c) 2015 by Travis Gockel. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
* as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* \author Travis Gockel ([email protected])
**/
#ifndef __JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__
#define __JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__
/// \file jsonv/detail/scope_exit.hpp
/// Definition of the \c on_scope_exit utility.
///
/// Copyright (c) 2015 by Travis Gockel. All rights reserved.
///
/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
/// version.
///
/// \author Travis Gockel ([email protected])
#pragma once

#include <jsonv/config.hpp>

#include <utility>

namespace jsonv
{
namespace detail
namespace jsonv::detail
{

template <typename Function>
Expand All @@ -29,29 +25,29 @@ class scope_exit_invoker
_func(std::move(func)),
_responsible(true)
{ }

scope_exit_invoker(scope_exit_invoker&& src) :
_func(std::move(src._func)),
_responsible(src._responsible)
{
src._responsible = false;
}

scope_exit_invoker(const scope_exit_invoker&) = delete;
scope_exit_invoker& operator=(const scope_exit_invoker&) = delete;
scope_exit_invoker& operator=(scope_exit_invoker&&) = delete;

~scope_exit_invoker()
{
if (_responsible)
_func();
}

void release()
{
_responsible = false;
}

private:
Function _func;
bool _responsible;
Expand All @@ -64,6 +60,3 @@ scope_exit_invoker<Function> on_scope_exit(Function func)
}

}
}

#endif/*__JSONV_DETAIL_SCOPE_EXIT_HPP_INCLUDED__*/
1 change: 1 addition & 0 deletions include/jsonv/forward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class path;
class path_element;
enum class path_element_kind : unsigned char;
template <typename TPointer> class polymorphic_adapter_builder;
class reader;
template <typename TValue, typename TError> class result;
class serializer;
class serialization_context;
Expand Down
Loading

0 comments on commit 7567f7a

Please sign in to comment.