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 | |
15 | #ifndef dap_nlohmann_json_serializer_h |
16 | #define dap_nlohmann_json_serializer_h |
17 | |
18 | #include "dap/protocol.h" |
19 | #include "dap/serialization.h" |
20 | #include "dap/types.h" |
21 | |
22 | #include <nlohmann/json_fwd.hpp> |
23 | |
24 | namespace dap { |
25 | namespace json { |
26 | |
27 | struct NlohmannDeserializer : public dap::Deserializer { |
28 | explicit NlohmannDeserializer(const std::string&); |
29 | ~NlohmannDeserializer(); |
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 | NlohmannDeserializer(const nlohmann::json*); |
77 | const nlohmann::json* const json; |
78 | const bool ownsJson; |
79 | }; |
80 | |
81 | struct NlohmannSerializer : public dap::Serializer { |
82 | NlohmannSerializer(); |
83 | ~NlohmannSerializer(); |
84 | |
85 | std::string dump() const; |
86 | |
87 | // dap::Serializer compliance |
88 | bool serialize(boolean v) override; |
89 | bool serialize(integer v) override; |
90 | bool serialize(number v) override; |
91 | bool serialize(const string& v) override; |
92 | bool serialize(const dap::object& v) override; |
93 | bool serialize(const any& v) override; |
94 | bool array(size_t count, |
95 | const std::function<bool(dap::Serializer*)>&) override; |
96 | bool object(const std::function<bool(dap::FieldSerializer*)>&) override; |
97 | void remove() override; |
98 | |
99 | // Unhide base overloads |
100 | template <typename T, |
101 | typename = std::enable_if<TypeOf<T>::has_custom_serialization>> |
102 | inline bool serialize(const T& v) { |
103 | return dap::Serializer::serialize(v); |
104 | } |
105 | |
106 | template <typename T> |
107 | inline bool serialize(const dap::array<T>& v) { |
108 | return dap::Serializer::serialize(v); |
109 | } |
110 | |
111 | template <typename T> |
112 | inline bool serialize(const dap::optional<T>& v) { |
113 | return dap::Serializer::serialize(v); |
114 | } |
115 | |
116 | template <typename T0, typename... Types> |
117 | inline bool serialize(const dap::variant<T0, Types...>& v) { |
118 | return dap::Serializer::serialize(v); |
119 | } |
120 | |
121 | inline bool serialize(const char* v) { return dap::Serializer::serialize(v); } |
122 | |
123 | private: |
124 | NlohmannSerializer(nlohmann::json*); |
125 | nlohmann::json* const json; |
126 | const bool ownsJson; |
127 | bool removed = false; |
128 | }; |
129 | |
130 | } // namespace json |
131 | } // namespace dap |
132 | |
133 | #endif // dap_nlohmann_json_serializer_h |