PolymorphicJsonTypeInfoResolver 8.0.27

dotnet add package PolymorphicJsonTypeInfoResolver --version 8.0.27
NuGet\Install-Package PolymorphicJsonTypeInfoResolver -Version 8.0.27
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="8.0.27" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PolymorphicJsonTypeInfoResolver --version 8.0.27
#r "nuget: PolymorphicJsonTypeInfoResolver, 8.0.27"
#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=8.0.27

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

Polymorphic Json Type Info Resolver

nuget stryker codecov maintainability Build status

Polymorphic Json Type Info Resolver allows you to configure polymorphism on the contract model without polluting the domain model with attributes. This library leverages the polymorphic type serialization feature introduced in .NET7.

Breaking Changes v8

In version 8, the automatic inclusion of all subtypes for a specified type and the validation logic have been removed. This change simplifies the library and reduces its scope. These features may be reintroduced in separate packages in the future, but for now, they are not part of this library.

What does this mean for you?

  • Manual Subtype Registration: You will now need to manually register each subtype using the With<T> method as shown in the usage section below.
  • No Validation Logic: Any custom validation logic will need to be implemented separately if required.
  • Updated Method Names: The method previously named Type has been renamed to With.
  • Builder Pattern: You now need to close the builder with a Build method.

Installation

You can install the Polymorphic Json Type Info Resolver via NuGet Package Manager or by using the .NET CLI.

NuGet Package Manager:

1. Search for "PolymorphicJsonTypeInfoResolver" in the NuGet Package Manager in Visual Studio.
2. Click "Install".

.NET CLI:

dotnet add package PolymorphicJsonTypeInfoResolver

Usage

Define your domain model:

record Box(Shape Something);
interface Shape;
record Square(double Length) : Shape;
record Circle(double Radius) : Shape;

Here's an example of how to use Polymorphic Json Type Info Resolver:

var options = new JsonSerializerOptions {
    TypeInfoResolver = new PolymorphicJsonTypeInfoResolver.Builder()
        .With<Shape>(x => x
            .DerivedTypes
            .Add<Square>("square")
            .Add<Circle>("circle"))
        .Build()
};
        
var json = JsonSerializer.Serialize(new Box(new Circle(10)), options);

The result will look like this:

{
    "Something": {
        "$type":"circle",
        "Radius":10
    }
}

In the above code snippet, we create a new instance of JsonSerializerOptions and set its TypeInfoResolver property to an instance of PolymorphicTypeInfoResolver. We then configure the resolver to serialize objects of type Shape with a $type property that specifies the derived type (Square or Circle).

Specify Options

For advanced configurations, you can specify the full JsonPolymorphismOptions when specifying a type:

 new Builder()
    .With<Shape>(new JsonPolymorphismOptions {
        TypeDiscriminatorPropertyName = "$TYPE",
        DerivedTypes =  {
            new JsonDerivedType(typeof(Circle), "circle")
        }
    })
    .Build()

Note: It is not possible to use the cleaner syntax Add<T> in this context.

Use a Factory for Options

To supply a factory for the polymorphic json options, you can use the following code:

new Builder(() => new JsonPolymorphismOptions {
        TypeDiscriminatorPropertyName = "$TYPE"
    })
    .With<Shape>(...)
    .Build()

In the above code snippet, we create a new instance of JsonSerializerOptions and set its TypeInfoResolver property to an instance of PolymorphicTypeInfoResolver. We then supply a factory function that returns an instance of JsonPolymorphismOptions with a custom $TYPE type discriminator property name.

Remark: this readme was peer-reviewed by ChatGPT.

Happy coding!

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.
  • net8.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 60 6/7/2024
7.1.0-alpha.1 93 4/24/2023
7.0.16 1,506 4/24/2023
7.0.15 205 4/24/2023
0.1.14 165 4/24/2023
0.1.13 157 4/24/2023
0.1.12 151 4/24/2023
0.1.11 158 4/22/2023
0.1.10 155 4/21/2023
0.1.9 183 4/21/2023
0.1.8 149 4/21/2023
0.0.7 176 4/20/2023
0.0.6 168 4/20/2023
0.0.4 169 4/20/2023
0.0.3 168 4/20/2023
0.0.1 167 4/20/2023

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