Skip to content

[API Proposal]: add JsonSerializerOptions.Web for JsonSerializerOptions #92181

@WeihanLi

Description

@WeihanLi

Background and motivation

Propose a public JsonSerializerOptions for the web, it could help avoid some duplicate cache

For example, if we extend the AsyncEnumerable for legacy frameworks, we could use the shared JsonSerializerOptions.Web without creating a new JsonSerializerOptions, and the original cache in runtime/aspnetcore etc would also benifit from this

new JsonSerializerOptions(JsonSerializerDefaults.Web) usage from grep:

https://grep.app/search?q=JsonSerializerOptions%28JsonSerializerDefaults.Web%29&filter[lang][0]=C%23

This may also help with the new analyzer rule

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1869

API Proposal

namespace System.Text.Json;

public sealed partial class JsonSerializerOptions
{
    public static JsonSerializerOptions Default { get; }
+   public static JsonSerializerOptions Web { get; }
}

API Usage

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:5297");
await using var responseStream = await httpClient.GetStreamAsync("api/Jobs");
// var stream = JsonSerializer.DeserializeAsyncEnumerable<Job>(responseStream, new JsonSerializerOptions(JsonSerializerDefaults.Web));
var stream = JsonSerializer.DeserializeAsyncEnumerable<Job>(responseStream, JsonSerializerOptions.Web);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions