Ploch.Common.Serialization.NewtonsoftJson.ExtensionsDependencyInjection
DI registration helpers for wiring
NewtonsoftJsonObjectSerializerinto aMicrosoft.Extensions.DependencyInjectioncontainer.
Overview
This library provides an IServiceCollection extension method for registering NewtonsoftJsonObjectSerializer as a singleton under the standard serialisation interfaces. It is the DI counterpart to Ploch.Common.Serialization.NewtonsoftJson and follows the same registration pattern as the System.Text.Json DI package.
After registration the following interfaces resolve to the same singleton NewtonsoftJsonObjectSerializer instance:
ISerializerISerializer<JsonSerializerSettings>
Note:
IAsyncSerializeris not registered becauseNewtonsoftJsonObjectSerializerdoes not implement stream-based async serialisation. IfIAsyncSerializerinjection is required, use theSystem.Text.JsonDI package instead.
The library targets netstandard2.0 and depends on Microsoft.Extensions.DependencyInjection.Abstractions.
Installation
dotnet add package Ploch.Common.Serialization.NewtonsoftJson.ExtensionsDependencyInjection
Key Types
| Type | Kind | Description |
|---|---|---|
NewtonsoftJsonSerializerRegistration |
Static class | Provides the AddNewtonsoftJsonSerializer extension method on IServiceCollection. |
Configuration
Using the extension method
// Startup / Program.cs
services.AddNewtonsoftJsonSerializer();
With custom settings:
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
services.AddNewtonsoftJsonSerializer(settings);
Registered services after AddNewtonsoftJsonSerializer
| Service type | Implementation | Lifetime |
|---|---|---|
ISerializer |
NewtonsoftJsonObjectSerializer |
Singleton |
ISerializer<JsonSerializerSettings> |
NewtonsoftJsonObjectSerializer |
Singleton |
The serialiser instance is constructed eagerly at registration time with the supplied JsonSerializerSettings (or a default instance if none is provided), then registered as a singleton for both interfaces.
Note: Unlike the System.Text.Json DI package, this package does not provide a
ServicesBundlesubclass. To use it as a dependency within another bundle, callAddNewtonsoftJsonSerializer()on theServicescollection inside the bundle'sDoConfigure()method.
Usage Examples
Injecting the base abstraction
public class ReportService(ISerializer serializer)
{
public string Serialise(Report report) => serializer.Serialize(report);
public Report? Deserialise(string json) => serializer.Deserialize<Report>(json);
}
Injecting the settings-aware interface
public class ConfigurableWriter(ISerializer<JsonSerializerSettings> serializer)
{
public string WriteIndented(object obj) =>
serializer.Serialize(obj, s => s.Formatting = Formatting.Indented);
}
Related Libraries
- Ploch.Common.Serialization — interface definitions
- Ploch.Common.Serialization.NewtonsoftJson — the serialiser this library registers
- Ploch.Common.Serialization.SystemTextJson.ExtensionsDependencyInjection — equivalent registration for
System.Text.Json, which also coversIAsyncSerializer