1
0

nlohmann_json_serializer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef dap_nlohmann_json_serializer_h
  15. #define dap_nlohmann_json_serializer_h
  16. #include "dap/protocol.h"
  17. #include "dap/serialization.h"
  18. #include "dap/types.h"
  19. #include <nlohmann/json_fwd.hpp>
  20. namespace dap {
  21. namespace json {
  22. struct NlohmannDeserializer : public dap::Deserializer {
  23. explicit NlohmannDeserializer(const std::string&);
  24. ~NlohmannDeserializer();
  25. // dap::Deserializer compliance
  26. bool deserialize(boolean* v) const override;
  27. bool deserialize(integer* v) const override;
  28. bool deserialize(number* v) const override;
  29. bool deserialize(string* v) const override;
  30. bool deserialize(object* v) const override;
  31. bool deserialize(any* v) const override;
  32. size_t count() const override;
  33. bool array(const std::function<bool(dap::Deserializer*)>&) const override;
  34. bool field(const std::string& name,
  35. const std::function<bool(dap::Deserializer*)>&) const override;
  36. // Unhide base overloads
  37. template <typename T>
  38. inline bool field(const std::string& name, T* v) {
  39. return dap::Deserializer::field(name, v);
  40. }
  41. template <typename T,
  42. typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  43. inline bool deserialize(T* v) const {
  44. return dap::Deserializer::deserialize(v);
  45. }
  46. template <typename T>
  47. inline bool deserialize(dap::array<T>* v) const {
  48. return dap::Deserializer::deserialize(v);
  49. }
  50. template <typename T>
  51. inline bool deserialize(dap::optional<T>* v) const {
  52. return dap::Deserializer::deserialize(v);
  53. }
  54. template <typename T0, typename... Types>
  55. inline bool deserialize(dap::variant<T0, Types...>* v) const {
  56. return dap::Deserializer::deserialize(v);
  57. }
  58. template <typename T>
  59. inline bool field(const std::string& name, T* v) const {
  60. return dap::Deserializer::deserialize(name, v);
  61. }
  62. private:
  63. NlohmannDeserializer(const nlohmann::json*);
  64. const nlohmann::json* const json;
  65. const bool ownsJson;
  66. };
  67. struct NlohmannSerializer : public dap::Serializer {
  68. NlohmannSerializer();
  69. ~NlohmannSerializer();
  70. std::string dump() const;
  71. // dap::Serializer compliance
  72. bool serialize(boolean v) override;
  73. bool serialize(integer v) override;
  74. bool serialize(number v) override;
  75. bool serialize(const string& v) override;
  76. bool serialize(const dap::object& v) override;
  77. bool serialize(const any& v) override;
  78. bool array(size_t count,
  79. const std::function<bool(dap::Serializer*)>&) override;
  80. bool object(const std::function<bool(dap::FieldSerializer*)>&) override;
  81. void remove() override;
  82. // Unhide base overloads
  83. template <typename T,
  84. typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  85. inline bool serialize(const T& v) {
  86. return dap::Serializer::serialize(v);
  87. }
  88. template <typename T>
  89. inline bool serialize(const dap::array<T>& v) {
  90. return dap::Serializer::serialize(v);
  91. }
  92. template <typename T>
  93. inline bool serialize(const dap::optional<T>& v) {
  94. return dap::Serializer::serialize(v);
  95. }
  96. template <typename T0, typename... Types>
  97. inline bool serialize(const dap::variant<T0, Types...>& v) {
  98. return dap::Serializer::serialize(v);
  99. }
  100. inline bool serialize(const char* v) { return dap::Serializer::serialize(v); }
  101. private:
  102. NlohmannSerializer(nlohmann::json*);
  103. nlohmann::json* const json;
  104. const bool ownsJson;
  105. bool removed = false;
  106. };
  107. } // namespace json
  108. } // namespace dap
  109. #endif // dap_nlohmann_json_serializer_h