-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Json
Milestone
Description
Background & Motivation
The JsonSerializerOptions.AddContext<TContext>() method was introduced in .NET 6 as a means to associate JsonSerializerOptions instances with a given JsonSerializerContext type. This method was largely superseded in .NET 7 with the introduction of contract customization and the JsonSerializerOptions.TypeInfoResolver property.
We should obsolete AddContext for the following reasons:
- Contrary to what the name suggests, it replaces user configuration instead of appending to it.
- It can be confused with the new
JsonSerializerOptions.TypeInfoResolverChainwhen manipulating resolver chains. - The method only works with
JsonSerializerContexttype parameters, not with arbitraryIJsonTypeInfoResolverinstances. - The method freezes the
JsonSerializerOptionsfor further modification:JsonSerializerOptions options = new(); options.AddContext<Context1>(); // success options.AddContext<Context2>(); // InvalidOperationException
API Proposal
public class JsonSerializerOptions
{
[Obsolete(/* SYLIB code TBD? */)]
public void AddContext<TContext>() : where TContext : JsonSerializerContext { }
}cc @eerhardt
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Json