Codebelt
v10.1.7

Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json

Use this package when MVC controllers should read and write JSON through Codebelt's Newtonsoft formatter pipeline instead of the default MVC JSON handling.

.NET 10.0 / .NET 9.0 MIT 7,336 downloads

Overview

Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json plugs Codebelt's NewtonsoftJsonFormatter into ASP.NET Core MVC so controller actions can deserialize request bodies and serialize responses through formatter types that MVC understands. The package also wires in JSON-based exception response formatting by delegating option registration to Codebelt.Extensions.AspNetCore.Newtonsoft.Json.

The package is focused on MVC integration. Its public surface is small: two formatter classes, one MVC options setup type, one JsonSerializerSettings copier, and builder extensions for IMvcBuilder and IMvcCoreBuilder.

Key APIs

MvcBuilderExtensions.AddNewtonsoftJsonFormatters registers JsonSerializationMvcOptionsSetup as an IConfigureOptions<MvcOptions> and then applies NewtonsoftJsonFormatterOptions through AddNewtonsoftJsonFormattersOptions. Tests show this is the main entry point when controllers should emit either problem details or fault details as JSON through the Newtonsoft pipeline.

MvcCoreBuilderExtensions.AddNewtonsoftJsonFormatters provides the same registration model for IMvcCoreBuilder. Use it when the app builds on MVC Core instead of the higher-level MVC builder.

MvcBuilderExtensions.AddNewtonsoftJsonFormattersOptions and MvcCoreBuilderExtensions.AddNewtonsoftJsonFormattersOptions delegate to AddNewtonsoftJsonExceptionResponseFormatter. That means formatter option registration and exception response formatting stay aligned around the same NewtonsoftJsonFormatterOptions instance.

JsonSerializationMvcOptionsSetup inserts JsonSerializationOutputFormatter and JsonSerializationInputFormatter at index 0 in MvcOptions. That placement makes these formatters take precedence over later MVC formatters for matching media types.

JsonSerializationInputFormatter derives from StreamInputFormatter<NewtonsoftJsonFormatter, NewtonsoftJsonFormatterOptions> and populates MVC supported media types from NewtonsoftJsonFormatterOptions.SupportedMediaTypes. Its constructor also adds the HTTP exception descriptor converter with the configured SensitivityDetails so inbound MVC JSON handling uses the same converter set as the rest of the formatter pipeline.

JsonSerializationOutputFormatter derives from StreamOutputFormatter<NewtonsoftJsonFormatter, NewtonsoftJsonFormatterOptions> and mirrors the same media type and converter setup for MVC responses. Tests verify that it exposes UTF-8, UTF-16, application/json, text/json, and application/problem+json support.

JsonSerializerSettingsExtensions.Use<T> copies a configured parameter-object-derived JsonSerializerSettings instance onto an existing settings object. It is the package's direct extension point when a caller needs to project a strongly configured settings type onto MVC-facing serializer settings.

Basic usage

using Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Codebelt.Extensions.Xunit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Xunit;

namespace MyProject.Tests;

public class NewtonsoftMvcFormatterTests : Test
{
    public NewtonsoftMvcFormatterTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void JsonSerializationMvcOptionsSetup_InsertsNewtonsoftFormattersFirst()
    {
        var formatterOptions = Options.Create(new NewtonsoftJsonFormatterOptions());
        var mvcOptions = new MvcOptions();

        new JsonSerializationMvcOptionsSetup(formatterOptions).Configure(mvcOptions);

        TestOutput.WriteLines(
            $"Output formatter: {mvcOptions.OutputFormatters[0].GetType().Name}",
            $"Input formatter: {mvcOptions.InputFormatters[0].GetType().Name}");

        Assert.IsType<JsonSerializationOutputFormatter>(mvcOptions.OutputFormatters[0]);
        Assert.IsType<JsonSerializationInputFormatter>(mvcOptions.InputFormatters[0]);
        Assert.Contains(((JsonSerializationOutputFormatter)mvcOptions.OutputFormatters[0]).SupportedMediaTypes, mt => mt.Contains("application/problem+json"));
    }
}

Use this pattern when you want MVC options to prefer the package's Newtonsoft-based formatters for controller input and output. It matters because the setup object inserts both formatters at the front of the MVC pipeline and carries over the media types expected by the package tests.

Installation

dotnet add package Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json

Usage guidance

Adopt this package when an ASP.NET Core MVC application already uses controller-based endpoints and needs MVC request and response bodies to flow through NewtonsoftJsonFormatterOptions, including the same exception descriptor converter and sensitivity settings used's ASP.NET Core formatter services. If you only need JSON exception response formatting outside MVC, Codebelt.Extensions.AspNetCore.Newtonsoft.Json is the smaller dependency surface.

Family packages