jsoncpp.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include <type_traits>
  3. #include <json/reader.h>
  4. #include <json/value.h>
  5. #include <jvalidate/adapter.h>
  6. #include <jvalidate/compat/expected.h>
  7. #include <jvalidate/detail/expect.h>
  8. #include <jvalidate/detail/number.h>
  9. #include <jvalidate/detail/simple_adapter.h>
  10. #include <jvalidate/enum.h>
  11. namespace jvalidate::adapter {
  12. template <>
  13. inline auto load_stream(std::istream & in, Json::Value & out) noexcept
  14. -> detail::expected<void, std::string> {
  15. Json::CharReaderBuilder builder;
  16. if (std::string error; not Json::parseFromStream(builder, in, &out, &error)) {
  17. return detail::unexpected(error);
  18. }
  19. return {};
  20. }
  21. template <typename JSON> class JsonCppAdapter;
  22. template <> struct AdapterTraits<Json::Value> {
  23. template <typename JSON> using Adapter = adapter::JsonCppAdapter<JSON>;
  24. using ConstAdapter = adapter::JsonCppAdapter<Json::Value const>;
  25. static Json::Value const & const_empty() {
  26. static Json::Value const g_value;
  27. return g_value;
  28. }
  29. };
  30. template <typename JSON> class JsonCppObjectAdapter : public detail::SimpleObjectAdapter<JSON> {
  31. public:
  32. using JsonCppObjectAdapter::SimpleObjectAdapter::SimpleObjectAdapter;
  33. bool contains(std::string const & key) const { return this->const_value().isMember(key); }
  34. JsonCppAdapter<JSON> operator[](std::string const & key) const {
  35. return this->value() ? &(*this->value())[key] : nullptr;
  36. }
  37. void assign(std::string const & key, Const const & frozen) const
  38. requires(not std::is_const_v<JSON>)
  39. {
  40. (*this)[key].assign(frozen);
  41. }
  42. };
  43. template <typename JSON> class JsonCppAdapter final : public detail::SimpleAdapter<JSON> {
  44. public:
  45. using JsonCppAdapter::SimpleAdapter::SimpleAdapter;
  46. Type type() const {
  47. using jvalidate::detail::fits_in_integer;
  48. switch (const_value().type()) {
  49. case Json::nullValue:
  50. return Type::Null;
  51. case Json::booleanValue:
  52. return Type::Boolean;
  53. case Json::realValue:
  54. // Not strictly necessary - Validation code will safely handle this too
  55. return fits_in_integer(const_value().asDouble()) ? Type::Integer : Type::Number;
  56. case Json::stringValue:
  57. return Type::String;
  58. case Json::arrayValue:
  59. return Type::Array;
  60. case Json::objectValue:
  61. return Type::Object;
  62. case Json::intValue:
  63. return Type::Integer;
  64. case Json::uintValue:
  65. return fits_in_integer(const_value().asUInt64()) ? Type::Integer : Type::Number;
  66. }
  67. }
  68. bool as_boolean() const { return const_value().asBool(); }
  69. int64_t as_integer() const { return const_value().asInt64(); }
  70. double as_number() const { return const_value().asDouble(); }
  71. std::string as_string() const { return const_value().asString(); }
  72. JsonCppObjectAdapter<JSON> as_object() const { return value(); }
  73. static std::string key(auto it) { return it.key().asString(); }
  74. using detail::SimpleAdapter<JSON>::assign;
  75. void assign(Adapter const & adapter) const
  76. requires(not std::is_const_v<JSON>)
  77. {
  78. switch (adapter.type()) {
  79. case Type::Null:
  80. *value() = Json::nullValue;
  81. return;
  82. case Type::Boolean:
  83. *value() = adapter.as_boolean();
  84. return;
  85. case Type::Integer:
  86. *value() = adapter.as_integer();
  87. return;
  88. case Type::Number:
  89. *value() = adapter.as_number();
  90. return;
  91. case Type::String:
  92. *value() = adapter.as_string();
  93. return;
  94. case Type::Array:
  95. adapter.apply_array([this, index = 0](Adapter const & elem) mutable {
  96. JsonCppAdapter((*value())[index]).assign(elem);
  97. ++index;
  98. return Status::Accept;
  99. });
  100. return;
  101. case Type::Object:
  102. adapter.apply_object([this](std::string const & key, Adapter const & elem) {
  103. JsonCppAdapter((*value())[key]).assign(elem);
  104. return Status::Accept;
  105. });
  106. return;
  107. }
  108. }
  109. public:
  110. using JsonCppAdapter::SimpleAdapter::const_value;
  111. using JsonCppAdapter::SimpleAdapter::value;
  112. };
  113. template <typename JSON> JsonCppAdapter(JSON const &) -> JsonCppAdapter<JSON const>;
  114. template <typename JSON> JsonCppAdapter(JSON &) -> JsonCppAdapter<JSON>;
  115. }