Hexalith.PolymorphicSerializations.CodeGenerators 1.6.4

There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.6.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexalith.PolymorphicSerializations.CodeGenerators" Version="1.6.4" />
                    
Directory.Packages.props
<PackageReference Include="Hexalith.PolymorphicSerializations.CodeGenerators" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hexalith.PolymorphicSerializations.CodeGenerators --version 1.6.4
                    
#r "nuget: Hexalith.PolymorphicSerializations.CodeGenerators, 1.6.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.6.4
                    
Install Hexalith.PolymorphicSerializations.CodeGenerators as a Cake Addin
#tool nuget:?package=Hexalith.PolymorphicSerializations.CodeGenerators&version=1.6.4
                    
Install Hexalith.PolymorphicSerializations.CodeGenerators as a Cake Tool

Hexalith.PolymorphicSerializations

License: MIT Discord Build status NuGet Latest Coverity Scan Build Status Codacy Badge Quality Gate Status Security Rating Maintainability Rating Code Smells Lines of Code Technical Debt Reliability Rating Duplicated Lines (%) Vulnerabilities Bugs

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:

  1. Adding partial keyword to your classes/records
  2. Applying [PolymorphicSerialization] attributes
  3. 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:

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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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.6.6 405 4/18/2025
1.6.5 96 4/18/2025
1.6.4 85 4/18/2025
1.6.3 128 4/18/2025
1.6.2 196 4/1/2025
1.6.1 141 3/31/2025
1.6.0 130 3/31/2025
1.5.0 138 3/31/2025
1.4.0 140 3/31/2025
1.3.0 127 3/30/2025
1.2.0 131 3/30/2025
1.1.0 133 3/30/2025
1.0.0 136 3/30/2025