Debian Codegraph @

LspCpp/src/jsonrpc/serializer.cpp

raw
1 #include "LibLsp/JsonRpc/serializer.h"
2 #include <stdexcept>
3 #include <rapidjson/allocators.h>
4 #include "LibLsp/JsonRpc/json.h"
5
6 //// Elementary types
7
8 void JsonNull::swap(JsonNull&) noexcept
9 {
10 }
11
12 void Reflect(Reader& visitor, uint8_t& value)
13 {
14 if (!visitor.IsInt())
15 {
16 throw std::invalid_argument("uint8_t");
17 }
18 value = (uint8_t)visitor.GetInt();
19 }
20 void Reflect(Writer& visitor, uint8_t& value)
21 {
22 visitor.Int(value);
23 }
24
25 void Reflect(Reader& visitor, short& value)
26 {
27 if (!visitor.IsInt())
28 {
29 throw std::invalid_argument("short");
30 }
31 value = (short)visitor.GetInt();
32 }
33 void Reflect(Writer& visitor, short& value)
34 {
35 visitor.Int(value);
36 }
37
38 void Reflect(Reader& visitor, unsigned short& value)
39 {
40 if (!visitor.IsInt())
41 {
42 throw std::invalid_argument("unsigned short");
43 }
44 value = (unsigned short)visitor.GetInt();
45 }
46 void Reflect(Writer& visitor, unsigned short& value)
47 {
48 visitor.Int(value);
49 }
50
51 void Reflect(Reader& visitor, int& value)
52 {
53 if (!visitor.IsInt())
54 {
55 throw std::invalid_argument("int");
56 }
57 value = visitor.GetInt();
58 }
59 void Reflect(Writer& visitor, int& value)
60 {
61 visitor.Int(value);
62 }
63
64 void Reflect(Reader& visitor, unsigned& value)
65 {
66 if (!visitor.IsUint64())
67 {
68 throw std::invalid_argument("unsigned");
69 }
70 value = visitor.GetUint32();
71 }
72 void Reflect(Writer& visitor, unsigned& value)
73 {
74 visitor.Uint32(value);
75 }
76
77 void Reflect(Reader& visitor, long& value)
78 {
79 if (!visitor.IsInt64())
80 {
81 throw std::invalid_argument("long");
82 }
83 value = long(visitor.GetInt64());
84 }
85 void Reflect(Writer& visitor, long& value)
86 {
87 visitor.Int64(value);
88 }
89
90 void Reflect(Reader& visitor, unsigned long& value)
91 {
92 if (!visitor.IsUint64())
93 {
94 throw std::invalid_argument("unsigned long");
95 }
96 value = (unsigned long)visitor.GetUint64();
97 }
98 void Reflect(Writer& visitor, unsigned long& value)
99 {
100 visitor.Uint64(value);
101 }
102
103 void Reflect(Reader& visitor, long long& value)
104 {
105 if (!visitor.IsInt64())
106 {
107 throw std::invalid_argument("long long");
108 }
109 value = visitor.GetInt64();
110 }
111 void Reflect(Writer& visitor, long long& value)
112 {
113 visitor.Int64(value);
114 }
115
116 void Reflect(Reader& visitor, unsigned long long& value)
117 {
118 if (!visitor.IsUint64())
119 {
120 throw std::invalid_argument("unsigned long long");
121 }
122 value = visitor.GetUint64();
123 }
124 void Reflect(Writer& visitor, unsigned long long& value)
125 {
126 visitor.Uint64(value);
127 }
128
129 void Reflect(Reader& visitor, double& value)
130 {
131 if (!visitor.IsNumber())
132 {
133 throw std::invalid_argument("double");
134 }
135 value = visitor.GetDouble();
136 }
137 void Reflect(Writer& visitor, double& value)
138 {
139 visitor.Double(value);
140 }
141
142 void Reflect(Reader& visitor, bool& value)
143 {
144 if (!visitor.IsBool())
145 {
146 throw std::invalid_argument("bool");
147 }
148 value = visitor.GetBool();
149 }
150 void Reflect(Writer& visitor, bool& value)
151 {
152 visitor.Bool(value);
153 }
154
155 void Reflect(Reader& visitor, std::string& value)
156 {
157 if (!visitor.IsString())
158 {
159 throw std::invalid_argument("std::string");
160 }
161 value = visitor.GetString();
162 }
163 void Reflect(Writer& visitor, std::string& value)
164 {
165 visitor.String(value.c_str(), (rapidjson::SizeType)value.size());
166 }
167
168 void Reflect(Reader& visitor, JsonNull&)
169 {
170 visitor.GetNull();
171 }
172
173 void Reflect(Writer& visitor, JsonNull&)
174 {
175 visitor.Null();
176 }
177
178 void Reflect(Reader& visitor, SerializeFormat& value)
179 {
180 std::string fmt = visitor.GetString();
181 value = fmt[0] == 'm' ? SerializeFormat::MessagePack : SerializeFormat::Json;
182 }
183
184 void Reflect(Writer& visitor, SerializeFormat& value)
185 {
186 switch (value)
187 {
188 case SerializeFormat::Json:
189 visitor.String("json");
190 break;
191 case SerializeFormat::MessagePack:
192 visitor.String("msgpack");
193 break;
194 }
195 }
196
197 std::string JsonReader::ToString() const
198 {
199 rapidjson::StringBuffer strBuf;
200 strBuf.Clear();
201 rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
202 m_->Accept(writer);
203 std::string strJson = strBuf.GetString();
204 return strJson;
205 }
206
207 void JsonReader::IterMap(std::function<void(char const*, Reader&)> fn)
208 {
209 path_.push_back("0");
210 for (auto& entry : m_->GetObject())
211 {
212 auto saved = m_;
213 m_ = &(entry.value);
214
215 fn(entry.name.GetString(), *this);
216 m_ = saved;
217 }
218 path_.pop_back();
219 }
220
221 void JsonReader::IterArray(std::function<void(Reader&)> fn)
222 {
223 if (!m_->IsArray())
224 {
225 throw std::invalid_argument("array");
226 }
227 // Use "0" to indicate any element for now.
228 path_.push_back("0");
229 for (auto& entry : m_->GetArray())
230 {
231 auto saved = m_;
232 m_ = &entry;
233 fn(*this);
234 m_ = saved;
235 }
236 path_.pop_back();
237 }
238
239 void JsonReader::DoMember(char const* name, std::function<void(Reader&)> fn)
240 {
241 path_.push_back(name);
242 auto it = m_->FindMember(name);
243 if (it != m_->MemberEnd())
244 {
245 auto saved = m_;
246 m_ = &it->value;
247 fn(*this);
248 m_ = saved;
249 }
250 path_.pop_back();
251 }
252
253 std::string JsonReader::GetPath() const
254 {
255 std::string ret;
256 for (auto& t : path_)
257 {
258 ret += '/';
259 ret += t;
260 }
261 ret.pop_back();
262 return ret;
263 }