Skip to content

Commit 4028ca5

Browse files
committed
cleanup
1 parent aff9347 commit 4028ca5

File tree

6 files changed

+73
-145
lines changed

6 files changed

+73
-145
lines changed

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/IReadBufferState.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ internal interface IReadBufferState<TReadBufferState, TStream> : IDisposable
1414

1515
public abstract ReadOnlySequence<byte> Bytes { get; }
1616

17-
public abstract ValueTask<TReadBufferState> ReadAsync(TStream utf8Json, CancellationToken cancellationToken,
17+
public abstract ValueTask<TReadBufferState> ReadAsync(
18+
TStream utf8Json,
19+
CancellationToken cancellationToken,
1820
bool fillBuffer = true);
1921

2022
public abstract void Read(TStream utf8Json);

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Text.Json.Serialization;
8+
using System.Text.Json.Serialization.Converters;
79
using System.Text.Json.Serialization.Metadata;
810

911
namespace System.Text.Json
@@ -139,5 +141,42 @@ static void ThrowUnableToCastValue(object? value)
139141

140142
return (T?)value;
141143
}
144+
145+
private static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForRootLevelValueMode<T>(JsonTypeInfo<T> elementTypeInfo)
146+
{
147+
if (elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo != null)
148+
{
149+
return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo;
150+
}
151+
152+
var converter = new RootLevelListConverter<T>(elementTypeInfo);
153+
var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
154+
{
155+
ElementTypeInfo = elementTypeInfo,
156+
};
157+
158+
listTypeInfo.EnsureConfigured();
159+
elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo = listTypeInfo;
160+
return listTypeInfo;
161+
}
162+
163+
private static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForArrayMode<T>(JsonTypeInfo<T> elementTypeInfo)
164+
{
165+
if (elementTypeInfo._asyncEnumerableArrayTypeInfo != null)
166+
{
167+
return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableArrayTypeInfo;
168+
}
169+
170+
var converter = new ListOfTConverter<List<T>, T>();
171+
var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
172+
{
173+
CreateObject = static () => new List<T?>(),
174+
ElementTypeInfo = elementTypeInfo,
175+
};
176+
177+
listTypeInfo.EnsureConfigured();
178+
elementTypeInfo._asyncEnumerableArrayTypeInfo = listTypeInfo;
179+
return listTypeInfo;
180+
}
142181
}
143182
}

src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Pipe.cs

Lines changed: 27 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.IO.Pipelines;
88
using System.Runtime.CompilerServices;
99
using System.Text.Json.Serialization;
10-
using System.Text.Json.Serialization.Converters;
1110
using System.Text.Json.Serialization.Metadata;
1211
using System.Threading;
1312
using System.Threading.Tasks;
@@ -18,7 +17,7 @@ public static partial class JsonSerializer
1817
{
1918
/// <summary>
2019
/// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
21-
/// The Stream will be read to completion.
20+
/// The PipeReader will be read to completion.
2221
/// </summary>
2322
/// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
2423
/// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
@@ -33,7 +32,7 @@ public static partial class JsonSerializer
3332
/// <exception cref="JsonException">
3433
/// The JSON is invalid,
3534
/// <typeparamref name="TValue"/> is not compatible with the JSON,
36-
/// or when there is remaining data in the Stream.
35+
/// or when there is remaining data in the PipeReader.
3736
/// </exception>
3837
/// <exception cref="NotSupportedException">
3938
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
@@ -46,18 +45,15 @@ public static partial class JsonSerializer
4645
JsonSerializerOptions? options = null,
4746
CancellationToken cancellationToken = default)
4847
{
49-
if (utf8Json is null)
50-
{
51-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
52-
}
48+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
5349

5450
JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
5551
return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
5652
}
5753

5854
/// <summary>
5955
/// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>.
60-
/// The Stream will be read to completion.
56+
/// The PipeReader will be read to completion.
6157
/// </summary>
6258
/// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam>
6359
/// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns>
@@ -72,29 +68,23 @@ public static partial class JsonSerializer
7268
/// <exception cref="JsonException">
7369
/// The JSON is invalid,
7470
/// <typeparamref name="TValue"/> is not compatible with the JSON,
75-
/// or when there is remaining data in the Stream.
71+
/// or when there is remaining data in the PipeReader.
7672
/// </exception>
7773
public static ValueTask<TValue?> DeserializeAsync<TValue>(
7874
PipeReader utf8Json,
7975
JsonTypeInfo<TValue> jsonTypeInfo,
8076
CancellationToken cancellationToken = default)
8177
{
82-
if (utf8Json is null)
83-
{
84-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
85-
}
86-
if (jsonTypeInfo is null)
87-
{
88-
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
89-
}
78+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
79+
ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
9080

9181
jsonTypeInfo.EnsureConfigured();
9282
return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken);
9383
}
9484

9585
/// <summary>
9686
/// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref name="jsonTypeInfo"/>.
97-
/// The Stream will be read to completion.
87+
/// The PipeReader will be read to completion.
9888
/// </summary>
9989
/// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns>
10090
/// <param name="utf8Json">JSON data to parse.</param>
@@ -107,29 +97,23 @@ public static partial class JsonSerializer
10797
/// </exception>
10898
/// <exception cref="JsonException">
10999
/// The JSON is invalid,
110-
/// or when there is remaining data in the Stream.
100+
/// or when there is remaining data in the PipeReader.
111101
/// </exception>
112102
public static ValueTask<object?> DeserializeAsync(
113103
PipeReader utf8Json,
114104
JsonTypeInfo jsonTypeInfo,
115105
CancellationToken cancellationToken = default)
116106
{
117-
if (utf8Json is null)
118-
{
119-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
120-
}
121-
if (jsonTypeInfo is null)
122-
{
123-
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
124-
}
107+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
108+
ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
125109

126110
jsonTypeInfo.EnsureConfigured();
127111
return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
128112
}
129113

130114
/// <summary>
131115
/// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
132-
/// The Stream will be read to completion.
116+
/// The PipeReader will be read to completion.
133117
/// </summary>
134118
/// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
135119
/// <param name="utf8Json">JSON data to parse.</param>
@@ -144,7 +128,7 @@ public static partial class JsonSerializer
144128
/// <exception cref="JsonException">
145129
/// The JSON is invalid,
146130
/// the <paramref name="returnType"/> is not compatible with the JSON,
147-
/// or when there is remaining data in the Stream.
131+
/// or when there is remaining data in the PipeReader.
148132
/// </exception>
149133
/// <exception cref="NotSupportedException">
150134
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
@@ -160,26 +144,17 @@ public static partial class JsonSerializer
160144
JsonSerializerContext context,
161145
CancellationToken cancellationToken = default)
162146
{
163-
if (utf8Json is null)
164-
{
165-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
166-
}
167-
if (returnType is null)
168-
{
169-
ThrowHelper.ThrowArgumentNullException(nameof(returnType));
170-
}
171-
if (context is null)
172-
{
173-
ThrowHelper.ThrowArgumentNullException(nameof(context));
174-
}
147+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
148+
ArgumentNullException.ThrowIfNull(returnType, nameof(returnType));
149+
ArgumentNullException.ThrowIfNull(context, nameof(context));
175150

176151
JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType);
177152
return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
178153
}
179154

