Skip to content

[API Proposal]: JsonSerializerOptions.GetTypeInfo<T> #118468

@agocke

Description

@agocke

Background and motivation

There's already a GetTypeInfo method that takes a System.Type, but a really common case is to downcast to the generic version. This API makes it simple to perform the same operation without introducing potential errors.

API Proposal

namespace System.Text.Json;

public static class JsonSerializerOptionsExtensions
{
    public static System.Text.Json.Serialization.Metadata.JsonTypeInfo<T> GetTypeInfo<T>(this JsonSerializerOptions options) { throw null; }
}

API Usage

See, for example,

var jsonTypeInfo = (JsonTypeInfo<T>)JsonSerializerOptions.Default.GetTypeInfo(typeof(T));

// Old use
var info = (JsonTypeInfo<string>)options.GetTypeInfo(typeof(string));

// New use
var info = options.GetTypeInfo<string>();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions