Codebelt

Savvyio.Extensions.DependencyInjection.Newtonsoft.Json

Register a Newtonsoft.Json-backed Savvy I/O marshaller in Microsoft DI with configurable formatting and service lifetime.

.NET 10.0 / .NET 9.0 MIT v5.0.8 2,120 downloads

Overview

Savvyio.Extensions.DependencyInjection.Newtonsoft.Json registers the Savvy I/O Newtonsoft.Json marshaller in IServiceCollection. It extends Savvyio.Extensions.DependencyInjection with the NewtonsoftJsonMarshaller from Savvyio.Extensions.Newtonsoft.Json, so applications can resolve the marshaller and its formatter options through Microsoft Dependency Injection instead of constructing them manually.

The package is intentionally narrow. It does not add a new serializer implementation or new converters on its own. It composes the existing marshaller and configured NewtonsoftJsonFormatterOptions into the container, defaulting JSON formatting to Formatting.None when no JSON setup delegate is supplied.

Key APIs

AddNewtonsoftJsonMarshaller is the package's only public API. It registers NewtonsoftJsonMarshaller on an IServiceCollection, stores NewtonsoftJsonFormatterOptions through the shared configured-options pattern, defaults the service lifetime to ServiceLifetime.Singleton, and accepts optional callbacks for both formatter configuration and ServiceOptions customization. The tests verify the null guard, the default compact formatting, custom JSON setup, and a custom lifetime.

Basic usage

using System;
using System.IO;
using Codebelt.Extensions.Newtonsoft.Json.Formatters;
using Codebelt.Extensions.Xunit;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Savvyio.Extensions.DependencyInjection.Newtonsoft.Json;
using Savvyio.Extensions.Newtonsoft.Json;
using Xunit;

namespace MyProject.Tests;

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

    [Fact]
    public void AddNewtonsoftJsonMarshaller_CustomSettings_RegistersMarshaller()
    {
        var services = new ServiceCollection();
        services.AddNewtonsoftJsonMarshaller(
            jsonSetup: o => o.Settings.Formatting = Formatting.Indented,
            serviceSetup: o => o.Lifetime = ServiceLifetime.Scoped);

        var descriptor = Assert.Single(services, d => d.ServiceType == typeof(NewtonsoftJsonMarshaller));
        Assert.Equal(ServiceLifetime.Scoped, descriptor.Lifetime);

        using var provider = services.BuildServiceProvider();
        using var scope = provider.CreateScope();
        var marshaller = scope.ServiceProvider.GetRequiredService<NewtonsoftJsonMarshaller>();
        var options = scope.ServiceProvider.GetRequiredService<NewtonsoftJsonFormatterOptions>();
        using var stream = marshaller.Serialize(new MemberSnapshot(42, "active"));
        using var reader = new StreamReader(stream);
        var json = reader.ReadToEnd();

        TestOutput.WriteLine(json);

        Assert.Equal(Formatting.Indented, options.Settings.Formatting);
        Assert.Contains(Environment.NewLine, json);
    }

    private sealed record MemberSnapshot(int Id, string Status);
}

Use this pattern when your application wants one DI-registered NewtonsoftJsonMarshaller with centrally configured formatter behavior. It matters because the package keeps the serializer setup in the container, so callers resolve the same marshaller and options instead of repeating local Newtonsoft.Json configuration.

Installation

dotnet add package Savvyio.Extensions.DependencyInjection.Newtonsoft.Json

Usage guidance

Use this package when the application already depends on Microsoft.Extensions.DependencyInjection and you want Savvy I/O's Newtonsoft.Json marshaller available as a registered service with shared formatter options. If you do not need DI registration, or if direct construction of NewtonsoftJsonMarshaller is enough, depend on Savvyio.Extensions.Newtonsoft.Json or plain Newtonsoft.Json APIs instead.

Family packages