ktsu.SerializationProvider 1.0.2

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ktsu.SerializationProvider --version 1.0.2
                    
NuGet\Install-Package ktsu.SerializationProvider -Version 1.0.2
                    
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="ktsu.SerializationProvider" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.SerializationProvider" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.SerializationProvider" />
                    
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 ktsu.SerializationProvider --version 1.0.2
                    
#r "nuget: ktsu.SerializationProvider, 1.0.2"
                    
#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.
#:package ktsu.SerializationProvider@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ktsu.SerializationProvider&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.SerializationProvider&version=1.0.2
                    
Install as a Cake Tool

ktsu.SerializationProvider

A dependency injection interface for pluggable serialization providers. Define serialization contracts once and swap implementations without changing application code.

Features

  • Unified Interface: Single interface for all serialization operations
  • Dependency Injection Ready: Easy registration with Microsoft.Extensions.DependencyInjection
  • Zero Dependencies: Pure interface library with no specific serialization library dependencies
  • Async Support: Full async/await support for all operations
  • Type Safety: Generic methods provide compile-time type safety
  • Extensible: Easy to create custom serialization providers
  • Well Tested: Comprehensive unit test coverage

Installation

dotnet add package SerializationProvider

Quick Start

1. Create a Serialization Provider

First, implement the ISerializationProvider interface:

public class JsonSerializationProvider : ISerializationProvider
{
    public string ProviderName => "System.Text.Json";
    public string ContentType => "application/json";

    public string Serialize<T>(T obj)
    {
        return System.Text.Json.JsonSerializer.Serialize(obj);
    }

    public T Deserialize<T>(string data)
    {
        if (string.IsNullOrWhiteSpace(data))
            throw new ArgumentException("Data cannot be null or empty", nameof(data));
        
        return System.Text.Json.JsonSerializer.Deserialize<T>(data)!;
    }

    public string Serialize(object obj, Type type)
    {
        return System.Text.Json.JsonSerializer.Serialize(obj, type);
    }

    public object Deserialize(string data, Type type)
    {
        if (string.IsNullOrWhiteSpace(data))
            throw new ArgumentException("Data cannot be null or empty", nameof(data));
        
        return System.Text.Json.JsonSerializer.Deserialize(data, type)!;
    }

    public Task<string> SerializeAsync<T>(T obj, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(Serialize(obj));
    }

    public Task<string> SerializeAsync(object obj, Type type, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(Serialize(obj, type));
    }

    public Task<T> DeserializeAsync<T>(string data, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(Deserialize<T>(data));
    }

    public Task<object> DeserializeAsync(string data, Type type, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(Deserialize(data, type));
    }
}

2. Register the Serialization Provider

using ktsu.SerializationProvider.Extensions;

// In your Startup.cs or Program.cs
services.AddSerializationProvider<JsonSerializationProvider>();

// Or register by instance
services.AddSerializationProvider(new JsonSerializationProvider());

// Or register by factory
services.AddSerializationProvider(serviceProvider => 
    new JsonSerializationProvider());

3. Inject and Use the Serialization Provider

public class MyService
{
    private readonly ISerializationProvider _serializationProvider;

    public MyService(ISerializationProvider serializationProvider)
    {
        _serializationProvider = serializationProvider;
    }

    public async Task<string> SerializeDataAsync<T>(T data)
    {
        return await _serializationProvider.SerializeAsync(data);
    }

    public async Task<T> DeserializeDataAsync<T>(string json)
    {
        return await _serializationProvider.DeserializeAsync<T>(json);
    }
}

API Reference

ISerializationProvider Interface

public interface ISerializationProvider
{
    string ProviderName { get; }
    string ContentType { get; }

    // Synchronous methods
    string Serialize<T>(T obj);
    string Serialize(object obj, Type type);
    T Deserialize<T>(string data);
    object Deserialize(string data, Type type);

    // Asynchronous methods
    Task<string> SerializeAsync<T>(T obj, CancellationToken cancellationToken = default);
    Task<string> SerializeAsync(object obj, Type type, CancellationToken cancellationToken = default);
    Task<T> DeserializeAsync<T>(string data, CancellationToken cancellationToken = default);
    Task<object> DeserializeAsync(string data, Type type, CancellationToken cancellationToken = default);
}

