| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- #pragma once
- #include <cmath>
- #include <cstdint>
- #include <limits>
- #include <map>
- #include <optional>
- #include <string_view>
- #include <jvalidate/detail/array_iterator.h>
- #include <jvalidate/detail/object_iterator.h>
- #include <jvalidate/enum.h>
- #include <jvalidate/forward.h>
- #include <jvalidate/status.h>
- namespace jvalidate::adapter {
- class Adapter {
- public:
- virtual ~Adapter() = default;
- virtual Type type() const = 0;
- virtual std::unique_ptr<Const const> 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<int64_t>::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<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;
- }
- }
- std::optional<int64_t> 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<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;
- }
- }
- 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;
- }
- }
- 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;
- }
- }
- 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;
- }
- }
- };
- class Const {
- public:
- virtual ~Const() = default;
- virtual Status apply(AdapterCallback const & cb) const = 0;
- };
- }
- namespace jvalidate::adapter::detail {
- 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_;
- };
- }
|