Class JsonInputFormatter
- Namespace
- Nerdbank.Json.AspNetCoreMvcFormatter
- Assembly
- Nerdbank.Json.AspNetCoreMvcFormatter.dll
An MVC input formatter that deserializes request bodies with JsonSerializer using an explicitly supplied, source-generated ITypeShapeProvider.
public class JsonInputFormatter : TextInputFormatter, IInputFormatter, IApiRequestFormatMetadataProvider
- Inheritance
-
JsonInputFormatter
- Implements
- Inherited Members
Remarks
The formatter reads incrementally from the request's PipeReader so large bodies are not buffered in
full, honors RequestAborted, and never uses reflection or roots System.Text.Json,
keeping it trimming- and NativeAOT-safe.
Each nonempty body must contain exactly one JSON document, allowing trailing whitespace and configured comments.
Constructors
JsonInputFormatter(ITypeShapeProvider)
Initializes a new instance of the JsonInputFormatter class with a default serializer.
public JsonInputFormatter(ITypeShapeProvider shapeProvider)
Parameters
shapeProviderITypeShapeProviderThe shape provider that describes every deserializable model type.
JsonInputFormatter(ITypeShapeProvider, JsonSerializer)
Initializes a new instance of the JsonInputFormatter class.
public JsonInputFormatter(ITypeShapeProvider shapeProvider, JsonSerializer serializer)
Parameters
shapeProviderITypeShapeProviderThe shape provider that describes every deserializable model type.
serializerJsonSerializerThe serializer used to read request bodies.
Methods
CanReadType(Type)
Determines whether this InputFormatter can deserialize an object of the given
type.
protected override bool CanReadType(Type type)
Parameters
Returns
- bool
trueif thetypecan be read, otherwisefalse.
Remarks
Returns true only when the configured shape provider can describe type. This
lets the formatter coexist with other input formatters (for example the built-in System.Text.Json
formatter for framework types) instead of claiming JSON payloads it cannot deserialize.
ReadRequestBodyAsync(InputFormatterContext, Encoding)
Reads an object from the request body.
public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
Parameters
contextInputFormatterContextencodingEncodingThe Encoding used to read the request body.
Returns
- Task<InputFormatterResult>
A Task that on completion deserializes the request body.