jsoncpp.h 3.5 KB

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