-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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]: Expose a JsonSerializerOptions.Default property #61093
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsBackground and motivation
I propose we make this singleton a public property so that users can read default configuration cheaply (e.g. the default JsonConverter for a requested type). API Proposalnamespace System.Text.Json
{
public partial class JsonSerializerOptions
{
public static JsonSerializerOptions Default { get; }
}
} API UsageHere's an (artificial) example of how the property can be used to cheaply recover the default converter used by STJ for integers: public class MyCustomConverter : JsonConverter<int>
{
private readonly static JsonConverter<int> s_defaultConverter = (JsonConverter<int>)JsonSerializerOptions.Default.GetConverter(typeof(int));
// custom serialization logic
public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
{
return writer.WriteStringValue(value.ToString());
}
// fall back to default deserialization logic
public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return s_defaultConverter.Read(ref reader, typeToConvert, options);
}
} Alternative DesignsNo response RisksNo response
|
Would the defaults be mutable? My recommendation is "No". |
It would not be. Moreover the implementation of this property would need to make sure that the instance is locked for modification before being returned. |
namespace System.Text.Json
{
public partial class JsonSerializerOptions
{
public static JsonSerializerOptions Default { get; }
}
} |
Background and motivation
JsonSerializerOptions
currently exposes an internals_defaultOptions
instance for use by System.Text.Json. This is useful sinceJsonSerializerOptions
holds the reflection-based serialization metadata cache, as such creating a defaultnew JsonSerializerOptions()
instance can be very expensive since it forces the regeneration of that cache.I propose we make this singleton a public property so that users can read default configuration cheaply (e.g. the default JsonConverter for a requested type).
API Proposal
API Usage
Here's an (artificial) example of how the property can be used to cheaply recover the default converter used by STJ for integers:
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: