| 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 |
|
| 15 |
#ifndef dap_jsoncpp_json_serializer_h |
| 16 |
#define dap_jsoncpp_json_serializer_h |
| 17 |
|
| 18 |
#include "dap/protocol.h" |
| 19 |
#include "dap/serialization.h" |
| 20 |
#include "dap/types.h" |
| 21 |
|
| 22 |
#include <cm3p/json/forwards.h> |
| 23 |
|
| 24 |
namespace dap { |
| 25 |
namespace json { |
| 26 |
|
| 27 |
struct JsonCppDeserializer : public dap::Deserializer { |
| 28 |
explicit JsonCppDeserializer(const std::string&); |
| 29 |
~JsonCppDeserializer(); |
| 30 |
|
| 31 |
// dap::Deserializer compliance |
| 32 |
bool deserialize(boolean* v) const override; |
| 33 |
bool deserialize(integer* v) const override; |
| 34 |
bool deserialize(number* v) const override; |
| 35 |
bool deserialize(string* v) const override; |
| 36 |
bool deserialize(object* v) const override; |
| 37 |
bool deserialize(any* v) const override; |
| 38 |
size_t count() const override; |
| 39 |
bool array(const std::function<bool(dap::Deserializer*)>&) const override; |
| 40 |
bool field(const std::string& name, |
| 41 |
const std::function<bool(dap::Deserializer*)>&) const override; |
| 42 |
|
| 43 |
// Unhide base overloads |
| 44 |
template <typename T> |
| 45 |
inline bool field(const std::string& name, T* v) { |
| 46 |
return dap::Deserializer::field(name, v); |
| 47 |
} |
| 48 |
|
| 49 |
template <typename T, |
| 50 |
typename = std::enable_if<TypeOf<T>::has_custom_serialization>> |
| 51 |
inline bool deserialize(T* v) const { |
| 52 |
return dap::Deserializer::deserialize(v); |
| 53 |
} |
| 54 |
|
| 55 |
template <typename T> |
| 56 |
inline bool deserialize(dap::array<T>* v) const { |
| 57 |
return dap::Deserializer::deserialize(v); |
| 58 |
} |
| 59 |
|
| 60 |
template <typename T> |
| 61 |
inline bool deserialize(dap::optional<T>* v) const { |
| 62 |
return dap::Deserializer::deserialize(v); |
| 63 |
} |
| 64 |
|
| 65 |
template <typename T0, typename... Types> |
| 66 |
inline bool deserialize(dap::variant<T0, Types...>* v) const { |
| 67 |
return dap::Deserializer::deserialize(v); |
| 68 |
} |
| 69 |
|
| 70 |
template <typename T> |
| 71 |
inline bool field(const std::string& name, T* v) const { |
| 72 |
return dap::Deserializer::deserialize(name, v); |
| 73 |
} |
| 74 |
|
| 75 |
private: |
| 76 |
JsonCppDeserializer(const Json::Value*); |
| 77 |
static Json::Value parse(const std::string& text); |
| 78 |
const Json::Value* const json; |
| 79 |
const bool ownsJson; |
| 80 |
}; |
| 81 |
|
| 82 |
struct JsonCppSerializer : public dap::Serializer { |
| 83 |
JsonCppSerializer(); |
| 84 |
~JsonCppSerializer(); |
| 85 |
|
| 86 |
std::string dump() const; |
| 87 |
|
| 88 |
// dap::Serializer compliance |
| 89 |
bool serialize(boolean v) override; |
| 90 |
bool serialize(integer v) override; |
| 91 |
bool serialize(number v) override; |
| 92 |
bool serialize(const string& v) override; |
| 93 |
bool serialize(const dap::object& v) override; |
| 94 |
bool serialize(const any& v) override; |
| 95 |
bool array(size_t count, |
| 96 |
const std::function<bool(dap::Serializer*)>&) override; |
| 97 |
bool object(const std::function<bool(dap::FieldSerializer*)>&) override; |
| 98 |
void remove() override; |
| 99 |
|
| 100 |
// Unhide base overloads |
| 101 |
template <typename T, |
| 102 |
typename = std::enable_if<TypeOf<T>::has_custom_serialization>> |
| 103 |
inline bool serialize(const T& v) { |
| 104 |
return dap::Serializer::serialize(v); |
| 105 |
} |
| 106 |
|
| 107 |
template <typename T> |
| 108 |
inline bool serialize(const dap::array<T>& v) { |
| 109 |
return dap::Serializer::serialize(v); |
| 110 |
} |
| 111 |
|
| 112 |
template <typename T> |
| 113 |
inline bool serialize(const dap::optional<T>& v) { |
| 114 |
return dap::Serializer::serialize(v); |
| 115 |
} |
| 116 |
|
| 117 |
template <typename T0, typename... Types> |
| 118 |
inline bool serialize(const dap::variant<T0, Types...>& v) { |
| 119 |
return dap::Serializer::serialize(v); |
| 120 |
} |
| 121 |
|
| 122 |
inline bool serialize(const char* v) { return dap::Serializer::serialize(v); } |
| 123 |
|
| 124 |
private: |
| 125 |
JsonCppSerializer(Json::Value*); |
| 126 |
Json::Value* const json; |
| 127 |
const bool ownsJson; |
| 128 |
bool removed = false; |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace json |
| 132 |
} // namespace dap |
| 133 |
|
| 134 |
#endif // dap_jsoncpp_json_serializer_h |