-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add Lock()
and IsLocked
methods to JsonSerializerOptions
#72268
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsBackground and motivationThe var options = new JsonSerializerOptions();
options.WriteIndented = true; // success, options is mutable
JsonSerializer.Serialize(someValue, options);
options.WriteIndented = true; // throws, options is locked The issue with this design is that users have no clear indication of whether an options instance is locked or not. Often, users need to serialize a throwaway value just to make sure no further modification can take place. We want to make this state more transparent and controllable by users. API Proposalnamespace System.Text.Json
public partial class JsonSerializerOptions
{
public bool IsLocked { get; }
public void Lock(); // idempotent lock operation
} API Usagevar options = new JsonSerializerOptions { TypeInfoResolver = new MyFancyResolver(), WriteIndented = true };
Console.WriteLine(options.IsLocked); // False
var converter = options.GetConverter(typeof(MyPoco)); // Returns an uncached converter instance
options.Lock(); // lock the instance and initialize the metadata cache
Console.WriteLine(options.IsLocked); // True
converter = options.GetConverter(typeof(MyPoco)); // Returns a cached converter instance Alternative DesignsWe might want to consider exposing equivalent functionality to RisksNo response
|
This has been previously discussed at #54482. I think one of the two issues should be closed. Also, I stand by the comment I made there saying that |
That's a good point, although I've never heard of "Freeze" being used in this context. Any other alternatives? One that I can think of is
That's my bad, I should have gone through the backlog before opening this. |
|
Closing as duplicate of #54482. |
Background and motivation
The
JsonSerializerOptions
class supports mutation of its settings, until first used in a serialization operation. After that, the class gets locked and any attempt to update settings will result inInvalidOperationException
:The issue with this design is that users have no clear indication of whether an options instance is locked or not. Often, users need to serialize a throwaway value just to make sure no further modification can take place. We want to make this state more transparent and controllable by users.
API Proposal
API Usage
Alternative Designs
We might want to consider exposing equivalent functionality to
JsonTypeInfo
andJsonPropertyInfo
, whose design uses a similar locking design.Risks
No response
The text was updated successfully, but these errors were encountered: