PolymorphicJsonTypeInfoResolver 0.1.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package PolymorphicJsonTypeInfoResolver --version 0.1.10
NuGet\Install-Package PolymorphicJsonTypeInfoResolver -Version 0.1.10
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.10" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PolymorphicJsonTypeInfoResolver --version 0.1.10
#r "nuget: PolymorphicJsonTypeInfoResolver, 0.1.10"
#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 PolymorphicJsonTypeInfoResolver as a Cake Addin
#addin nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.10

// Install PolymorphicJsonTypeInfoResolver as a Cake Tool
#tool nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.10

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. 
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
7.1.0-alpha.1 92 4/24/2023
7.0.16 1,348 4/24/2023
7.0.15 203 4/24/2023
0.1.14 162 4/24/2023
0.1.13 155 4/24/2023
0.1.12 149 4/24/2023
0.1.11 156 4/22/2023
0.1.10 153 4/21/2023
0.1.9 181 4/21/2023
0.1.8 147 4/21/2023
0.0.7 173 4/20/2023
0.0.6 166 4/20/2023
0.0.4 167 4/20/2023
0.0.3 166 4/20/2023
0.0.1 165 4/20/2023

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