forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaoCppJSONResponse.cpp
More file actions
287 lines (232 loc) · 4.49 KB
/
Copy pathTaoCppJSONResponse.cpp
File metadata and controls
287 lines (232 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "graphqlservice/JSONResponse.h"
#include <tao/json.hpp>
#include <cstdint>
#include <iostream>
#include <limits>
#include <sstream>
#include <vector>
namespace graphql::response {
class StreamWriter : public std::enable_shared_from_this<StreamWriter>
{
public:
StreamWriter(std::ostream& stream)
: _writer { stream }
{
}
void add_value(std::shared_ptr<const Value>&& value)
{
auto writer = std::make_shared<ValueVisitor>(shared_from_this());
ValueTokenStream(Value { *value }).visit(writer);
}
void reserve(std::size_t /* count */)
{
}
void start_object()
{
_scopeStack.push_back(Scope::Object);
_writer.begin_object();
}
void add_member(std::string&& key)
{
_writer.key(key);
}
void end_object()
{
_writer.end_object();
_scopeStack.pop_back();
end_value();
}
void start_array()
{
_scopeStack.push_back(Scope::Object);
_writer.begin_array();
}
void end_array()
{
_writer.end_array();
_scopeStack.pop_back();
end_value();
}
void add_null()
{
_writer.null();
end_value();
}
void add_string(std::string&& value)
{
_writer.string(value);
end_value();
}
void add_enum(std::string&& value)
{
add_string(std::move(value));
}
void add_id(IdType&& value)
{
add_string(value.release<std::string>());
}
void add_bool(bool value)
{
_writer.boolean(value);
end_value();
}
void add_int(int value)
{
_writer.number(static_cast<std::int64_t>(value));
end_value();
}
void add_float(double value)
{
_writer.number(value);
end_value();
}
void complete()
{
}
private:
enum class Scope
{
Array,
Object,
};
void end_value()
{
if (_scopeStack.empty())
{
return;
}
switch (_scopeStack.back())
{
case Scope::Array:
_writer.element();
break;
case Scope::Object:
_writer.member();
break;
}
}
tao::json::events::to_stream _writer;
std::vector<Scope> _scopeStack;
};
std::string toJSON(Value&& response)
{
std::ostringstream stream;
auto writer = std::make_shared<ValueVisitor>(std::make_shared<StreamWriter>(stream));
ValueTokenStream(std::move(response)).visit(writer);
return stream.str();
}
struct ResponseHandler
{
ResponseHandler()
{
// Start with a single null value.
_responseStack.push_back({});
}
Value getResponse()
{
auto response = std::move(_responseStack.back());
_responseStack.pop_back();
return response;
}
void null()
{
setValue(Value());
}
void boolean(bool b)
{
setValue(Value(b));
}
void number(double d)
{
auto value = Value(Type::Float);
value.set<FloatType>(std::move(d));
setValue(std::move(value));
}
void number(std::int64_t i)
{
if (i < std::numeric_limits<std::int32_t>::min()
|| i > std::numeric_limits<std::int32_t>::max())
{
// https://spec.graphql.org/October2021/#sec-Int
number(static_cast<double>(i));
}
else
{
static_assert(sizeof(std::int32_t) == sizeof(IntType),
"GraphQL only supports 32-bit signed integers");
auto value = Value(Type::Int);
value.set<IntType>(static_cast<std::int32_t>(i));
setValue(std::move(value));
}
}
void number(std::uint64_t i)
{
if (i > static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()))
{
// https://spec.graphql.org/October2021/#sec-Int
number(static_cast<double>(i));
}
else
{
number(static_cast<std::int64_t>(i));
}
}
void string(std::string&& str)
{
setValue(Value(std::move(str)).from_json());
}
void begin_array()
{
_responseStack.push_back(Value(Type::List));
}
void element()
{
}
void end_array()
{
setValue(getResponse());
}
void begin_object()
{
_responseStack.push_back(Value(Type::Map));
}
void key(std::string&& str)
{
_keyStack.push_back(std::move(str));
}
void member()
{
}
void end_object()
{
setValue(getResponse());
}
private:
void setValue(Value&& value)
{
switch (_responseStack.back().type())
{
case Type::Map:
_responseStack.back().emplace_back(std::move(_keyStack.back()), std::move(value));
_keyStack.pop_back();
break;
case Type::List:
_responseStack.back().emplace_back(std::move(value));
break;
default:
_responseStack.back() = std::move(value);
break;
}
}
std::vector<std::string> _keyStack;
std::vector<Value> _responseStack;
};
Value parseJSON(const std::string& json)
{
ResponseHandler handler;
tao::json::events::from_string(handler, json);
return handler.getResponse();
}
} // namespace graphql::response