Usage Examples

Basic Serialization

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime BirthDate { get; set; }
}

// Serialize
var person = new Person { Name = "John Doe", Age = 30, BirthDate = DateTime.Now };
string json = serializationProvider.Serialize(person);

// Deserialize
var deserializedPerson = serializationProvider.Deserialize<Person>(json);

Async Operations

// Async serialize
string json = await serializationProvider.SerializeAsync(person);

// Async deserialize
var person = await serializationProvider.DeserializeAsync<Person>(json);

Type-based Operations

// When you need to work with Type objects
Type personType = typeof(Person);
string json = serializationProvider.Serialize(person, personType);
object deserializedObject = serializationProvider.Deserialize(json, personType);

Dependency Injection Registration Options

Register by Type

services.AddSerializationProvider<MyCustomSerializationProvider>();

Register by Instance

services.AddSerializationProvider(new MyCustomSerializationProvider());

Register by Factory

services.AddSerializationProvider(serviceProvider => 
    new MyCustomSerializationProvider(
        serviceProvider.GetRequiredService<ILogger<MyCustomSerializationProvider>>()
    ));

Creating Custom Serialization Providers

JSON Provider with Newtonsoft.Json

public class NewtonsoftJsonSerializationProvider : ISerializationProvider
{
    private readonly JsonSerializerSettings _settings;

    public NewtonsoftJsonSerializationProvider(JsonSerializerSettings? settings = null)
    {
        _settings = settings ?? new JsonSerializerSettings();
    }

    public string ProviderName => "Newtonsoft.Json";
    public string ContentType => "application/json";

    public string Serialize<T>(T obj)
    {
        return JsonConvert.SerializeObject(obj, _settings);
    }

    public T Deserialize<T>(string data)
    {
        if (string.IsNullOrWhiteSpace(data))
            throw new ArgumentException("Data cannot be null or empty", nameof(data));
        
        return JsonConvert.DeserializeObject<T>(data, _settings)!;
    }

    // Implement other interface methods...
}

XML Provider

public class XmlSerializationProvider : ISerializationProvider
{
    public string ProviderName => "System.Xml";
    public string ContentType => "application/xml";

    public string Serialize<T>(T obj)
    {
        var serializer = new XmlSerializer(typeof(T));
        using var stringWriter = new StringWriter();
        serializer.Serialize(stringWriter, obj);
        return stringWriter.ToString();
    }

    public T Deserialize<T>(string data)
    {
        if (string.IsNullOrWhiteSpace(data))
            throw new ArgumentException("Data cannot be null or empty", nameof(data));

        var serializer = new XmlSerializer(typeof(T));
        using var stringReader = new StringReader(data);
        return (T)serializer.Deserialize(stringReader)!;
    }

    // Implement other interface methods...
}

Best Practices

  1. Use Async Methods: For I/O operations, prefer async methods to avoid blocking threads
  2. Handle Exceptions: Wrap serialization operations in try-catch blocks for production code
  3. Validate Input: Always validate input data in your serialization provider implementations
  4. Single Provider per Application: Register only one serialization provider per application to avoid confusion
  5. Provider Naming: Use descriptive provider names to help with debugging and logging

Testing

The library includes comprehensive unit tests. Run them using:

dotnet test

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

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

Changelog

See CHANGELOG.md for a list of changes and version history.

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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on ktsu.SerializationProvider:

Package Downloads
ktsu.UniversalSerializer

A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.

ktsu.PersistenceProvider

A generic persistence provider library for .NET that supports multiple storage backends (memory, file system, application data, and temporary storage). Features type-safe persistence with dependency injection support, async/await operations, and integration with ktsu.SerializationProvider and ktsu.FileSystemProvider libraries.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3-pre.2 478 7/22/2025
1.0.3-pre.1 114 7/9/2025
1.0.2 224 6/18/2025
1.0.1 136 6/18/2025

## v1.0.2 (patch)\n\nChanges since v1.0.1:\n\n- Refactor namespaces to include 'ktsu' prefix for SerializationProvider and its extensions in source and test files. ([@matt-edmondson](https://github.com/matt-edmondson))\n