Table of Contents

Class JsonOutputFormatter

Namespace
Nerdbank.Json.AspNetCoreMvcFormatter
Assembly
Nerdbank.Json.AspNetCoreMvcFormatter.dll

An MVC output formatter that serializes responses with JsonSerializer using an explicitly supplied, source-generated ITypeShapeProvider.

public class JsonOutputFormatter : TextOutputFormatter, IOutputFormatter, IApiResponseTypeMetadataProvider
Inheritance
JsonOutputFormatter
Implements
Inherited Members

Remarks

The formatter writes incrementally to the response's PipeWriter with backpressure so large object graphs are streamed without buffering the whole payload, honors RequestAborted, and never uses reflection or roots System.Text.Json, keeping it trimming- and NativeAOT-safe.

Constructors

JsonOutputFormatter(ITypeShapeProvider)

Initializes a new instance of the JsonOutputFormatter class with a default serializer.

public JsonOutputFormatter(ITypeShapeProvider shapeProvider)

Parameters

shapeProvider ITypeShapeProvider

The shape provider that describes every serializable model type.

JsonOutputFormatter(ITypeShapeProvider, JsonSerializer)

Initializes a new instance of the JsonOutputFormatter class.

public JsonOutputFormatter(ITypeShapeProvider shapeProvider, JsonSerializer serializer)

Parameters

shapeProvider ITypeShapeProvider

The shape provider that describes every serializable model type.

serializer JsonSerializer

The serializer used to write responses.

Methods

CanWriteType(Type?)

Returns a value indicating whether or not the given type can be written by this serializer.

protected override bool CanWriteType(Type? type)

Parameters

type Type

The object type.

Returns

bool

true if the type can be written, otherwise false.

Remarks

Returns true only when the configured shape provider can describe type. This lets the formatter coexist with other output formatters (for example the built-in System.Text.Json formatter for framework types such as ProblemDetails) instead of claiming responses it cannot serialize.

WriteResponseBodyAsync(OutputFormatterWriteContext, Encoding)

Writes the response body.

public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)

Parameters

context OutputFormatterWriteContext

The formatter context associated with the call.

selectedEncoding Encoding

The Encoding that should be used to write the response.

Returns

Task

A task which can write the response body.