jsoncpp.h 3.5 KB

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