![]() |
VOOZH | about |
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators --version 1.13.0
NuGet\Install-Package Hexalith.PolymorphicSerializations.CodeGenerators -Version 1.13.0
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.13.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageVersion Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.13.0" />Directory.Packages.props
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>Project file
paket add Hexalith.PolymorphicSerializations.CodeGenerators --version 1.13.0
#r "nuget: Hexalith.PolymorphicSerializations.CodeGenerators, 1.13.0"
#:package Hexalith.PolymorphicSerializations.CodeGenerators@1.13.0
#addin nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.13.0Install as a Cake Addin
#tool nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.13.0Install as a Cake Tool
👁 License: MIT
👁 Discord
👁 Build status
👁 NuGet
👁 Latest
👁 Codacy Badge
👁 Quality Gate Status
👁 Security Rating
👁 Maintainability Rating
👁 Code Smells
👁 Lines of Code
👁 Technical Debt
👁 Reliability Rating
👁 Duplicated Lines (%)
👁 Vulnerabilities
👁 Bugs
Hexalith.PolymorphicSerializations provides robust support for polymorphic serialization and deserialization in .NET applications, integrating seamlessly with System.Text.Json. It simplifies handling complex object hierarchies where instances of derived types need to be serialized and deserialized based on a common base type or interface.
This library is particularly useful when dealing with scenarios like:
System.Text.Json serialization engine.System.Text.Json.Install the packages via NuGet Package Manager or the .NET CLI:
dotnet add package Hexalith.PolymorphicSerializations
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators
The library uses source generators to create serialization mappers at compile time. This requires:
partial keyword to your classes/records[PolymorphicSerialization] attributes// Basic class with no inheritance
// The 'partial' keyword allows the source generator to extend this type
// The serialized JSON will include a discriminator with value "Car"
[PolymorphicSerialization]
public partial record Car(string Name, string EnergyType);
// Polymorphic inheritance example
// Base class must be marked as polymorphic
[PolymorphicSerialization]
public abstract partial record Animal(string Name);
// Derived class specifying its base type
// The serialized JSON will include a discriminator with value "Dog"
[PolymorphicSerialization(baseType: typeof(Animal))]
public partial record Dog(string Name, bool Dangerous) : Animal(Name);
// Another derived class
// The serialized JSON will include a discriminator with value "Cat"
[PolymorphicSerialization(baseType: typeof(Animal))]
public partial record Cat(string Name, string Color) : Animal(Name);
// Versioned class example
// The serialized JSON will include a discriminator with value "CatV2"
// This supports versioning of your data models
[PolymorphicSerialization("Cat", 2, typeof(Animal))]
public partial record NewCatVersion2(string Name, bool LikesCatnip) : Animal(Name);
The source generator creates a registration extension method in your project's namespace. The generated class follows the pattern {AssemblyName}Serialization:
using System.Text.Json;
using MyProject.Extensions; // Contains the generated extension method
using Hexalith.PolymorphicSerializations;
// Register all polymorphic mappers defined in your project
// This connects your model classes to the serialization system
// The generated class name follows the pattern: {AssemblyName}Serialization
MyProjectSerialization.RegisterPolymorphicMappers();
// Serialize the Car object. You need to specify polymorphic deserialization by using the Polymorphic type.
string json = JsonSerializer.Serialize<Polymorphic>(new Car("Volvo", "Electric"), PolymorphicHelper.DefaultJsonSerializerOptions);
// Deserialize the Car object. You need to specify polymorphic deserialization by using the Polymorphic type.
var value = JsonSerializer.Deserialize<Polymorphic>(json, PolymorphicHelper.DefaultJsonSerializerOptions);
For more detailed usage examples and demonstrations of various features, explore the sample applications:
DeserializeFileMessages).The repository is organized as follows:
Contributions are welcome! Here's how you can contribute:
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)Please ensure your code follows the project's coding standards and includes appropriate tests.
partial.This project is licensed under the MIT License - see the file for details.
When referencing the Hexalith.PolymorphicSerializations.CodeGenerators package in your projects, ensure you set it up correctly to work as an analyzer:
<ItemGroup>
<PackageReference Include="Hexalith.PolymorphicSerializations" />
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" PrivateAssets="all" />
</ItemGroup>
The PrivateAssets="all" attribute is critical for the source generator to work properly, ensuring that the analyzer is only used during compilation and isn't referenced by consumers of your assembly.
If you continue to have issues with the analyzer not being detected, you can try adding this more explicit reference:
<ItemGroup>
<PackageReference Include="Hexalith.PolymorphicSerializations" />
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" PrivateAssets="all" GeneratePathProperty="true" />
<Analyzer Include="$(PkgHexalith_PolymorphicSerializations_CodeGenerators)\analyzers\dotnet\cs\Hexalith.PolymorphicSerializations.CodeGenerators.dll" />
</ItemGroup>
Make sure to rebuild your solution after making these changes.
Learn more about Target Frameworks and .NET Standard.
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. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.13.0 | 42 | 6/17/2026 |
| 1.12.0 | 54 | 6/14/2026 |
| 1.11.0 | 403 | 1/3/2026 |
| 1.10.1 | 118 | 1/3/2026 |
| 1.10.0 | 133 | 1/3/2026 |
| 1.9.2 | 400 | 12/21/2025 |
| 1.9.1 | 1,047 | 12/6/2025 |
| 1.9.0 | 1,015 | 11/30/2025 |
| 1.8.1 | 398 | 11/30/2025 |
| 1.8.0 | 1,424 | 11/13/2025 |
| 1.7.1 | 505 | 10/13/2025 |
| 1.7.0 | 846 | 9/20/2025 |
| 1.6.15 | 491 | 8/10/2025 |
| 1.6.14 | 805 | 8/5/2025 |
| 1.6.13 | 2,089 | 7/24/2025 |
| 1.6.12 | 2,523 | 7/9/2025 |
| 1.6.11 | 1,403 | 7/6/2025 |
| 1.6.10 | 1,812 | 7/4/2025 |
| 1.6.9 | 642 | 6/29/2025 |
| 1.6.8 | 258 | 6/24/2025 |