Skip to content

Commit f624eca

Browse files
authored
Improve allocations from JsonResultSerializer (#6650)
1 parent cada109 commit f624eca

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/HotChocolate/Core/src/Execution/Serialization/JsonQueryResultSerializer.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ public sealed class JsonQueryResultSerializer : IQueryResultSerializer
3232
/// </param>
3333
public JsonQueryResultSerializer(bool indented = false, JavaScriptEncoder? encoder = null)
3434
{
35-
_options = new JsonWriterOptions { Indented = indented, Encoder = encoder };
35+
_options = new JsonWriterOptions
36+
{
37+
Indented = indented,
38+
Encoder = encoder ?? JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
39+
};
3640
}
3741

3842
public unsafe string Serialize(IQueryResult result)
3943
{
44+
if (result is null)
45+
{
46+
throw new ArgumentNullException(nameof(result));
47+
}
48+
4049
using var buffer = new ArrayWriter();
4150

4251
Serialize(result, buffer);
@@ -49,14 +58,29 @@ public unsafe string Serialize(IQueryResult result)
4958

5059
/// <inheritdoc />
5160
public void Serialize(IQueryResult result, IBufferWriter<byte> writer)
61+
{
62+
if (result is null)
63+
{
64+
throw new ArgumentNullException(nameof(result));
65+
}
66+
67+
if (writer is null)
68+
{
69+
throw new ArgumentNullException(nameof(writer));
70+
}
71+
72+
SerializeInternal(result, writer);
73+
}
74+
75+
private void SerializeInternal(IQueryResult result, IBufferWriter<byte> writer)
5276
{
5377
using var jsonWriter = new Utf8JsonWriter(writer, _options);
5478
WriteResult(jsonWriter, result);
5579
jsonWriter.Flush();
5680
}
5781

5882
/// <inheritdoc />
59-
public async Task SerializeAsync(
83+
public Task SerializeAsync(
6084
IQueryResult result,
6185
Stream stream,
6286
CancellationToken cancellationToken = default)
@@ -71,11 +95,32 @@ public async Task SerializeAsync(
7195
throw new ArgumentNullException(nameof(stream));
7296
}
7397

74-
await using var writer = new Utf8JsonWriter(stream, _options);
98+
return SerializeInternalAsync(result, stream, cancellationToken);
99+
}
100+
101+
private async Task SerializeInternalAsync(
102+
IQueryResult result,
103+
Stream outputStream,
104+
CancellationToken cancellationToken = default)
105+
{
106+
await using var writer = new Utf8JsonWriter(outputStream, _options);
75107

76108
WriteResult(writer, result);
109+
using var buffer = new ArrayWriter();
110+
SerializeInternal(result, buffer);
111+
112+
#if NETSTANDARD2_0
113+
await outputStream
114+
.WriteAsync(buffer.GetInternalBuffer(), 0, buffer.Length, cancellationToken)
115+
.ConfigureAwait(false);
116+
#else
117+
await outputStream
118+
.WriteAsync(buffer.Body, cancellationToken)
119+
.ConfigureAwait(false);
120+
#endif
77121

78122
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
123+
await outputStream.FlushAsync(cancellationToken).ConfigureAwait(false);
79124
}
80125

81126
private void WriteResult(Utf8JsonWriter writer, IQueryResult result)

0 commit comments

Comments
 (0)