-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
JsonQueryResultSerializer.cs
419 lines (340 loc) · 11.5 KB
/
JsonQueryResultSerializer.cs
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using HotChocolate.Utilities;
namespace HotChocolate.Execution.Serialization
{
public sealed class JsonQueryResultSerializer
: IQueryResultSerializer
{
private const string _data = "data";
private const string _errors = "errors";
private const string _extensions = "extensions";
private const string _message = "message";
private const string _locations = "locations";
private const string _path = "path";
private const string _line = "line";
private const string _column = "column";
private readonly JsonWriterOptions _options;
public JsonQueryResultSerializer(bool indented = false)
{
_options = new JsonWriterOptions { Indented = indented };
}
public unsafe string Serialize(IQueryResult result)
{
using var buffer = new ArrayWriter();
Serialize(result, buffer);
fixed (byte* b = buffer.GetInternalBuffer())
{
return Encoding.UTF8.GetString(b, buffer.Length);
}
}
internal void Serialize(IQueryResult result, IBufferWriter<byte> writer)
{
using var jsonWriter = new Utf8JsonWriter(writer, _options);
WriteResult(jsonWriter, result);
jsonWriter.Flush();
}
public async Task SerializeAsync(
IQueryResult result,
Stream stream,
CancellationToken cancellationToken = default)
{
if (result is null)
{
throw new ArgumentNullException(nameof(result));
}
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
await using var writer = new Utf8JsonWriter(stream, _options);
WriteResult(writer, result);
await writer.FlushAsync(cancellationToken);
}
private static void WriteResult(Utf8JsonWriter writer, IQueryResult result)
{
writer.WriteStartObject();
WritePatchInfo(writer, result);
WriteErrors(writer, result.Errors);
WriteData(writer, result.Data);
WriteExtensions(writer, result.Extensions);
WriteHasNext(writer, result);
writer.WriteEndObject();
}
private static void WritePatchInfo(
Utf8JsonWriter writer,
IQueryResult result)
{
if (result.Label is not null)
{
writer.WriteString("label", result.Label);
}
if (result.Path is not null)
{
WritePath(writer, result.Path);
}
}
private static void WriteHasNext(
Utf8JsonWriter writer,
IQueryResult result)
{
if (result.HasNext.HasValue)
{
writer.WriteBoolean("hasNext", result.HasNext.Value);
}
}
private static void WriteData(
Utf8JsonWriter writer,
IReadOnlyDictionary<string, object?>? data)
{
if (data is { } && data.Count > 0)
{
writer.WritePropertyName(_data);
if (data is IResultMap resultMap)
{
WriteResultMap(writer, resultMap);
}
else
{
WriteDictionary(writer, data);
}
}
}
private static void WriteErrors(Utf8JsonWriter writer, IReadOnlyList<IError>? errors)
{
if (errors is { } && errors.Count > 0)
{
writer.WritePropertyName(_errors);
writer.WriteStartArray();
for (var i = 0; i < errors.Count; i++)
{
WriteError(writer, errors[i]);
}
writer.WriteEndArray();
}
}
private static void WriteError(Utf8JsonWriter writer, IError error)
{
writer.WriteStartObject();
writer.WriteString(_message, error.Message);
WriteLocations(writer, error.Locations);
WritePath(writer, error.Path);
WriteExtensions(writer, error.Extensions);
writer.WriteEndObject();
}
private static void WriteLocations(Utf8JsonWriter writer, IReadOnlyList<Location>? locations)
{
if (locations is { } && locations.Count > 0)
{
writer.WritePropertyName(_locations);
writer.WriteStartArray();
for (var i = 0; i < locations.Count; i++)
{
WriteLocation(writer, locations[i]);
}
writer.WriteEndArray();
}
}
private static void WriteLocation(Utf8JsonWriter writer, Location location)
{
writer.WriteStartObject();
writer.WriteNumber(_line, location.Line);
writer.WriteNumber(_column, location.Column);
writer.WriteEndObject();
}
private static void WritePath(Utf8JsonWriter writer, Path? path)
{
if (path is not null && path is not RootPathSegment)
{
writer.WritePropertyName(_path);
WritePathValue(writer, path);
}
}
private static void WritePathValue(Utf8JsonWriter writer, Path path)
{
if (path is RootPathSegment)
{
writer.WriteStartArray();
writer.WriteEndArray();
return;
}
writer.WriteStartArray();
IReadOnlyList<object> list = path.ToList();
for (var i = 0; i < list.Count; i++)
{
switch (list[i])
{
case NameString n:
writer.WriteStringValue(n.Value);
break;
case string s:
writer.WriteStringValue(s);
break;
case int n:
writer.WriteNumberValue(n);
break;
case short n:
writer.WriteNumberValue(n);
break;
case long n:
writer.WriteNumberValue(n);
break;
default:
writer.WriteStringValue(list[i].ToString());
break;
}
}
writer.WriteEndArray();
}
private static void WriteExtensions(
Utf8JsonWriter writer,
IReadOnlyDictionary<string, object?>? dict)
{
if (dict is { } && dict.Count > 0)
{
writer.WritePropertyName(_extensions);
WriteDictionary(writer, dict);
}
}
private static void WriteDictionary(
Utf8JsonWriter writer,
IReadOnlyDictionary<string, object?> dict)
{
writer.WriteStartObject();
foreach (KeyValuePair<string, object?> item in dict)
{
writer.WritePropertyName(item.Key);
WriteFieldValue(writer, item.Value);
}
writer.WriteEndObject();
}
private static void WriteResultMap(
Utf8JsonWriter writer,
IResultMap resultMap)
{
writer.WriteStartObject();
for (var i = 0; i < resultMap.Count; i++)
{
ResultValue value = resultMap[i];
if (value.HasValue)
{
writer.WritePropertyName(value.Name);
WriteFieldValue(writer, value.Value);
}
}
writer.WriteEndObject();
}
private static void WriteList(
Utf8JsonWriter writer,
IList list)
{
writer.WriteStartArray();
for (var i = 0; i < list.Count; i++)
{
WriteFieldValue(writer, list[i]);
}
writer.WriteEndArray();
}
private static void WriteResultMapList(
Utf8JsonWriter writer,
IResultMapList list)
{
writer.WriteStartArray();
for (var i = 0; i < list.Count; i++)
{
if (list[i] is { } m)
{
WriteResultMap(writer, m);
}
else
{
WriteFieldValue(writer, null);
}
}
writer.WriteEndArray();
}
private static void WriteFieldValue(
Utf8JsonWriter writer,
object? value)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
switch (value)
{
case IResultMap resultMap:
WriteResultMap(writer, resultMap);
break;
case IResultMapList resultMapList:
WriteResultMapList(writer, resultMapList);
break;
case IReadOnlyDictionary<string, object?> dict:
WriteDictionary(writer, dict);
break;
case IList list:
WriteList(writer, list);
break;
case IError error:
WriteError(writer, error);
break;
case string s:
writer.WriteStringValue(s);
break;
case byte b:
writer.WriteNumberValue(b);
break;
case short s:
writer.WriteNumberValue(s);
break;
case ushort s:
writer.WriteNumberValue(s);
break;
case int i:
writer.WriteNumberValue(i);
break;
case uint i:
writer.WriteNumberValue(i);
break;
case long l:
writer.WriteNumberValue(l);
break;
case ulong l:
writer.WriteNumberValue(l);
break;
case float f:
writer.WriteNumberValue(f);
break;
case double d:
writer.WriteNumberValue(d);
break;
case decimal d:
writer.WriteNumberValue(d);
break;
case bool b:
writer.WriteBooleanValue(b);
break;
case NameString n:
writer.WriteStringValue(n.Value);
break;
case Uri u:
writer.WriteStringValue(u.ToString());
break;
case Path p:
WritePathValue(writer, p);
break;
default:
writer.WriteStringValue(value.ToString());
break;
}
}
}
}