Debian Codegraph @

Utilities/cmcppdap/src/rapid_json_serializer.h

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