1
0

jsoncpp_json_serializer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2023 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_jsoncpp_json_serializer_h
  15. #define dap_jsoncpp_json_serializer_h
  16. #include "dap/protocol.h"
  17. #include "dap/serialization.h"
  18. #include "dap/types.h"
  19. #include <cm3p/json/forwards.h>
  20. namespace dap {
  21. namespace json {
  22. struct JsonCppDeserializer : public dap::Deserializer {
  23. explicit JsonCppDeserializer(const std::string&);
  24. ~JsonCppDeserializer();
  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. JsonCppDeserializer(const Json::Value*);
  64. static Json::Value parse(const std::string& text);
  65. const Json::Value* const json;
  66. const bool ownsJson;
  67. };
  68. struct JsonCppSerializer : public dap::Serializer {
  69. JsonCppSerializer();
  70. ~JsonCppSerializer();
  71. std::string dump() const;
  72. // dap::Serializer compliance
  73. bool serialize(boolean v) override;
  74. bool serialize(integer v) override;
  75. bool serialize(number v) override;
  76. bool serialize(const string& v) override;
  77. bool serialize(const dap::object& v) override;
  78. bool serialize(const any& v) override;
  79. bool array(size_t count,
  80. const std::function<bool(dap::Serializer*)>&) override;
  81. bool object(const std::function<bool(dap::FieldSerializer*)>&) override;
  82. void remove() override;
  83. // Unhide base overloads
  84. template <typename T,
  85. typename = std::enable_if<TypeOf<T>::has_custom_serialization>>
  86. inline bool serialize(const T& v) {
  87. return dap::Serializer::serialize(v);
  88. }
  89. template <typename T>
  90. inline bool serialize(const dap::array<T>& v) {
  91. return dap::Serializer::serialize(v);
  92. }
  93. template <typename T>
  94. inline bool serialize(const dap::optional<T>& v) {
  95. return dap::Serializer::serialize(v);
  96. }
  97. template <typename T0, typename... Types>
  98. inline bool serialize(const dap::variant<T0, Types...>& v) {
  99. return dap::Serializer::serialize(v);
  100. }
  101. inline bool serialize(const char* v) { return dap::Serializer::serialize(v); }
  102. private:
  103. JsonCppSerializer(Json::Value*);
  104. Json::Value* const json;
  105. const bool ownsJson;
  106. bool removed = false;
  107. };
  108. } // namespace json
  109. } // namespace dap
  110. #endif // dap_jsoncpp_json_serializer_h