Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 29, 2025

The System.Text.Json documentation was missing critical information about a limitation when using custom converters with ReferenceHandler.Preserve. Users were confused why their custom converters didn't work properly when reference preservation was enabled.

Problem

When using ReferenceHandler.Preserve with custom converters, the reference handling state is not preserved when the serializer calls into custom converters. This means:

  • Custom converters cannot access the current ReferenceResolver instance
  • Converters are called for each object instance separately, without knowledge of whether the object has already been serialized elsewhere in the graph
  • Calling ReferenceHandler.CreateResolver() throws an exception because the resolver has already been created

Solution

Added a new subsection "ReferenceResolver limitations with custom converters" under the "Preserve references" section that:

  • Clearly explains the limitation with a concrete code example showing Parent and Child classes
  • Documents that this is a known runtime limitation
  • Provides three practical workaround approaches for developers who need both custom conversion logic and reference preservation
  • Links to the relevant runtime issues (#51715 and #42163) for tracking future improvements

This documentation change helps users understand why their converters don't work as expected with reference preservation and provides guidance on current limitations and potential approaches.

Fixes #44491.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/standard/serialization/system-text-json/converters-how-to.md How to write custom converters for JSON serialization (marshalling) in .NET

Copilot AI changed the title [WIP] Missing any explanation on how to respect the ReferenceResolver in use when ReferenceHandler.Preserve has been used Document ReferenceResolver limitations with custom converters in System.Text.Json Jul 29, 2025
Copilot AI requested a review from gewarren July 29, 2025 00:25
Copilot finished work on behalf of gewarren July 29, 2025 00:25
@gewarren gewarren requested a review from eiriktsarpalis July 29, 2025 00:35
@gewarren gewarren marked this pull request as ready for review July 29, 2025 00:35
@gewarren gewarren requested a review from a team as a code owner July 29, 2025 00:35
@gewarren gewarren enabled auto-merge (squash) July 29, 2025 16:59
@gewarren gewarren merged commit 09a561f into main Jul 29, 2025
10 checks passed
@gewarren gewarren deleted the copilot/fix-44491 branch July 29, 2025 17:06
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.

Missing any explanation on how to respect the ReferenceResolver in use when ReferenceHandler.Preserve has been used

3 participants