Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectNewtonsoftJsonMvcExtensions

Namespace: AdCodicem.ValueObjects.AspNetCore.NewtonsoftJson
Assembly: AdCodicem.ValueObjects.AspNetCore.NewtonsoftJson.dll

Wires value object support into an MVC application whose request bodies Newtonsoft.Json reads.

public static class ValueObjectNewtonsoftJsonMvcExtensions

Inheritance​

object ← ValueObjectNewtonsoftJsonMvcExtensions

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Methods​

AddValueObjectsNewtonsoftJson(IMvcBuilder)​

Registers value object model binding and serialization on an MVC builder that reads with Newtonsoft.Json, and records the code of the rule a value refused inside a request body broke.

public static IMvcBuilder AddValueObjectsNewtonsoftJson(this IMvcBuilder builder)

Parameters​

builder IMvcBuilder

MVC builder, on which AddNewtonsoftJson() is called, before or after this.

Returns​

IMvcBuilder

The same builder, so calls can be chained.

Remarks​

It calls first, so that value objects bind from the route, the query string and the headers, and one call sets everything up; an application that calls that method as well, before or after, behaves the same.

It adds to MVC's , with and , as does, once every configuration of those options has run, and only when the settings hold no such converter yet: settings the application gave one are its own, and are left as it left them. Without the converter, Newtonsoft.Json reads a value object through its type converter, whose refusal names no rule and quotes the refused text.

Those settings are the ones MVC writes every response with as well, and they apply to every member of every body. An application that ran without the converter changes its wire: a numeric or boolean value object is answered 7 or true, where the type converter wrote "7" or "True", and a boolean value object sent as a string is refused. A member of type or JToken keeps a string that looks like a date as a string, and reads a number with a fraction as a , which refuses one beyond its range.

It then puts a formatter of its own in place of MVC's Newtonsoft.Json input formatter, at its index: one deriving from it, built over the same serializer settings and the same , with the media types and encodings the application gave the formatter it replaces. It reads a body as the framework's does, and records the code of each value object the body refuses under the key of its model state error, so that puts it in the response beside the message. Newtonsoft.Json reads on past a refused member of an object whose properties it sets, so each refused member keeps its own code, until the model state holds as many errors as lets it. The model state, the logs and the exceptions that propagate are the framework's, whatever says when the request is read. A Newtonsoft.Json formatter the application built over settings of its own, or derived, the JSON Patch formatter among them, is left as it is, and records no code: a JSON Patch document records the codes of its values when the action applies it through TryApplyTo, of .

The swap is a post-configuration of . In a post-configuration registered after this call, InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>() finds this formatter in its place, which is not of that exact type: remove there every formatter that is a but neither a , which derives from it and which that call keeps, nor a formatter the application derived itself.

It does not call AddNewtonsoftJson(), which is the application's choice of serializer. Without it MVC reads bodies with System.Text.Json, whose formatter already replaces, and this call amounts to that one.

Exceptions​

ArgumentNullException

builder is null.