Skip to content

[API Proposal]: Add freezing APIs to JsonTypeInfo #76893

Description

@eiriktsarpalis

Background and motivation

Following up on #76540, this tracks the proposal to add user browsable locking APIs for the System.Text.Json metadata types. It follows the API shape applied to JsonSerializerOptions in #74431.

API Proposal

namespace System.Text.Json.Metadata;

public partial static class JsonTypeInfo
{
    public bool IsReadOnly { get; }
    public void MakeReadOnly();
}

API Usage

var resolver = new DefaultJsonTypeInfoResolver
{
    Modifiers =
    {
         static typeInfo =>
         {
               if (typeInfo.IsReadOnly)
               {
                       // type is locked for further modification, exit the modifier
                       return;
               }

               if (typeInfo.Type == typeof(MySpecialPoco))
               {
                      typeInfo.CreateObject = () => new MySpecialPoco("defaultValue");
                      // prevent subsequent modifiers/resolvers from further modifying this contract
                      typeInfo.MakeReadOnly(); 
               }
         }
    }
};

Alternative Designs

No response

Risks

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions