-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathJsonResumableConverterOfT.cs
50 lines (45 loc) · 1.75 KB
/
JsonResumableConverterOfT.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Text.Json.Serialization
{
/// <summary>
/// Base class for converters that are able to resume after reading or writing to a buffer.
/// This is used when the Stream-based serialization APIs are used.
/// </summary>
/// <typeparam name="T"></typeparam>
internal abstract class JsonResumableConverter<T> : JsonConverter<T>
{
public sealed override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
// Bridge from resumable to value converters.
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
ReadStack state = default;
state.Initialize(typeToConvert, options, supportContinuation: false);
TryRead(ref reader, typeToConvert, options, ref state, out T? value);
return value;
}
public sealed override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
// Bridge from resumable to value converters.
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
WriteStack state = default;
state.Initialize(typeof(T), options, supportContinuation: false);
try
{
TryWrite(writer, value, options, ref state);
}
catch
{
state.DisposePendingDisposablesOnException();
throw;
}
}
public sealed override bool HandleNull => false;
}
}