|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
#include <cstdint>
|
|
|
#include <optional>
|
|
|
+#include <ostream>
|
|
|
|
|
|
#include <jvalidate/detail/array_iterator.h>
|
|
|
#include <jvalidate/detail/number.h>
|
|
|
@@ -313,6 +314,48 @@ public:
|
|
|
return std::nullopt;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ virtual void write(std::ostream & os) const {
|
|
|
+ std::string_view div;
|
|
|
+ switch (type()) {
|
|
|
+ case Type::Null:
|
|
|
+ os << "null";
|
|
|
+ break;
|
|
|
+ case Type::Boolean:
|
|
|
+ os << (as_boolean() ? "true" : "false");
|
|
|
+ break;
|
|
|
+ case Type::Integer:
|
|
|
+ os << as_integer();
|
|
|
+ break;
|
|
|
+ case Type::Number:
|
|
|
+ os << as_number();
|
|
|
+ break;
|
|
|
+ case Type::String:
|
|
|
+ os << '"' << as_string() << '"';
|
|
|
+ break;
|
|
|
+ case Type::Array:
|
|
|
+ os << '[';
|
|
|
+ apply_array([&os, &div](Adapter const & elem) {
|
|
|
+ os << std::exchange(div, ", ") << elem;
|
|
|
+ return Status::Accept;
|
|
|
+ });
|
|
|
+ os << ']';
|
|
|
+ break;
|
|
|
+ case Type::Object:
|
|
|
+ os << '{';
|
|
|
+ apply_object([&os, &div](std::string const & key, Adapter const & elem) {
|
|
|
+ os << std::exchange(div, ", ") << '"' << key << '"' << ':' << elem;
|
|
|
+ return Status::Accept;
|
|
|
+ });
|
|
|
+ os << '}';
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ friend std::ostream & operator<<(std::ostream & os, Adapter const & self) {
|
|
|
+ self.write(os);
|
|
|
+ return os;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
@@ -331,6 +374,13 @@ public:
|
|
|
* @return the result of cb on the contained JSON
|
|
|
*/
|
|
|
virtual Status apply(AdapterCallback const & cb) const = 0;
|
|
|
+ friend std::ostream & operator<<(std::ostream & os, Const const & self) {
|
|
|
+ self.apply([&os](Adapter const & adapter) {
|
|
|
+ adapter.write(os);
|
|
|
+ return Status::Accept;
|
|
|
+ });
|
|
|
+ return os;
|
|
|
+ }
|
|
|
};
|
|
|
}
|
|
|
|
|
|
@@ -355,3 +405,15 @@ private:
|
|
|
JSON value_;
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+namespace std {
|
|
|
+inline ostream & operator<<(ostream & os,
|
|
|
+ vector<unique_ptr<jvalidate::adapter::Const const>> const & items) {
|
|
|
+ std::string_view div;
|
|
|
+ os << '[';
|
|
|
+ for (auto const & ptr : items) {
|
|
|
+ os << std::exchange(div, ", ") << *ptr;
|
|
|
+ }
|
|
|
+ return os << ']';
|
|
|
+}
|
|
|
+}
|