PolymorphicJsonTypeInfoResolver 0.1.11
See the version list below for details.
dotnet add package PolymorphicJsonTypeInfoResolver --version 0.1.11
NuGet\Install-Package PolymorphicJsonTypeInfoResolver -Version 0.1.11
<PackageReference Include="PolymorphicJsonTypeInfoResolver" Version="0.1.11" />
paket add PolymorphicJsonTypeInfoResolver --version 0.1.11
#r "nuget: PolymorphicJsonTypeInfoResolver, 0.1.11"
// Install PolymorphicJsonTypeInfoResolver as a Cake Addin #addin nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.11 // Install PolymorphicJsonTypeInfoResolver as a Cake Tool #tool nuget:?package=PolymorphicJsonTypeInfoResolver&version=0.1.11
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.
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
Here's an example of how to use Polymorphic Json Type Info Resolver:
var options = new JsonSerializerOptions {
TypeInfoResolver = new PolymorphicTypeInfoResolver()
.Type<Shape>(x => x
.DerivedTypes
.Add<Square>("square")
.Add<Circle>("circle"))
};
var json = JsonSerializer.Serialize(new Box(new Circle(10)), options);
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). Finally, we serialize a Box object containing a Circle object with a radius of 10.
Remark: the
Add
method can use the type name for discriminator, but I'm not sure that's a good idea after all. Since it is part of your contract you may want to decouple it so it does not automatically change as part of refactoring.
{
"Shape": {
"$type":"circle",
"Radius":10
}
}
To supply a factory for the polymorphic JSON options, you can use the following code:
var options = new JsonSerializerOptions {
TypeInfoResolver = new PolymorphicTypeInfoResolver(options: () => new JsonPolymorphismOptions {
TypeDiscriminatorPropertyName = "$TYPE"
})
};
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.
To opt-in to all derived types from its own assembly or the specified assembly, you can use the following code:
new JsonSerializerOptions {
TypeInfoResolver = new PolymorphicTypeInfoResolver()
.Type<Shape>(x => x
.DerivedTypes
.AddAllAssignableTo<Shape>(t => t.Name)
.AddAllAssignableTo<Shape, Parallelogram>(t => t.Name))
};
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 all derived types assignable to Shape
from its own assembly and the assembly where Parallelogram
is located with their name
as the type discriminator.
Remark: this readme was peer-reviewed by ChatGPT.
Happy coding!
Product | Versions 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. |
-
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 | 1,889 | 6/7/2024 |
7.1.0-alpha.1 | 109 | 4/24/2023 |
7.0.16 | 1,701 | 4/24/2023 |
7.0.15 | 234 | 4/24/2023 |
0.1.14 | 201 | 4/24/2023 |
0.1.13 | 178 | 4/24/2023 |
0.1.12 | 187 | 4/24/2023 |
0.1.11 | 190 | 4/22/2023 |
0.1.10 | 180 | 4/21/2023 |
0.1.9 | 215 | 4/21/2023 |
0.1.8 | 177 | 4/21/2023 |
0.0.7 | 204 | 4/20/2023 |
0.0.6 | 195 | 4/20/2023 |
0.0.4 | 194 | 4/20/2023 |
0.0.3 | 199 | 4/20/2023 |
0.0.1 | 199 | 4/20/2023 |
Breaking change: using `JsonPolymorphismOptions` instead of wrapping in own builder.