| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- #pragma once
- #include <cstdint>
- #include <optional>
- #include <jvalidate/detail/array_iterator.h>
- #include <jvalidate/detail/number.h>
- #include <jvalidate/detail/object_iterator.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/forward.h>
- #include <jvalidate/status.h>
- namespace jvalidate::adapter {
- /**
- * @brief An interface for a type-erased reference-wrapper around a JSON node.
- *
- * Unlike languages like python, there are dozens of different C++ Libraries
- * for JSON parsing/construction. Each of these libraries has its own set of
- * getter functions, rules for handling missing values, and degree to which it
- * can engage in fuzziness of types.
- *
- * Adapter has two main groups of methods:
- * - as_*() and *_size() virtual functions
- * - maybe_*() concrete functions
- *
- * Most interaction with Adapter will be done via the maybe_*() functions,
- * with or without strictness enabled depending on what constraint is being
- * checked.
- */
- class Adapter {
- public:
- virtual ~Adapter() = default;
- /**
- * @brief Get the jvalidate::adapter::Type that this adapter represents.
- * This represents the types recognized by json-schema:
- * null, bool, integer, number, string, array, object
- * This function is meant to be used internally - and not by any of the
- * Constraint objects.
- */
- virtual Type type() const = 0;
- /**
- * @brief Obtain an immutable copy of the current node.
- * Because an Adapter stores a reference to the underlying JSON, it cannot
- * be stored by e.g. a Const/Enum Constraint without risking a Segfault.
- */
- virtual std::unique_ptr<Const const> freeze() const = 0;
- /**
- * @brief Extract a boolean value from this JSON node.
- * @pre type() == Type::Boolean
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual bool as_boolean() const = 0;
- /**
- * @brief Extract an integer value from this JSON node.
- * @pre type() == Type::Integer
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual int64_t as_integer() const = 0;
- /**
- * @brief Extract a decimal value from this JSON node.
- * @pre type() == Type::Number
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual double as_number() const = 0;
- /**
- * @brief Extract a string value from this JSON node.
- * @pre type() == Type::String
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual std::string as_string() const = 0;
- /**
- * @brief Get the size of the JSON array in this node.
- * @pre type() == Type::Array
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual size_t array_size() const = 0;
- /**
- * @brief Get the size of the JSON object in this node.
- * @pre type() == Type::Object
- *
- * @throws If the pre-condition is not valid, then this function may throw
- * or produce other undefined behavior, depending on the implementation
- * details of the underlying type.
- */
- virtual size_t object_size() const = 0;
- /**
- * @brief Loop through every element of the JSON array in this node, applying
- * the given callback function to them.
- *
- * @param cb A callback of the form Adapter => Status
- *
- * @return Status::Accept iff there are no errors
- */
- virtual Status apply_array(AdapterCallback const & cb) const = 0;
- /**
- * @brief Loop through every element of the JSON object in this node, applying
- * the given callback function to them.
- *
- * @param cb A callback of the form (string, Adapter) => Status
- *
- * @return Status::Accept iff there are no errors
- */
- virtual Status apply_object(ObjectAdapterCallback const & cb) const = 0;
- virtual bool equals(Adapter const & lhs, bool strict) const = 0;
- /**
- * @brief Test if this object is null-like
- *
- * @param strict Does this function allow for fuzzy comparisons with strings?
- */
- bool maybe_null(bool strict) const {
- switch (type()) {
- case Type::Null:
- return true;
- case Type::String:
- return not strict and as_string().empty();
- default:
- return false;
- }
- }
- /**
- * @brief Attempts to extract a boolean value from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with strings?
- *
- * @return The boolean value contained if it is possible to deduce
- * (or type() == Type::Boolean), else nullopt.
- */
- std::optional<bool> maybe_boolean(bool strict) const {
- switch (type()) {
- case Type::Boolean:
- return as_boolean();
- case Type::String:
- if (not strict) {
- auto str = as_string();
- if (str == "true") {
- return true;
- }
- if (str == "false") {
- return false;
- }
- }
- return std::nullopt;
- default:
- return std::nullopt;
- }
- }
- /**
- * @brief Attempts to extract a integer value from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with strings
- * and/or booleans?
- *
- * @return The integer value contained if it is possible to deduce from an
- * integer, number, boolean, or string. Else nullopt
- */
- std::optional<int64_t> maybe_integer(bool strict) const {
- switch (type()) {
- case Type::Number:
- if (double value = as_number(); jvalidate::detail::fits_in_integer(value)) {
- return static_cast<int64_t>(value);
- }
- return std::nullopt;
- case Type::Integer:
- return as_integer();
- case Type::Boolean:
- if (not strict) {
- return as_boolean() ? 1 : 0;
- }
- return std::nullopt;
- case Type::String:
- if (not strict) {
- auto str = as_string();
- size_t end = 0;
- int64_t rval = std::stoll(str, &end);
- if (end == str.size()) {
- return rval;
- }
- }
- return std::nullopt;
- default:
- return std::nullopt;
- }
- }
- /**
- * @brief Attempts to extract a number value from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with strings?
- *
- * @return The number value contained if it is possible to deduce
- * (or type() == Type::Integer || type() == Type::Number), else nullopt.
- */
- std::optional<double> maybe_number(bool strict) const {
- switch (type()) {
- case Type::Number:
- return as_number();
- case Type::Integer:
- return as_integer();
- case Type::String:
- if (not strict) {
- auto str = as_string();
- size_t end = 0;
- double rval = std::stod(str, &end);
- if (end == str.size()) {
- return rval;
- }
- }
- return std::nullopt;
- default:
- return std::nullopt;
- }
- }
- /**
- * @brief Attempts to extract a string value from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with other
- * types?
- *
- * @return The string value contained if it is possible to deduce from a
- * scalar type, else nullopt.
- */
- std::optional<std::string> maybe_string(bool strict) const {
- switch (type()) {
- case Type::Null:
- return strict ? std::nullopt : std::make_optional("");
- case Type::String:
- return as_string();
- case Type::Number:
- if (not strict) {
- return std::to_string(as_number());
- }
- return std::nullopt;
- case Type::Integer:
- if (not strict) {
- return std::to_string(as_integer());
- }
- return std::nullopt;
- case Type::Boolean:
- if (not strict) {
- return as_boolean() ? "true" : "false";
- }
- return std::nullopt;
- default:
- return std::nullopt;
- }
- }
- /**
- * @brief Attempts to extract the array length from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with other
- * types?
- *
- * @return array_size() if this is an array, else 0 or nullopt, depending
- * on some factors.
- */
- std::optional<size_t> maybe_array_size(bool strict) const {
- switch (type()) {
- case Type::Array:
- return array_size();
- case Type::Null:
- return strict ? std::nullopt : std::make_optional(0UL);
- case Type::Object:
- return (strict || object_size() != 0) ? std::nullopt : std::make_optional(0UL);
- default:
- return std::nullopt;
- }
- }
- /**
- * @brief Attempts to extract the object length from this JSON node
- *
- * @param strict Does this function allow for fuzzy comparisons with other
- * types?
- *
- * @return object_size() if this is an object, else 0 or nullopt, depending
- * on some factors.
- */
- std::optional<size_t> maybe_object_size(bool strict) const {
- switch (type()) {
- case Type::Object:
- return object_size();
- case Type::Null:
- return strict ? std::nullopt : std::make_optional(0UL);
- case Type::Array:
- return (strict || array_size() != 0) ? std::nullopt : std::make_optional(0UL);
- default:
- return std::nullopt;
- }
- }
- };
- /**
- * @brief An interface for an immutable, owning handle to a type-erased JSON
- * node. {@see Adapter::freeze} for more explaination why this is necessary.
- */
- class Const {
- public:
- virtual ~Const() = default;
- /**
- * @brief Perform an action on this object, such as copying or testing
- * equality.
- *
- * @param cb A callback function of the form Adapter => Status
- *
- * @return the result of cb on the contained JSON
- */
- virtual Status apply(AdapterCallback const & cb) const = 0;
- };
- }
- namespace jvalidate::adapter::detail {
- /**
- * @brief The simplest implementation of Const.
- * Depends on the AdapterTraits struct.
- *
- * @tparam JSON The type being adapted
- */
- template <typename JSON> class GenericConst final : public Const {
- public:
- explicit GenericConst(JSON const & value) : value_(value) {}
- Status apply(AdapterCallback const & cb) const {
- return cb(typename AdapterTraits<JSON>::ConstAdapter(value_));
- }
- JSON const & value() const { return value_; }
- private:
- JSON value_;
- };
- }
|