jsoncpp.h 4.0 KB

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