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

namespaceSystem.Text.Json.Metadata;publicpartialstaticclassJsonTypeInfo{publicboolIsReadOnly{get;}publicvoidMakeReadOnly();}

API Usage

varresolver=newDefaultJsonTypeInfoResolver{Modifiers={static typeInfo =>{if(typeInfo.IsReadOnly){// type is locked for further modification, exit the modifierreturn;}if(typeInfo.Type==typeof(MySpecialPoco)){typeInfo.CreateObject=()=>newMySpecialPoco("defaultValue");// prevent subsequent modifiers/resolvers from further modifying this contracttypeInfo.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