Hexalith.PolymorphicSerializations 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Hexalith.PolymorphicSerializations --version 1.2.0
                    
NuGet\Install-Package Hexalith.PolymorphicSerializations -Version 1.2.0
                    
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" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hexalith.PolymorphicSerializations" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Hexalith.PolymorphicSerializations" />
                    
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 --version 1.2.0
                    
#r "nuget: Hexalith.PolymorphicSerializations, 1.2.0"
                    
#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&version=1.2.0
                    
Install Hexalith.PolymorphicSerializations as a Cake Addin
#tool nuget:?package=Hexalith.PolymorphicSerializations&version=1.2.0
                    
Install Hexalith.PolymorphicSerializations as a Cake Tool

Hexalith.PolymorphicSerializations

A .NET library that simplifies polymorphic serialization and deserialization using System.Text.Json.

Overview

Hexalith.PolymorphicSerializations provides utilities and helpers for handling polymorphic serialization scenarios in .NET applications. It enables type-based JSON serialization and deserialization by managing type discriminators and mapping between base types and their derived implementations.

Features

  • Polymorphic serialization and deserialization of object hierarchies
  • Type discrimination using a customizable property name
  • Version-aware type handling
  • Fluent builder API for serialization configuration
  • Custom attribute-based type metadata

Installation

dotnet add package Hexalith.PolymorphicSerializations

Usage

Basic Example

using Hexalith.PolymorphicSerializations;
using System.Text.Json;

// Create JSON serializer options with polymorphic resolution
JsonSerializerOptions options = new()
{
    WriteIndented = true,
    TypeInfoResolver = new PolymorphicSerializationResolver()
};

// Serialize polymorphic objects
string json = JsonSerializer.Serialize(myPolymorphicObject, options);

// Deserialize back to the correct concrete type
var deserialized = JsonSerializer.Deserialize<BaseType>(json, options);

Defining Polymorphic Types

Create base types that derive from PolymorphicRecordBase:

[DataContract]
public abstract record Animal : PolymorphicRecordBase
{
    public string Name { get; init; }
}

[PolymorphicSerialization(version: 1)]
public record Dog(string Name, string Breed) : Animal;

[PolymorphicSerialization(name: "Feline", version: 2)]
public record Cat(string Name, int Lives) : Animal;

Registering Type Mappers

Use the builder pattern to register type mappers:

// Create mappers with fluent API
var mappers = new PolymorphicSerializationResolverBuilder()
    .AddMapper(new PolymorphicSerializationMapper<Dog, Animal>("Dog"))
    .AddMapper(new PolymorphicSerializationMapper<Cat, Animal>("FelineV2"))
    .Build();

// Register mappers globally
PolymorphicSerializationResolver.TryAddDefaultMappers(mappers);

Using Default JSON Serializer Options

The library provides default serializer options:

// Use pre-configured default options
string json = JsonSerializer.Serialize(myAnimal, PolymorphicHelper.DefaultJsonSerializerOptions);
Animal deserializedAnimal = JsonSerializer.Deserialize<Animal>(json, PolymorphicHelper.DefaultJsonSerializerOptions);

Key Components

PolymorphicSerializationAttribute

This attribute provides metadata for a polymorphic class:

[PolymorphicSerialization(name: "CustomName", version: 2, baseType: typeof(BaseClass))]
public record DerivedClass : BaseClass;

Parameters:

  • name: Custom name for the type (defaults to the class name)
  • version: Version of the type (defaults to 1)
  • baseType: Base type of the class (optional)

PolymorphicRecordBase

A marker base record that can be used as the root of polymorphic hierarchies.

PolymorphicSerializationResolver

Extends DefaultJsonTypeInfoResolver to handle polymorphic serialization:

  • Maintains a registry of type mappers
  • Configures JSON serialization with type discriminators
  • Handles derived type resolution

PolymorphicSerializationMapper

Maps derived types to their base types with type discriminators:

var mapper = new PolymorphicSerializationMapper<ConcreteType, BaseType>("TypeDiscriminator");

PolymorphicHelper

Provides utility methods:

  • Default JSON serializer options
  • Type discriminator property name (default: "$type")
  • Helper methods to extract polymorphic type information

Advanced Usage

Custom Type Discrimination

You can extract type information from any type or object instance:

Type myType = typeof(Dog);
var (name, typeName, version) = myType.GetPolymorphicTypeDiscriminator();

// Or from an instance
Dog dog = new("Rex", "Golden Retriever");
var typeInfo = dog.GetPolymorphicTypeDiscriminator();

Versioning

The library supports versioning of types:

// Version 1 (no suffix)
[PolymorphicSerialization(version: 1)]
public record OrderV1 : Order;

// Version 2 (with suffix V2)
[PolymorphicSerialization(version: 2)]
public record OrderV2 : Order;

License

This project is licensed under the MIT License - see the LICENSE file for details.

Company

Developed and maintained by ITANEO

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Hexalith.PolymorphicSerializations:

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 1,722 4/18/2025
1.6.5 133 4/18/2025
1.6.4 116 4/18/2025
1.6.3 157 4/18/2025
1.6.2 324 4/1/2025
1.6.1 144 3/31/2025
1.6.0 132 3/31/2025
1.5.0 137 3/31/2025
1.4.0 134 3/31/2025
1.3.0 123 3/30/2025
1.2.0 123 3/30/2025
1.1.0 133 3/30/2025
1.0.0 86 3/29/2025