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
shapeProviderITypeShapeProviderThe 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
shapeProviderITypeShapeProviderThe shape provider that describes every serializable model type.
serializerJsonSerializerThe 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
typeTypeThe object type.
Returns
- bool
trueif the type can be written, otherwisefalse.
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
contextOutputFormatterWriteContextThe formatter context associated with the call.
selectedEncodingEncodingThe Encoding that should be used to write the response.
Returns
- Task
A task which can write the response body.