#pragma once #include #include #include #include #include #include #include #include #include #include #include namespace jvalidate::adapter { class Adapter { public: virtual ~Adapter() = default; virtual Type type() const = 0; virtual std::unique_ptr freeze() const = 0; virtual bool as_boolean() const = 0; virtual int64_t as_integer() const = 0; virtual double as_number() const = 0; virtual std::string as_string() const = 0; virtual size_t array_size() const = 0; virtual size_t object_size() const = 0; virtual Status apply_array(AdapterCallback const & cb) const = 0; virtual Status apply_object(ObjectAdapterCallback const & cb) const = 0; virtual bool equals(Adapter const & lhs, bool strict) const = 0; static bool is_integer(double value) { return std::floor(value) == value && std::abs(value) <= std::numeric_limits::max(); } 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; } } 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; } } std::optional maybe_integer(bool strict) const { switch (type()) { 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; } } 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; } } 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; } } 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; } } 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; } } }; class Const { public: virtual ~Const() = default; virtual Status apply(AdapterCallback const & cb) const = 0; }; } namespace jvalidate::adapter::detail { 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_; }; }