ktsu.SerializationProvider
1.0.5
Prefix Reserved
See the version list below for details.
dotnet add package ktsu.SerializationProvider --version 1.0.5
NuGet\Install-Package ktsu.SerializationProvider -Version 1.0.5
<PackageReference Include="ktsu.SerializationProvider" Version="1.0.5" />
<PackageVersion Include="ktsu.SerializationProvider" Version="1.0.5" />
<PackageReference Include="ktsu.SerializationProvider" />
paket add ktsu.SerializationProvider --version 1.0.5
#r "nuget: ktsu.SerializationProvider, 1.0.5"
#:package ktsu.SerializationProvider@1.0.5
#addin nuget:?package=ktsu.SerializationProvider&version=1.0.5
#tool nuget:?package=ktsu.SerializationProvider&version=1.0.5
⚠️ 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.
| 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 is compatible. 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 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 is compatible. 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. |
| .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 is compatible. |
| .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
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Polyfill (>= 9.8.1)
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Polyfill (>= 9.8.1)
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Polyfill (>= 9.8.1)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Polyfill (>= 9.8.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.2)
- Polyfill (>= 9.8.1)
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.6-pre.22 | 39 | 4/22/2026 |
| 1.0.6-pre.21 | 47 | 4/15/2026 |
| 1.0.6-pre.20 | 48 | 4/13/2026 |
| 1.0.6-pre.19 | 51 | 4/10/2026 |
| 1.0.6-pre.18 | 45 | 4/8/2026 |
| 1.0.6-pre.17 | 51 | 4/6/2026 |
| 1.0.6-pre.16 | 49 | 4/3/2026 |
| 1.0.6-pre.15 | 48 | 3/26/2026 |
| 1.0.6-pre.14 | 56 | 3/17/2026 |
| 1.0.6-pre.13 | 56 | 3/16/2026 |
| 1.0.6-pre.12 | 53 | 3/13/2026 |
| 1.0.6-pre.11 | 54 | 3/11/2026 |
| 1.0.6-pre.10 | 46 | 3/6/2026 |
| 1.0.6-pre.9 | 52 | 3/5/2026 |
| 1.0.6-pre.8 | 47 | 3/4/2026 |
| 1.0.6-pre.7 | 49 | 3/2/2026 |
| 1.0.6-pre.6 | 57 | 2/27/2026 |
| 1.0.6-pre.5 | 50 | 2/25/2026 |
| 1.0.6-pre.4 | 55 | 2/23/2026 |
| 1.0.5 | 343 | 2/5/2026 |
## 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))