Iciclecreek.Json.Net.DependencyInjection 1.0.1

dotnet add package Iciclecreek.Json.Net.DependencyInjection --version 1.0.1                
NuGet\Install-Package Iciclecreek.Json.Net.DependencyInjection -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Iciclecreek.Json.Net.DependencyInjection" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Iciclecreek.Json.Net.DependencyInjection --version 1.0.1                
#r "nuget: Iciclecreek.Json.Net.DependencyInjection, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Iciclecreek.Json.Net.DependencyInjection as a Cake Addin
#addin nuget:?package=Iciclecreek.Json.Net.DependencyInjection&version=1.0.1

// Install Iciclecreek.Json.Net.DependencyInjection as a Cake Tool
#tool nuget:?package=Iciclecreek.Json.Net.DependencyInjection&version=1.0.1                

Iciclecreek.Json.Net.DependencyInjection

This library implements a JSON.NET converter which will use IServiceProvider to perform dependency injection as needed while deserializing.

Add NUGET package

To add to your project add dotnet add package Iciclecreek.Json.Net.DependencyInjection

Description

There are 2 classes in this library

  • ServiceProviderConverter - a universal converter which will use the IServiceProvider to instantiate objects that do not have a parameterless converter
  • ServiceProviderConvert<T> - A typed converter which will use IServiceProvider only for the given type T.

Usage

To use these converters you add them to the converters collection on JsonSerializerSettings object.

Example for universal types needing dependency injection:

IServiceProvider serviceProvider = new ServiceCollection()
    .AddSingleton<JsonSerializerSettings>((sp) => new JsonSerializerSettings() 
    { 
        Converters = new List<JsonConverter>() 
        { 
            new ServiceProviderConverter(sp) 
        } 
    })
    // .., add other dependencies as needed ...
    .BuildServiceProvider();

Example for explicit resgistration just for types that need dependency injection:

IServiceProvider serviceProvider = new ServiceCollection()
    .AddSingleton<JsonSerializerSettings>((sp) => new JsonSerializerSettings() 
    { 
        Converters = new List<JsonConverter>() 
        { 
            new ServiceProviderConverter<Obj1>(sp),
            new ServiceProviderConverter<Obj2>(sp),
            new ServiceProviderConverter<Obj3>(sp) 
        } 
    })
    // .., add other dependencies as needed ...
    .BuildServiceProvider();

NOTE: Registering for objects explicitely is slighty faster on first deserialization because the universal converter has to detect when an object can't be created.

Then simply use your json serializer settings when deserializing:

var result = JsonConvert.DeserializeObject<MyClass>(json, serviceProvider.GetRequiredService<JsonSerializerSettings>());
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 130 2/7/2024
1.0.0 123 2/5/2024