Skip to content

[API Proposal]: Expose a JsonSerializerOptions.Default property #61093

Closed

Description

@eiriktsarpalis

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

Activity

  1. added this to the 7.0.0 milestone on Nov 2, 2021
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Nov 2, 2021
  3. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    untriagedNew issue has not been triaged by the area owner
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Nov 2, 2021
  4. TonyValenti commented on Nov 2, 2021

    @TonyValenti

    Would the defaults be mutable? My recommendation is "No".

  5. eiriktsarpalis commented on Nov 2, 2021

    @eiriktsarpalis
    MemberAuthor

    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.

  6. terrajobst commented on Nov 2, 2021

    @terrajobst
    Contributor

    Video

    • We need to make sure the instance is marked as "you can't touch this anymore"
    namespace System.Text.Json
    {
        public partial class JsonSerializerOptions
        {
            public static JsonSerializerOptions Default { get; }
        }
    }
  7. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Nov 2, 2021
  8. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 10, 2021
  9. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Nov 15, 2021
  10. ghost locked as resolved and limited conversation to collaborators on Dec 15, 2021
  11. added
    User StoryA single user-facing feature. Can be grouped under an epic.
    Cost:SWork that requires one engineer up to 1 week
    on Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Cost:SWork that requires one engineer up to 1 weekTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Json

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions