You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Register default serializers without constructing them, allowing a custom JSON serializer such as Newtonsoft.Json to replace the default before System.Text.Json is loaded.
Share content-type matching predicates between registration and the built-in serializers to avoid duplicated rules. Public APIs and serializer instance lifetimes remain unchanged.
Uses the deferred-registration approach proposed in #2403, with shared matching rules and automated regression coverage.
Validation:
Release build passed for all six current library targets.
Five isolated net48 AppDomain tests verify that only the required JSON assemblies load.
Four regression scenarios failed against the original implementation.
Added tests for registration metadata consistency and fresh serializer instances.
No user-facing documentation changes are needed because existing configuration APIs remain unchanged.
Purpose
This pull request is a:
Bugfix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
Checklist
I have added tests that prove my fix is effective or that my feature works
I have added necessary documentation (if appropriate)
Defer default serializer construction to avoid unnecessary JSON loading
🐞 Bug fix🧪 Tests🕐 20-40 Minutes
AI Description
• Register built-in serializers lazily so replacing JSON avoids loading System.Text.Json.
• Centralize JSON and XML content-type predicates across registration and serializer
implementations.
• Add isolated assembly-loading, metadata-consistency, and fresh-instance regression coverage.
Diagram
sequenceDiagram
participant C as RestClient
participant SC as Serializer Config
participant R as Serializer Registry
participant CJ as Custom JSON
participant DF as Default Factory
participant JA as JSON Assembly
C->>SC: Initialize serializers
SC->>R: Store deferred defaults
Note over DF,JA: Not loaded during registration
alt Custom JSON configured
C->>SC: Configure custom JSON
SC->>CJ: Inspect metadata
SC->>R: Replace JSON record
R->>CJ: Create selected serializer
else Default JSON requested
R->>DF: Invoke factory
DF->>JA: Load dependency
DF-->>R: Return fresh serializer
end
Loading
High-Level Assessment
The following are alternative approaches to this PR:
1. Cache serializers with Lazy
➕ Naturally delays dependency loading until first use
➕ Centralizes deferred initialization behavior
➖ Would reuse serializer instances and change existing lifetime semantics
➖ Still requires metadata without constructing the serializer
2. Add a static serializer metadata contract
➕ Keeps metadata colocated with each serializer type
➕ Reduces manual descriptor construction in registration
➖ Requires a broader serializer contract and compatibility change
➖ Adds complexity for custom serializers and older target frameworks
Recommendation: Keep the PR's deferred descriptor approach. It fixes premature dependency loading with a narrow internal change, preserves fresh serializer instances and existing public configuration APIs, and avoids the compatibility costs of a new metadata contract. Shared ContentType predicates limit the main risk of manually registered metadata drifting from built-in implementations.
Files changed (6) +164 / -7
Bug fix (1) +15 / -1
SerializerConfig.csRegister built-in serializers without constructing them+15/-1
Register built-in serializers without constructing them
• Creates default JSON and XML SerializerRecord entries directly from static metadata and deferred factories. Custom configuration can therefore replace System.Text.Json before its serializer or dependency is loaded, while each lookup still returns a fresh instance.
ContentType.csCentralize JSON and XML matching predicates+4/-1
Centralize JSON and XML matching predicates
• Adds shared internal content-type predicates for JSON and XML. Registration descriptors and built-in serializers can now use identical matching rules.
SystemTextJsonSerializer.csReuse the shared JSON content-type predicate+2/-2
Reuse the shared JSON content-type predicate
• Replaces the serializer-local JSON suffix predicate with the centralized ContentType.SupportsJson delegate, keeping runtime metadata aligned with deferred registration.
XmlRestSerializer.csReuse the shared XML content-type predicate+2/-2
Reuse the shared XML content-type predicate
• Replaces the serializer-local XML suffix predicate with ContentType.SupportsXml so deferred registration and serializer metadata share the same delegate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #2402.
Register default serializers without constructing them, allowing a custom JSON serializer such as Newtonsoft.Json to replace the default before System.Text.Json is loaded.
Share content-type matching predicates between registration and the built-in serializers to avoid duplicated rules. Public APIs and serializer instance lifetimes remain unchanged.
Uses the deferred-registration approach proposed in #2403, with shared matching rules and automated regression coverage.
Validation:
No user-facing documentation changes are needed because existing configuration APIs remain unchanged.
Purpose
This pull request is a:
Checklist