Skip to content

Conversation

@agocke
Copy link
Member

@agocke agocke commented Aug 6, 2025

Fixes #118468

@dotnet-policy-service
Copy link
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 Copilot AI review requested due to automatic review settings August 12, 2025 19:27
Copy link
Contributor

Copilot AI left a comment

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

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

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
Copy link
Member Author

agocke commented Aug 29, 2025

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
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
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
Copy link
Member

eiriktsarpalis commented Oct 1, 2025

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

@agocke
Copy link
Member Author

agocke commented Oct 1, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: JsonSerializerOptions.GetTypeInfo<T>

3 participants