question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider trimmer friendly overloads for HttpRequestJsonExtensions / HttpResponseJsonExtensions

See original GitHub issue

Background 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, Mar 31, 2022

API review:

Discussion over whether context parameter is confusing. Could rename to jsonContext.

Will stick with context to be consistent with HttpClient JSON extension methods. Can change if there is feedback during .NET 7 previews.

0reactions
JamesNKcommented, Mar 31, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found