Codebelt
v10.1.7

Codebelt.Extensions.Newtonsoft.Json.App

One package reference for the full Codebelt Newtonsoft.Json stack.

.NET 10.0 / .NET 9.0 MIT 11,227 downloads

Overview

Codebelt.Extensions.Newtonsoft.Json.App is a meta-package for applications that want the repository's full Newtonsoft.Json stack in one reference. Its project file disables build output and references three sibling packages, so installing it adds their assemblies without introducing another API surface of its own.

The bundle combines general JSON formatting and parsing from Codebelt.Extensions.Newtonsoft.Json, ASP.NET Core exception response registration from Codebelt.Extensions.AspNetCore.Newtonsoft.Json, and MVC input/output formatter integration from Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.

Key APIs

This package exposes no additional public APIs. Consumer-facing APIs come from Codebelt.Extensions.Newtonsoft.Json, Codebelt.Extensions.AspNetCore.Newtonsoft.Json, and Codebelt.Extensions.AspNetCore.Mvc.Formatters.Newtonsoft.Json.

Basic usage

Codebelt.Extensions.Newtonsoft.Json

using System;
using System.Linq;
using Codebelt.Extensions.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Codebelt.Extensions.Xunit;
using Xunit;

namespace MyProject.Tests;

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

    [Fact]
    public void SerializeAndInspectJsonDocument()
    {
        var formatter = new NewtonsoftJsonFormatter();
        using var json = formatter.Serialize(new DeploymentReport("west-europe", 2), typeof(DeploymentReport));

        var tokens = JData.ReadAll(json, o => o.LeaveOpen = true).ToList();

        TestOutput.WriteLine(string.Join(Environment.NewLine, tokens.Select(t => $"{t.Path}={t.Value}")));

        Assert.Contains(tokens, t => t.Path == "region" && Equals(t.Value, "west-europe"));
        Assert.Contains(tokens, t => t.Path == "replicas" && Equals(t.Value, Convert.ChangeType(2, t.Type)));
    }

    private sealed record DeploymentReport(string Region, int Replicas);
}

Codebelt.Extensions.AspNetCore.Newtonsoft.Json

using Codebelt.Extensions.AspNetCore.Newtonsoft.Json;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Codebelt.Extensions.Xunit;
using Cuemon.AspNetCore.Diagnostics;
using Cuemon.Diagnostics;
using Cuemon.Extensions.AspNetCore.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Xunit;

namespace MyProject.Tests;

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

    [Fact]
    public void RegisterMinimalExceptionFormatting()
    {
        var services = new ServiceCollection();
        services.AddFaultDescriptorOptions();
        services.AddMinimalNewtonsoftJsonOptions(o => o.SensitivityDetails = FaultSensitivityDetails.All);

        using var provider = services.BuildServiceProvider();
        var formatter = provider.GetService<HttpExceptionDescriptorResponseFormatter<NewtonsoftJsonFormatterOptions>>();
        var options = provider.GetRequiredService<IOptions<NewtonsoftJsonFormatterOptions>>().Value;

        TestOutput.WriteLine($"Sensitivity: {options.SensitivityDetails}");

        Assert.NotNull(formatter);
        Assert.Equal(FaultSensitivityDetails.All, options.SensitivityDetails);
    }
}

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

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

namespace MyProject.Tests;

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

    [Fact]
    public void RegisterMvcInputAndOutputFormatters()
    {
        var services = new ServiceCollection();
        services.AddControllers().AddNewtonsoftJsonFormatters();

        using var provider = services.BuildServiceProvider();
        var mvcOptions = provider.GetRequiredService<IOptions<MvcOptions>>().Value;

        TestOutput.WriteLine($"Input formatters: {mvcOptions.InputFormatters.Count}");
        TestOutput.WriteLine($"Output formatters: {mvcOptions.OutputFormatters.Count}");

        Assert.Contains(mvcOptions.InputFormatters, formatter => formatter is JsonSerializationInputFormatter);
        Assert.Contains(mvcOptions.OutputFormatters, formatter => formatter is JsonSerializationOutputFormatter);
    }
}

This package is the single package reference. The APIs shown above are owned by the three referenced packages that it brings into the application.

Installation

dotnet add package Codebelt.Extensions.Newtonsoft.Json.App

Usage guidance

Use this package when one application needs the full stack: general Newtonsoft.Json helpers, ASP.NET Core exception-response formatting, and MVC JSON formatters. If you only need one layer, reference the smaller package directly so the dependency graph matches the scope of your application.

Family packages