adapter.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #include <cmath>
  3. #include <cstdint>
  4. #include <limits>
  5. #include <map>
  6. #include <optional>
  7. #include <string_view>
  8. #include <jvalidate/detail/array_iterator.h>
  9. #include <jvalidate/detail/object_iterator.h>
  10. #include <jvalidate/enum.h>
  11. #include <jvalidate/forward.h>
  12. #include <jvalidate/status.h>
  13. namespace jvalidate::adapter {
  14. class Adapter {
  15. public:
  16. virtual ~Adapter() = default;
  17. virtual Type type() const = 0;
  18. virtual std::unique_ptr<Const const> freeze() const = 0;
  19. virtual bool as_boolean() const = 0;
  20. virtual int64_t as_integer() const = 0;
  21. virtual double as_number() const = 0;
  22. virtual std::string as_string() const = 0;
  23. virtual size_t array_size() const = 0;
  24. virtual size_t object_size() const = 0;
  25. virtual Status apply_array(AdapterCallback const & cb) const = 0;
  26. virtual Status apply_object(ObjectAdapterCallback const & cb) const = 0;
  27. virtual bool equals(Adapter const & lhs, bool strict) const = 0;
  28. static bool is_integer(double value) {
  29. return std::floor(value) == value && std::abs(value) <= std::numeric_limits<int64_t>::max();
  30. }
  31. bool maybe_null(bool strict) const {
  32. switch (type()) {
  33. case Type::Null:
  34. return true;
  35. case Type::String:
  36. return not strict and as_string().empty();
  37. default:
  38. return false;
  39. }
  40. }
  41. std::optional<bool> maybe_boolean(bool strict) const {
  42. switch (type()) {
  43. case Type::Boolean:
  44. return as_boolean();
  45. case Type::String:
  46. if (not strict) {
  47. auto str = as_string();
  48. if (str == "true") {
  49. return true;
  50. }
  51. if (str == "false") {
  52. return false;
  53. }
  54. }
  55. return std::nullopt;
  56. default:
  57. return std::nullopt;
  58. }
  59. }
  60. std::optional<int64_t> maybe_integer(bool strict) const {
  61. switch (type()) {
  62. case Type::Integer:
  63. return as_integer();
  64. case Type::Boolean:
  65. if (not strict) {
  66. return as_boolean() ? 1 : 0;
  67. }
  68. return std::nullopt;
  69. case Type::String:
  70. if (not strict) {
  71. auto str = as_string();
  72. size_t end = 0;
  73. int64_t rval = std::stoll(str, &end);
  74. if (end == str.size()) {
  75. return rval;
  76. }
  77. }
  78. return std::nullopt;
  79. default:
  80. return std::nullopt;
  81. }
  82. }
  83. std::optional<double> maybe_number(bool strict) const {
  84. switch (type()) {
  85. case Type::Number:
  86. return as_number();
  87. case Type::Integer:
  88. return as_integer();
  89. case Type::String:
  90. if (not strict) {
  91. auto str = as_string();
  92. size_t end = 0;
  93. double rval = std::stod(str, &end);
  94. if (end == str.size()) {
  95. return rval;
  96. }
  97. }
  98. return std::nullopt;
  99. default:
  100. return std::nullopt;
  101. }
  102. }
  103. std::optional<std::string> maybe_string(bool strict) const {
  104. switch (type()) {
  105. case Type::Null:
  106. return strict ? std::nullopt : std::make_optional("");
  107. case Type::String:
  108. return as_string();
  109. case Type::Number:
  110. if (not strict) {
  111. return std::to_string(as_number());
  112. }
  113. return std::nullopt;
  114. case Type::Integer:
  115. if (not strict) {
  116. return std::to_string(as_integer());
  117. }
  118. return std::nullopt;
  119. case Type::Boolean:
  120. if (not strict) {
  121. return as_boolean() ? "true" : "false";
  122. }
  123. return std::nullopt;
  124. default:
  125. return std::nullopt;
  126. }
  127. }
  128. std::optional<size_t> maybe_array_size(bool strict) const {
  129. switch (type()) {
  130. case Type::Array:
  131. return array_size();
  132. case Type::Null:
  133. return strict ? std::nullopt : std::make_optional(0UL);
  134. case Type::Object:
  135. return (strict || object_size() != 0) ? std::nullopt : std::make_optional(0UL);
  136. default:
  137. return std::nullopt;
  138. }
  139. }
  140. std::optional<size_t> maybe_object_size(bool strict) const {
  141. switch (type()) {
  142. case Type::Object:
  143. return object_size();
  144. case Type::Null:
  145. return strict ? std::nullopt : std::make_optional(0UL);
  146. case Type::Array:
  147. return (strict || array_size() != 0) ? std::nullopt : std::make_optional(0UL);
  148. default:
  149. return std::nullopt;
  150. }
  151. }
  152. };
  153. class Const {
  154. public:
  155. virtual ~Const() = default;
  156. virtual Status apply(AdapterCallback const & cb) const = 0;
  157. };
  158. }
  159. namespace jvalidate::adapter::detail {
  160. template <typename JSON> class GenericConst final : public Const {
  161. public:
  162. explicit GenericConst(JSON const & value) : value_(value) {}
  163. Status apply(AdapterCallback const & cb) const {
  164. return cb(typename AdapterTraits<JSON>::ConstAdapter(value_));
  165. }
  166. JSON const & value() const { return value_; }
  167. private:
  168. JSON value_;
  169. };
  170. }