-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
Cost:SWork that requires one engineer up to 1 weekWork that requires one engineer up to 1 weekTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.A single user-facing feature. Can be grouped under an epic.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 and motivation
JsonSerializerOptions
currently exposes an internal s_defaultOptions
instance for use by System.Text.Json. This is useful since JsonSerializerOptions
holds the reflection-based serialization metadata cache, as such creating a default new 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
namespace System.Text.Json
{
public partial class JsonSerializerOptions
{
public static JsonSerializerOptions Default { get; }
}
}
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:
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 Designs
No response
Risks
No response
N0D4N and Mrxx99
Metadata
Metadata
Assignees
Labels
Cost:SWork that requires one engineer up to 1 weekWork that requires one engineer up to 1 weekTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.A single user-facing feature. Can be grouped under an epic.api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Text.Json