Hexalith.PolymorphicSerializations.CodeGenerators
1.6.4
See the version list below for details.
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators --version 1.6.4
NuGet\Install-Package Hexalith.PolymorphicSerializations.CodeGenerators -Version 1.6.4
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.6.4" />
<PackageVersion Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.6.4" />
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" />
paket add Hexalith.PolymorphicSerializations.CodeGenerators --version 1.6.4
#r "nuget: Hexalith.PolymorphicSerializations.CodeGenerators, 1.6.4"
#addin nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.6.4
#tool nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.6.4
Hexalith.PolymorphicSerializations
Overview
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:
- Event Sourcing (serializing different domain events)
- Message Queues (sending/receiving various message types)
- APIs returning different response types based on context
- Configuration systems loading diverse component types
- NoSQL Databases
Features
- Type Discrimination - Automatically includes type information in serialized JSON to ensure proper deserialization.
- Custom Type Resolution - Flexible system for mapping between .NET types and JSON discriminator values.
- Minimal Configuration - Simple attribute-based setup with reasonable defaults.
- High Performance - Built on the high-performance
System.Text.Json
serialization engine. - Framework Agnostic - Works with any .NET application using .NET Standard 2.0 or higher.
- No External Dependencies - Only depends on
System.Text.Json
.
Requirements
- .NET 9.0 or higher
- System.Text.Json 9.0 or higher
Installation
Install the packages via NuGet Package Manager or the .NET CLI:
dotnet add package Hexalith.PolymorphicSerializations
dotnet add package Hexalith.PolymorphicSerializations.CodeGenerators
Quick Start
1. Configure serialization by adding attributes to your model
The library uses source generators to create serialization mappers at compile time. This requires:
- Adding
partial
keyword to your classes/records - Applying
[PolymorphicSerialization]
attributes - Installing the code generator package
// 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);
2. Register the polymorphic mappers
The source generator creates a registration extension method in your project's namespace:
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
MyProject.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);
Sample Application
For more detailed usage examples and demonstrations of various features, explore the sample applications:
- Sample Application README
- Includes examples like deserializing polymorphic messages from files (
DeserializeFileMessages
).
- Includes examples like deserializing polymorphic messages from files (
Repository Structure
The repository is organized as follows:
- src Is the source code directory where you will add your package projects.
- test Contains test projects for your packages.
- examples Contains example implementations of your packages.
- Hexalith.Builds Contains shared build configurations and tools.
Contributing
Contributions are welcome! Here's how you can contribute:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please ensure your code follows the project's coding standards and includes appropriate tests.
Troubleshooting
Common Issues
- Type not recognized during deserialization: Ensure you've applied the correct attributes and registered the converter with your JsonSerializerOptions.
- Missing type discriminator: Check that your base class has the [PolymorphicSerialization] attribute.
- Serialization exceptions: Verify that all derived types have the [PolymorphicSerialization] attribute with the correct baseType parameter.
- Build errors: Make sure all classes with polymorphic attributes are marked as
partial
.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.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.