jsoncpp.h 3.9 KB

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