Consider trimmer friendly overloads for HttpRequestJsonExtensions / HttpResponseJsonExtensions
See original GitHub issueBackground and Motivation
Proposed API
ASP.NET Core provides APIs for reading JSON from the HTTP Request body and writing JSON to the HTTP Response body via extension methods to HttpRequest
and HttpResponse
which closely resembles the System.Text.Json’s Stream (de)serialization APIs. This issue suggests adding JsonSerializerContext
/ JsonTypeInfo<T>
based overloads to continue matching STJ’s APIs which have the added advantage of being trimmer friendly.
namespace Microsoft.AspNetCore.Http;
public static class HttpRequestJsonExtensions
{
public static async ValueTask<object?> ReadFromJsonAsync(this HttpRequest request, Type type, JsonSerializerOptions? options, CancellationToken cancellationToken = default);
+ public static async ValueTask<object?> ReadFromJsonAsync(this HttpRequest request, Type type, JsonSerializerContext context, CancellationToken cancellationToken = default);
public static async ValueTask<TValue?> ReadFromJsonAsync<TValue>(this HttpRequest request, JsonSerializerOptions? options, CancellationToken cancellationToken = default);
+ public static async ValueTask<TValue?> ReadFromJsonAsync<TValue>(this HttpRequest request, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default);
}
public static partial class HttpResponseJsonExtensions
{
public static Task WriteAsJsonAsync<TValue>(this HttpResponse response, TValue value, JsonSerializerOptions? options, string? contentType, CancellationToken cancellationToken = default);
+ public static Task WriteAsJsonAsync<TValue>(this HttpResponse response, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, string? contentType = default, CancellationToken cancellationToken = default);
public static Task WriteAsJsonAsync(this HttpResponse response, object? value, Type type, JsonSerializerOptions? options, string? contentType, CancellationToken cancellationToken = default);
+ public static Task WriteAsJsonAsync(this HttpResponse response, object? value, Type type, JsonSerializerContext context, string? contentType = default, CancellationToken cancellationToken = default);
}
// Note that in the two proposed write APIs, `contentType` is an optional parameter unlike the existing overloads which relies on multiple overloads for optionality.
Usage Examples
Usage is similar to JsonSerializer
’s use for JsonSerializerContext - https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-source-generation?pivots=dotnet-6-0#jsonserializer-methods-that-use-source-generation
var myModel = await request.ReadFromJsonAsync(typeof(MyModel), MyJsonContext.Default);
OR
var myModel = await request.ReadFromJsonAsync(MyJsonContext.Default.MyModel);
await response.WriteAsJsonAsync(myModel, typeof(MyModel), MyJsonContext.Defult);
OR
await response.WriteAsJsonAsync(myModel, MyJsonContext.Defult.MyModel);
Alternative Designs
- Users can always use the System.Text.Json APIs directly
Risks
n/a
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
API review:
Discussion over whether
context
parameter is confusing. Could rename tojsonContext
.Will stick with
context
to be consistent with HttpClient JSON extension methods. Can change if there is feedback during .NET 7 previews.Done in https://github.com/dotnet/aspnetcore/pull/40561