PolymorphicJsonTypeInfoResolver 0.1.9

There is a newer version of this package available.
See the version list below for details.
dotnet add package PolymorphicJsonTypeInfoResolver --version 0.1.9
                    
NuGet\Install-Package PolymorphicJsonTypeInfoResolver -Version 0.1.9
                    
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="PolymorphicJsonTypeInfoResolver" Version="0.1.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PolymorphicJsonTypeInfoResolver" Version="0.1.9" />
                    
Directory.Packages.props
<PackageReference Include="PolymorphicJsonTypeInfoResolver" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PolymorphicJsonTypeInfoResolver --version 0.1.9
                    
#r "nuget: PolymorphicJsonTypeInfoResolver, 0.1.9"
                    
#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.
#addin nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.9
                    
Install PolymorphicJsonTypeInfoResolver as a Cake Addin
#tool nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.9
                    
Install PolymorphicJsonTypeInfoResolver as a Cake Tool

Polymorphic Json Type Info Resolver

nuget stryker codecov maintainability Build status

Using polymorphism with the contract model available since .NET7+.

var options = new JsonSerializerOptions {
    TypeInfoResolver = new PolymorphicTypeInfoResolver()
        .Type<B>(x => x.DerivedTypes.Add<C>("c"))
}
        
var json = JsonSerializer.Serialize(new A(new C("cheap")), options);
{
    "Specification": {
        "$type":"C",
        "Remarks":"cheap"
    }
}

Factory for default options:

var options = new JsonSerializerOptions {
    TypeInfoResolver = new PolymorphicTypeInfoResolver(options: () => new JsonPolymorphismOptions {
        TypeDiscriminatorPropertyName = "$TYPE"
    })
};

Opt-in for all derived types (from the same assemby):

new JsonSerializerOptions {
    TypeInfoResolver = new PolymorphicTypeInfoResolver()
        .Type<C>(x => x
            .DerivedTypes
            .AddAllAssignableTo<C>())
};
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

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
8.0.27 7,118 6/7/2024
7.1.0-alpha.1 128 4/24/2023
7.0.16 1,776 4/24/2023
7.0.15 310 4/24/2023
0.1.14 222 4/24/2023
0.1.13 202 4/24/2023
0.1.12 211 4/24/2023
0.1.11 213 4/22/2023
0.1.10 205 4/21/2023
0.1.9 239 4/21/2023
0.1.8 200 4/21/2023
0.0.7 235 4/20/2023
0.0.6 218 4/20/2023
0.0.4 223 4/20/2023
0.0.3 221 4/20/2023
0.0.1 225 4/20/2023

Breaking change: using `JsonPolymorphismOptions` instead of wrapping in own builder.