-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonReader.Async.cs
194 lines (173 loc) · 8.24 KB
/
JsonReader.Async.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
// Copyright (c) 2007 James Newton-King. All rights reserved.
// Use of this source code is governed by The MIT License,
// as found in the license.md file.
namespace Argon;
public abstract partial class JsonReader
: IAsyncDisposable
{
ValueTask IAsyncDisposable.DisposeAsync()
{
try
{
Dispose(true);
return default;
}
catch (Exception exc)
{
return ValueTask.FromException(exc);
}
}
/// <summary>
/// Asynchronously reads the next JSON token from the source.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns <c>true</c> if the next token was read successfully; <c>false</c> if there are no more tokens to read.
/// </returns>
public virtual Task<bool> ReadAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<bool>() ?? Read().ToAsync();
/// <summary>
/// Asynchronously skips the children of the current token.
/// </summary>
public async Task SkipAsync(CancellationToken cancellation = default)
{
if (TokenType == JsonToken.PropertyName)
{
await ReadAsync(cancellation).ConfigureAwait(false);
}
if (TokenType.IsStartToken())
{
var depth = Depth;
while (await ReadAsync(cancellation).ConfigureAwait(false) && depth < Depth)
{
}
}
}
internal async Task ReaderReadAndAssertAsync(CancellationToken cancellation)
{
if (!await ReadAsync(cancellation).ConfigureAwait(false))
{
throw CreateUnexpectedEndException();
}
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="bool" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="bool" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<bool?> ReadAsBooleanAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<bool?>() ?? Task.FromResult(ReadAsBoolean());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="byte" />[].
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="byte" />[]. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<byte[]?> ReadAsBytesAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<byte[]?>() ?? Task.FromResult(ReadAsBytes());
internal async Task<byte[]?> ReadArrayIntoByteArrayAsync(CancellationToken cancellation)
{
var buffer = new List<byte>();
while (true)
{
if (!await ReadAsync(cancellation).ConfigureAwait(false))
{
SetToken(JsonToken.None);
}
if (ReadArrayElementIntoByteArrayReportDone(buffer))
{
var d = buffer.ToArray();
SetToken(JsonToken.Bytes, d, false);
return d;
}
}
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="DateTime" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="DateTime" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<DateTime?> ReadAsDateTimeAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<DateTime?>() ?? Task.FromResult(ReadAsDateTime());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="DateTimeOffset" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="DateTimeOffset" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<DateTimeOffset?> ReadAsDateTimeOffsetAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<DateTimeOffset?>() ?? Task.FromResult(ReadAsDateTimeOffset());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="decimal" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="decimal" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<decimal?> ReadAsDecimalAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<decimal?>() ?? Task.FromResult(ReadAsDecimal());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="double" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="double" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<double?> ReadAsDoubleAsync(CancellationToken cancellation = default) =>
Task.FromResult(ReadAsDouble());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}" /> of <see cref="int" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="Nullable{T}" /> of <see cref="int" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<int?> ReadAsInt32Async(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<int?>() ?? Task.FromResult(ReadAsInt32());
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="string" />.
/// </summary>
/// <returns>
/// A <see cref="Task{TResult}" /> that represents the asynchronous read. The <see cref="Task{TResult}.Result" />
/// property returns the <see cref="string" />. This result will be <c>null</c> at the end of an array.
/// </returns>
public virtual Task<string?> ReadAsStringAsync(CancellationToken cancellation = default) =>
cancellation.CancelIfRequestedAsync<string?>() ?? Task.FromResult(ReadAsString());
internal async Task<bool> ReadAndMoveToContentAsync(CancellationToken cancellation) =>
await ReadAsync(cancellation).ConfigureAwait(false) && await MoveToContentAsync(cancellation).ConfigureAwait(false);
internal Task<bool> MoveToContentAsync(CancellationToken cancellation)
{
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
return MoveToContentFromNonContentAsync(cancellation);
default:
return AsyncUtils.True;
}
}
async Task<bool> MoveToContentFromNonContentAsync(CancellationToken cancellation)
{
while (true)
{
if (!await ReadAsync(cancellation).ConfigureAwait(false))
{
return false;
}
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
break;
default:
return true;
}
}
}
}