Hexalith.PolymorphicSerializations.CodeGenerators
1.2.0
See the version list below for details.
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators --version 1.2.0
NuGet\Install-Package Hexalith.PolymorphicSerializations.CodeGenerators -Version 1.2.0
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.2.0" />
<PackageVersion Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.2.0" />
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" />
paket add Hexalith.PolymorphicSerializations.CodeGenerators --version 1.2.0
#r "nuget: Hexalith.PolymorphicSerializations.CodeGenerators, 1.2.0"
#:package Hexalith.PolymorphicSerializations.CodeGenerators@1.2.0
#addin nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.2.0
#tool nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.2.0
Hexalith.PolymorphicSerializations.CodeGenerators
Overview
The Hexalith.PolymorphicSerializations.CodeGenerators package provides a source generator for System.Text.Json polymorphic serialization in .NET. It automatically generates serialization mapper classes and DI registration code for types marked with the PolymorphicSerializationAttribute.
This library simplifies the implementation of polymorphic serialization in .NET applications by eliminating the need to write boilerplate code for serialization mapping.
Features
- Automatically generates mapper classes for polymorphic serialization
- Creates DI registration extension methods for easy integration with dependency injection
- Supports versioning of polymorphic types
- Works with both class and record types
- Provides compile-time validation of inheritance hierarchy
- Integrates with System.Text.Json
Installation
Add the package to your project:
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators
Also add the core package:
dotnet add package Hexalith.PolymorphicSerializations
Usage
1. Define Your Polymorphic Types
Create a base class/record that inherits from PolymorphicRecordBase:
using Hexalith.PolymorphicSerializations;
[PolymorphicSerialization]
public abstract record MyBaseRecord(string Id);
Create derived classes/records and mark them with the PolymorphicSerializationAttribute:
[PolymorphicSerialization]
public record FirstDerivedType(string Id, string Name, int Value)
: MyBaseRecord(Id);
[PolymorphicSerialization("CustomType", 2)]
public record SecondDerivedType(string Id, string Description, bool IsActive)
: MyBaseRecord(Id);
2. Use the Generated Extension Methods
The source generator will create extension methods to register the mappers with dependency injection:
// In your startup/program.cs
using YourNamespace.Extensions;
services.AddYourNamespacePolymorphicMappers();
// Or for scenarios without DI
YourNamespace.Extensions.YourNamespace.RegisterPolymorphicMappers();
3. Configure JSON Serialization
Use the PolymorphicSerializationResolver with System.Text.Json:
using System.Text.Json;
using Hexalith.PolymorphicSerializations;
var options = new JsonSerializerOptions
{
WriteIndented = true,
TypeInfoResolver = new PolymorphicSerializationResolver()
};
// Serialize polymorphic objects
string json = JsonSerializer.Serialize<MyBaseRecord>(derivedInstance, options);
// Deserialize to the correct type
MyBaseRecord result = JsonSerializer.Deserialize<MyBaseRecord>(json, options);
Or use the pre-configured options:
using Hexalith.PolymorphicSerializations;
// Get default options with PolymorphicSerializationResolver configured
var options = PolymorphicHelper.DefaultJsonSerializerOptions;
Generated Code
For each class/record marked with PolymorphicSerializationAttribute, the generator produces:
- A mapper class that implements
IPolymorphicSerializationMapper - Extension methods for registering all mappers with dependency injection
- Static methods for registering with the default
PolymorphicSerializationResolver
Attribute Parameters
The PolymorphicSerializationAttribute accepts the following parameters:
name(optional): Custom type discriminator name (defaults to class name)version(optional): Version number (defaults to 1)baseType(optional): Explicit base type specification
Type Discriminator Format
Type discriminators are generated as follows:
- For version 1:
{name} - For version 2+:
{name}V{version}
Inheritance Requirements
Classes/records marked with PolymorphicSerializationAttribute must either:
- Inherit from
PolymorphicRecordBase - Inherit from a class/record that is also marked with
PolymorphicSerializationAttribute
The source generator emits compilation errors when these requirements are not met.
JSON Format
Serialized objects include a type discriminator property ($type) that is used during deserialization:
{
"$type": "FirstDerivedType",
"Id": "123",
"Name": "Example",
"Value": 42
}
License
Licensed under the MIT License. See the LICENSE file in the project root for details.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (8)
Showing the top 5 NuGet packages that depend on Hexalith.PolymorphicSerializations.CodeGenerators:
| Package | Downloads |
|---|---|
|
Hexalith.Domain.Abstractions
Hexalith is a set of libraries to build a micro-service architecture. |
|
|
Hexalith.Application.Abstractions
Hexalith is a set of libraries to build a micro-service architecture. |
|
|
Hexalith.Domain.UserConversationProfiles
Hexalith is a set of libraries to build a micro-service architecture. |
|
|
Hexalith.Domain.Surveys
Hexalith is a set of libraries to build a micro-service architecture. |
|
|
Hexalith.Domain.Dimensions
Hexalith is a set of libraries to build a micro-service architecture. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.9.1 | 279 | 12/6/2025 |
| 1.9.0 | 951 | 11/30/2025 |
| 1.8.1 | 346 | 11/30/2025 |
| 1.8.0 | 1,309 | 11/13/2025 |
| 1.7.1 | 462 | 10/13/2025 |
| 1.7.0 | 786 | 9/20/2025 |
| 1.6.15 | 449 | 8/10/2025 |
| 1.6.14 | 762 | 8/5/2025 |
| 1.6.13 | 2,041 | 7/24/2025 |
| 1.6.12 | 2,477 | 7/9/2025 |
| 1.6.11 | 1,354 | 7/6/2025 |
| 1.6.10 | 1,767 | 7/4/2025 |
| 1.6.9 | 600 | 6/29/2025 |
| 1.6.8 | 216 | 6/24/2025 |
| 1.6.7 | 5,789 | 6/13/2025 |
| 1.6.6 | 3,918 | 4/18/2025 |
| 1.6.5 | 179 | 4/18/2025 |
| 1.6.4 | 173 | 4/18/2025 |
| 1.6.3 | 215 | 4/18/2025 |
| 1.6.2 | 280 | 4/1/2025 |
| 1.6.1 | 185 | 3/31/2025 |
| 1.6.0 | 186 | 3/31/2025 |
| 1.5.0 | 189 | 3/31/2025 |
| 1.4.0 | 193 | 3/31/2025 |
| 1.3.0 | 178 | 3/30/2025 |
| 1.2.0 | 193 | 3/30/2025 |
| 1.1.0 | 190 | 3/30/2025 |
| 1.0.0 | 198 | 3/30/2025 |