PolymorphicJsonTypeInfoResolver 8.0.27
dotnet add package PolymorphicJsonTypeInfoResolver --version 8.0.27
NuGet\Install-Package PolymorphicJsonTypeInfoResolver -Version 8.0.27
<PackageReference Include="PolymorphicJsonTypeInfoResolver" Version="8.0.27" />
paket add PolymorphicJsonTypeInfoResolver --version 8.0.27
#r "nuget: PolymorphicJsonTypeInfoResolver, 8.0.27"
// 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
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 toWith
. - 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 | Versions 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. |
-
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 | 1,737 | 6/7/2024 |
7.1.0-alpha.1 | 107 | 4/24/2023 |
7.0.16 | 1,699 | 4/24/2023 |
7.0.15 | 232 | 4/24/2023 |
0.1.14 | 199 | 4/24/2023 |
0.1.13 | 176 | 4/24/2023 |
0.1.12 | 185 | 4/24/2023 |
0.1.11 | 188 | 4/22/2023 |
0.1.10 | 178 | 4/21/2023 |
0.1.9 | 213 | 4/21/2023 |
0.1.8 | 175 | 4/21/2023 |
0.0.7 | 202 | 4/20/2023 |
0.0.6 | 193 | 4/20/2023 |
0.0.4 | 192 | 4/20/2023 |
0.0.3 | 197 | 4/20/2023 |
0.0.1 | 195 | 4/20/2023 |
Breaking change: using `JsonPolymorphismOptions` instead of wrapping in own builder.