Skip to content

Add extension method for JsonSerializerOptions.GetTypeInfo - #118469

Closed
agocke wants to merge 8 commits into
dotnet:mainfrom
agocke:gti-generic
Closed

Add extension method for JsonSerializerOptions.GetTypeInfo#118469
agocke wants to merge 8 commits into
dotnet:mainfrom
agocke:gti-generic

Conversation

@agocke

@agocke agocke commented Aug 6, 2025

Copy link
Copy Markdown
Member

Fixes #118468

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@agocke
agocke marked this pull request as ready for review August 12, 2025 19:27
Copilot AI review requested due to automatic review settings August 12, 2025 19:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds generic overloads for the GetTypeInfo and TryGetTypeInfo methods in JsonSerializerOptions to provide type-safe alternatives that return JsonTypeInfo<T> instead of the base JsonTypeInfo type. This enhancement improves the developer experience by eliminating the need for manual casting when working with strongly-typed JSON serialization metadata.

Key changes:

  • Added GetTypeInfo<T>() generic method that returns JsonTypeInfo<T>
  • Added TryGetTypeInfo<T>() generic method with strongly-typed output parameter
  • Updated the reference assembly to include the new method signatures

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs Implementation of generic GetTypeInfo<T>() and TryGetTypeInfo<T>() methods
src/libraries/System.Text.Json/ref/System.Text.Json.cs Reference assembly updates to expose the new generic method signatures

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but could you also add a couple of rudimentary unit tests?

agocke and others added 4 commits August 15, 2025 11:56
Co-authored-by: Pranav Senthilnathan <pranav.senthilnathan@live.com>
Co-authored-by: Pranav Senthilnathan <pranav.senthilnathan@live.com>
Co-authored-by: Pranav Senthilnathan <pranav.senthilnathan@live.com>
@agocke

agocke commented Aug 29, 2025

Copy link
Copy Markdown
Member Author

Note; I noticed that there was a duplicate implementation of this pattern in JsonSerializerContext. I'd like to fix both of them together, so I'll prototype this and present an updated API change.

@eiriktsarpalis

Copy link
Copy Markdown
Member

Note; I noticed that there was a duplicate implementation of this pattern in JsonSerializerContext. I'd like to fix both of them together, so I'll prototype this and present an updated API change.

I don't think that would be strictly necessary for JSC. Source generated instances already expose strongly typed JTOs as static properties.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@eiriktsarpalis eiriktsarpalis reopened this Oct 1, 2025
@eiriktsarpalis

eiriktsarpalis commented Oct 1, 2025

Copy link
Copy Markdown
Member

@agocke are there any other changes you were planning to make before putting this PR out of draft?

@agocke

agocke commented Oct 1, 2025

Copy link
Copy Markdown
Member Author

I need to make sure I caught everything and that all tests pass.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@WeihanLi

Copy link
Copy Markdown
Contributor

seems this should be re-opened?

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@github-actions github-actions Bot locked and limited conversation to collaborators Feb 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: JsonSerializerOptions.GetTypeInfo<T>

5 participants