-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PipeWriter overloads to Json #101461
Merged
Merged
Add PipeWriter overloads to Json #101461
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cb407b9
Add PipeWriter overloads to Json
BrennanConroy 7512f26
test
BrennanConroy 4efef1f
organize and tests
BrennanConroy 5772e3f
cleanup
BrennanConroy 94c4be4
FlushThreshold
BrennanConroy 04f53d3
cleanup
BrennanConroy 9448b38
system.buffer is special
BrennanConroy eb2c628
tests
BrennanConroy dde0db0
fixup
BrennanConroy 2c18119
de-dupe
BrennanConroy 530c0c0
Merge branch 'main' into brecon/pipe
BrennanConroy 95cb59c
minor
BrennanConroy ad097e9
fb
BrennanConroy 2e0d69d
no async!
BrennanConroy d617b86
Merge branch 'main' into brecon/pipe
eiriktsarpalis 6a6d91b
Update src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Se…
eiriktsarpalis 5f622c8
Merge branch 'main' into brecon/pipe
eiriktsarpalis cdcc77e
Merge branch 'main' into brecon/pipe
eiriktsarpalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
...ibraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.Pipe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO.Pipelines; | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json.Serialization.Metadata; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Text.Json | ||
{ | ||
public static partial class JsonSerializer | ||
{ | ||
/// <summary> | ||
/// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.PipeWriter"/>. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of the value to serialize.</typeparam> | ||
/// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param> | ||
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> | ||
/// <returns>A task that represents the asynchronous write operation.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="utf8Json"/> is <see langword="null"/>. | ||
/// </exception> | ||
public static Task SerializeAsync<TValue>( | ||
PipeWriter utf8Json, | ||
TValue value, | ||
JsonTypeInfo<TValue> jsonTypeInfo, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (utf8Json is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json)); | ||
} | ||
if (jsonTypeInfo is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo)); | ||
} | ||
|
||
jsonTypeInfo.EnsureConfigured(); | ||
return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.PipeWriter"/>. | ||
/// </summary> | ||
/// <typeparam name="TValue">The type of the value to serialize.</typeparam> | ||
/// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="options">Options to control the conversion behavior.</param> | ||
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> | ||
/// <returns>A task that represents the asynchronous write operation.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="utf8Json"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <exception cref="NotSupportedException"> | ||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <typeparamref name="TValue"/> or its serializable members. | ||
/// </exception> | ||
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] | ||
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] | ||
public static Task SerializeAsync<TValue>( | ||
PipeWriter utf8Json, | ||
TValue value, | ||
JsonSerializerOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (utf8Json is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json)); | ||
} | ||
|
||
JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); | ||
return jsonTypeInfo.SerializeAsync(utf8Json, value, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.PipeWriter"/>. | ||
/// </summary> | ||
/// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="jsonTypeInfo">Metadata about the type to convert.</param> | ||
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> | ||
/// <returns>A task that represents the asynchronous write operation.</returns> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="utf8Json"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <exception cref="InvalidCastException"> | ||
/// <paramref name="value"/> does not match the type of <paramref name="jsonTypeInfo"/>. | ||
/// </exception> | ||
public static Task SerializeAsync( | ||
PipeWriter utf8Json, | ||
object? value, | ||
JsonTypeInfo jsonTypeInfo, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (utf8Json is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json)); | ||
} | ||
|
||
if (jsonTypeInfo is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo)); | ||
} | ||
|
||
jsonTypeInfo.EnsureConfigured(); | ||
return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.PipeWriter"/>. | ||
/// </summary> | ||
/// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> | ||
/// <param name="context">A metadata provider for serializable types.</param> | ||
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> | ||
/// <returns>A task that represents the asynchronous write operation.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. | ||
/// </exception> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="utf8Json"/>, <paramref name="inputType"/>, or <paramref name="context"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <exception cref="NotSupportedException"> | ||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <paramref name="inputType"/> or its serializable members. | ||
/// </exception> | ||
public static Task SerializeAsync( | ||
PipeWriter utf8Json, | ||
object? value, | ||
Type inputType, | ||
JsonSerializerContext context, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (utf8Json is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json)); | ||
} | ||
|
||
if (context is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(context)); | ||
} | ||
|
||
ValidateInputType(value, inputType); | ||
JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType); | ||
|
||
return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Converts the provided value to UTF-8 encoded JSON text and write it to the <see cref="System.IO.Pipelines.PipeWriter"/>. | ||
/// </summary> | ||
/// <param name="utf8Json">The UTF-8 <see cref="System.IO.Pipelines.PipeWriter"/> to write to.</param> | ||
/// <param name="value">The value to convert.</param> | ||
/// <param name="inputType">The type of the <paramref name="value"/> to convert.</param> | ||
/// <param name="options">Options to control the conversion behavior.</param> | ||
/// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> | ||
/// <returns>A task that represents the asynchronous write operation.</returns> | ||
/// <exception cref="ArgumentException"> | ||
/// <paramref name="inputType"/> is not compatible with <paramref name="value"/>. | ||
/// </exception> | ||
/// <exception cref="ArgumentNullException"> | ||
/// <paramref name="utf8Json"/> or <paramref name="inputType"/> is <see langword="null"/>. | ||
/// </exception> | ||
/// <exception cref="NotSupportedException"> | ||
/// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> | ||
/// for <paramref name="inputType"/> or its serializable members. | ||
/// </exception> | ||
[RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] | ||
[RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] | ||
public static Task SerializeAsync( | ||
PipeWriter utf8Json, | ||
object? value, | ||
Type inputType, | ||
JsonSerializerOptions? options = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (utf8Json is null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(nameof(utf8Json)); | ||
} | ||
|
||
ValidateInputType(value, inputType); | ||
JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, inputType); | ||
|
||
return jsonTypeInfo.SerializeAsObjectAsync(utf8Json, value, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do these properties do, out of curiosity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This property makes the assembly get included in the transport package for aspnetcore. Aspnetcore includes everything from that transport package in their shared framework.
Given that you move this assembly inbox into Microsoft.NETCore.App, this change is correct.