-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathJSONResponse.cpp
232 lines (186 loc) · 3.96 KB
/
JSONResponse.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "graphqlservice/JSONResponse.h"
#define RAPIDJSON_NAMESPACE graphql::rapidjson
#include <rapidjson/rapidjson.h>
#include <rapidjson/reader.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
#include <limits>
#include <stdexcept>
#include <vector>
namespace graphql::response {
class StringWriter
{
public:
StringWriter(rapidjson::StringBuffer& buffer)
: _writer { buffer }
{
}
void start_object()
{
_writer.StartObject();
}
void add_member(const std::string& key)
{
_writer.Key(key.c_str());
}
void end_object()
{
_writer.EndObject();
}
void start_array()
{
_writer.StartArray();
}
void end_arrary()
{
_writer.EndArray();
}
void write_null()
{
_writer.Null();
}
void write_string(const std::string& value)
{
_writer.String(value.c_str());
}
void write_bool(bool value)
{
_writer.Bool(value);
}
void write_int(int value)
{
_writer.Int(value);
}
void write_float(double value)
{
_writer.Double(value);
}
private:
rapidjson::Writer<rapidjson::StringBuffer> _writer;
};
std::string toJSON(Value&& response)
{
rapidjson::StringBuffer buffer;
Writer writer { std::make_unique<StringWriter>(buffer) };
writer.write(std::move(response));
return buffer.GetString();
}
struct ResponseHandler : rapidjson::BaseReaderHandler<rapidjson::UTF8<>, ResponseHandler>
{
ResponseHandler()
{
// Start with a single null value.
_responseStack.push_back({});
}
Value getResponse()
{
auto response = std::move(_responseStack.back());
_responseStack.pop_back();
return response;
}
bool Null()
{
setValue(Value());
return true;
}
bool Bool(bool b)
{
setValue(Value(b));
return true;
}
bool Int(int i)
{
// https://spec.graphql.org/October2021/#sec-Int
static_assert(sizeof(i) == 4, "GraphQL only supports 32-bit signed integers");
auto value = Value(Type::Int);
value.set<IntType>(std::move(i));
setValue(std::move(value));
return true;
}
bool Uint(unsigned int i)
{
if (i > static_cast<unsigned int>(std::numeric_limits<int>::max()))
{
// https://spec.graphql.org/October2021/#sec-Int
return Double(static_cast<double>(i));
}
return Int(static_cast<int>(i));
}
bool Int64(int64_t i)
{
// https://spec.graphql.org/October2021/#sec-Int
return Double(static_cast<double>(i));
}
bool Uint64(uint64_t i)
{
// https://spec.graphql.org/October2021/#sec-Int
return Double(static_cast<double>(i));
}
bool Double(double d)
{
auto value = Value(Type::Float);
value.set<FloatType>(std::move(d));
setValue(std::move(value));
return true;
}
bool String(const Ch* str, rapidjson::SizeType /*length*/, bool /*copy*/)
{
setValue(Value(std::string(str)).from_json());
return true;
}
bool StartObject()
{
_responseStack.push_back(Value(Type::Map));
return true;
}
bool Key(const Ch* str, rapidjson::SizeType /*length*/, bool /*copy*/)
{
_keyStack.push_back(str);
return true;
}
bool EndObject(rapidjson::SizeType /*count*/)
{
setValue(getResponse());
return true;
}
bool StartArray()
{
_responseStack.push_back(Value(Type::List));
return true;
}
bool EndArray(rapidjson::SizeType /*count*/)
{
setValue(getResponse());
return true;
}
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;
rapidjson::Reader reader;
rapidjson::StringStream ss(json.c_str());
reader.Parse(ss, handler);
return handler.getResponse();
}
} // namespace graphql::response