Skip to content

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

Closed
@eiriktsarpalis

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions