#pragma once #include #include #include #include #include #include #include #include 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 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 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 maybe_integer(bool strict) const { switch (type()) { case Type::Number: if (double value = as_number(); jvalidate::detail::fits_in_integer(value)) { return static_cast(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 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 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 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 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 class GenericConst final : public Const { public: explicit GenericConst(JSON const & value) : value_(value) {} Status apply(AdapterCallback const & cb) const { return cb(typename AdapterTraits::ConstAdapter(value_)); } JSON const & value() const { return value_; } private: JSON value_; }; }