180155
/// <summary>
181156
/// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>.
182-
/// The Stream will be read to completion.
157+
/// The PipeReader will be read to completion.
183158
/// </summary>
184159
/// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns>
185160
/// <param name="utf8Json">JSON data to parse.</param>
@@ -194,7 +169,7 @@ public static partial class JsonSerializer
194169
/// <exception cref="JsonException">
195170
/// The JSON is invalid,
196171
/// the <paramref name="returnType"/> is not compatible with the JSON,
197-
/// or when there is remaining data in the Stream.
172+
/// or when there is remaining data in the PipeReader.
198173
/// </exception>
199174
/// <exception cref="NotSupportedException">
200175
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/>
@@ -208,14 +183,8 @@ public static partial class JsonSerializer
208183
JsonSerializerOptions? options = null,
209184
CancellationToken cancellationToken = default)
210185
{
211-
if (utf8Json is null)
212-
{
213-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
214-
}
215-
if (returnType is null)
216-
{
217-
ThrowHelper.ThrowArgumentNullException(nameof(returnType));
218-
}
186+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
187+
ArgumentNullException.ThrowIfNull(returnType, nameof(returnType));
219188

220189
JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType);
221190
return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken);
@@ -277,9 +246,9 @@ public static partial class JsonSerializer
277246
/// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>.
278247
/// </exception>
279248
/// <remarks>
280-
/// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of
249+
/// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the PipeReader as a sequence of
281250
/// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="TValue"/>.
282-
/// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array and
251+
/// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the PipeReader as a JSON array and
283252
/// attempts to serialize each element into <typeparamref name="TValue"/>.
284253
/// </remarks>
285254
public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>(
@@ -288,15 +257,8 @@ public static partial class JsonSerializer
288257
bool topLevelValues,
289258
CancellationToken cancellationToken = default)
290259
{
291-
if (utf8Json is null)
292-
{
293-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
294-
}
295-
296-
if (jsonTypeInfo is null)
297-
{
298-
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
299-
}
260+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
261+
ArgumentNullException.ThrowIfNull(jsonTypeInfo, nameof(jsonTypeInfo));
300262

301263
jsonTypeInfo.EnsureConfigured();
302264
return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
@@ -316,9 +278,9 @@ public static partial class JsonSerializer
316278
/// <paramref name="utf8Json"/> is <see langword="null"/>.
317279
/// </exception>
318280
/// <remarks>
319-
/// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of
281+
/// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the PipeReader as a sequence of
320282
/// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="TValue"/>.
321-
/// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array and
283+
/// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the PipeReader as a JSON array and
322284
/// attempts to serialize each element into <typeparamref name="TValue"/>.
323285
/// </remarks>
324286
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)]
@@ -329,10 +291,7 @@ public static partial class JsonSerializer
329291
JsonSerializerOptions? options = null,
330292
CancellationToken cancellationToken = default)
331293
{
332-
if (utf8Json is null)
333-
{
334-
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
335-
}
294+
ArgumentNullException.ThrowIfNull(utf8Json, nameof(utf8Json));
336295

337296
JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options);
338297
return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken);
@@ -395,51 +354,13 @@ public static partial class JsonSerializer
395354

396355
list.Clear();
397356
}
398-
399357
} while (!success);
400358
}
401359
finally
402360
{
403361
bufferState.Dispose();
404362
}
405363
}
406-
407-
static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForArrayMode(JsonTypeInfo<T> elementTypeInfo)
408-
{
409-
if (elementTypeInfo._asyncEnumerableArrayTypeInfo != null)
410-
{
411-
return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableArrayTypeInfo;
412-
}
413-
414-
var converter = new ListOfTConverter<List<T>, T>();
415-
var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
416-
{
417-
CreateObject = static () => new List<T?>(),
418-
ElementTypeInfo = elementTypeInfo,
419-
};
420-
421-
listTypeInfo.EnsureConfigured();
422-
elementTypeInfo._asyncEnumerableArrayTypeInfo = listTypeInfo;
423-
return listTypeInfo;
424-
}
425-
426-
static JsonTypeInfo<List<T?>> GetOrAddListTypeInfoForRootLevelValueMode(JsonTypeInfo<T> elementTypeInfo)
427-
{
428-
if (elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo != null)
429-
{
430-
return (JsonTypeInfo<List<T?>>)elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo;
431-
}
432-
433-
var converter = new RootLevelListConverter<T>(elementTypeInfo);
434-
var listTypeInfo = new JsonTypeInfo<List<T?>>(converter, elementTypeInfo.Options)
435-
{
436-
ElementTypeInfo = elementTypeInfo,
437-
};
438-
439-
listTypeInfo.EnsureConfigured();
440-
elementTypeInfo._asyncEnumerableRootLevelValueTypeInfo = listTypeInfo;
441-
return listTypeInfo;
442-
}
443364
}
444365
}
445366
}

0 commit comments

Comments
 (0)