ktsu.SerializationProvider 1.0.6-pre.2
⚠️ DEPRECATED
This project is no longer maintained. Its functionality has been moved to ktsu.Abstractions.
Please migrate to the new library for continued support and updates.
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
- Use Async Methods: For I/O operations, prefer async methods to avoid blocking threads
- Handle Exceptions: Wrap serialization operations in try-catch blocks for production code
- Validate Input: Always validate input data in your serialization provider implementations
- Single Provider per Application: Register only one serialization provider per application to avoid confusion
- 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.
Showing the top 20 packages that depend on ktsu.SerializationProvider.
| Packages | Downloads |
|---|---|
|
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.
|
141 |
|
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.
|
138 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
126 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
124 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
122 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
53 |
|
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.
|
53 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
52 |
|
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.
|
16 |
|
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.
|
15 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
15 |
|
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.
|
2 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
2 |
|
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.
|
1 |
|
ktsu.UniversalSerializer
A unified serialization library for .NET that provides a consistent API for various serialization formats including JSON, XML, YAML, TOML, and MessagePack.
|
1 |
## v1.0.6-pre.2 (prerelease)
Changes since v1.0.6-pre.1:
- Bump the microsoft group with 2 updates ([@dependabot[bot]](https://github.com/dependabot[bot]))
## v1.0.6-pre.1 (prerelease)
Changes since v1.0.5:
- Sync scripts\update-winget-manifests.ps1 ([@ktsu[bot]](https://github.com/ktsu[bot]))
## v1.0.5 (patch)
Changes since v1.0.4:
- Add optional NuGet API key parameter to pipeline configuration ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.4 (patch)
Changes since v1.0.3:
- Compatibility supressions ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.3 (patch)
Changes since v1.0.2:
- Remove .github\workflows\project.yml ([@matt-edmondson](https://github.com/matt-edmondson))
- migrate to dotnet 10 ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.3-pre.2 (prerelease)
Changes since v1.0.3-pre.1:
## v1.0.3-pre.1 (prerelease)
Incremental prerelease update.
## v1.0.2 (patch)
Changes since v1.0.1:
- Refactor namespaces to include 'ktsu' prefix for SerializationProvider and its extensions in source and test files. ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1 (patch)
Changes since v1.0.0:
- Enhance Invoke-DotNetPack function to escape newlines in release notes for MSBuild compatibility ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.0 (major)
- Remove SerializationException and DeserializationException classes; update test project by removing Moq package reference. ([@matt-edmondson](https://github.com/matt-edmondson))
- Initial implementation ([@matt-edmondson](https://github.com/matt-edmondson))
.NET 8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Polyfill (>= 9.8.1)
.NET 9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Polyfill (>= 9.8.1)
.NET 10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Polyfill (>= 9.8.1)
.NET Standard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Polyfill (>= 9.8.1)
.NET Standard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.3)
- Polyfill (>= 9.8.1)
| Version | Downloads | Last updated |
|---|---|---|
| 1.0.6-pre.2 | 22 | 02/11/2026 |
| 1.0.6-pre.1 | 54 | 02/06/2026 |
| 1.0.5 | 55 | 02/05/2026 |
| 1.0.4 | 54 | 02/05/2026 |
| 1.0.3 | 53 | 02/05/2026 |