| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #pragma once
- #include <memory>
- #include <type_traits>
- #include <json/value.h>
- #include <jvalidate/detail/simple_adapter.h>
- namespace jvalidate::adapter {
- template <typename JSON> class JsonCppAdapter;
- template <> struct AdapterTraits<Json::Value> {
- template <typename JSON> using Adapter = adapter::JsonCppAdapter<JSON>;
- using ConstAdapter = adapter::JsonCppAdapter<Json::Value const>;
- static Json::Value const & const_empty() {
- static Json::Value const g_value;
- return g_value;
- }
- };
- template <typename JSON> class JsonCppObjectAdapter : public detail::SimpleObjectAdapter<JSON> {
- public:
- using JsonCppObjectAdapter::SimpleObjectAdapter::SimpleObjectAdapter;
- bool contains(std::string const & key) const { return this->const_value().isMember(key); }
- JsonCppAdapter<JSON> operator[](std::string const & key) const {
- return this->value() ? &(*this->value())[key] : nullptr;
- }
- };
- template <typename JSON> class JsonCppAdapter final : public detail::SimpleAdapter<JSON> {
- private:
- JSON * value_;
- public:
- using JsonCppAdapter::SimpleAdapter::SimpleAdapter;
- Type type() const {
- switch (const_value().type()) {
- case Json::nullValue:
- return Type::Null;
- case Json::booleanValue:
- return Type::Boolean;
- case Json::realValue:
- return Type::Number;
- case Json::stringValue:
- return Type::String;
- case Json::arrayValue:
- return Type::Array;
- case Json::objectValue:
- return Type::Object;
- case Json::intValue:
- case Json::uintValue:
- return Type::Integer;
- }
- }
- bool as_boolean() const { return const_value().asBool(); }
- int64_t as_integer() const { return const_value().asInt64(); }
- double as_number() const { return const_value().asDouble(); }
- std::string as_string() const { return const_value().asString(); }
- JsonCppObjectAdapter<JSON> as_object() const { return value_; }
- static std::string key(auto it) { return it.key().asString(); }
- private:
- using JsonCppAdapter::SimpleAdapter::const_value;
- };
- }
|