ktsu.SerializationProviders.SystemTextJson
1.0.1
Prefix Reserved
dotnet add package ktsu.SerializationProviders.SystemTextJson --version 1.0.1
NuGet\Install-Package ktsu.SerializationProviders.SystemTextJson -Version 1.0.1
<PackageReference Include="ktsu.SerializationProviders.SystemTextJson" Version="1.0.1" />
<PackageVersion Include="ktsu.SerializationProviders.SystemTextJson" Version="1.0.1" />
<PackageReference Include="ktsu.SerializationProviders.SystemTextJson" />
paket add ktsu.SerializationProviders.SystemTextJson --version 1.0.1
#r "nuget: ktsu.SerializationProviders.SystemTextJson, 1.0.1"
#:package ktsu.SerializationProviders.SystemTextJson@1.0.1
#addin nuget:?package=ktsu.SerializationProviders.SystemTextJson&version=1.0.1
#tool nuget:?package=ktsu.SerializationProviders.SystemTextJson&version=1.0.1
ktsu Serialization Providers
A collection of serialization providers implementing ktsu.Abstractions.ISerializationProvider
for popular .NET JSON libraries. This library provides a consistent, standardized interface for serialization across different JSON libraries.
๐ฆ Available Providers
- NewtonsoftJson - Provider for Newtonsoft.Json
- SystemTextJson - Provider for System.Text.Json
๐ Installation
Newtonsoft.Json Provider
dotnet add package ktsu.SerializationProviders.NewtonsoftJson
System.Text.Json Provider
dotnet add package ktsu.SerializationProviders.SystemTextJson
๐ก Usage
Basic Usage
using ktsu.SerializationProvider;
// Using Newtonsoft.Json provider
var newtonsoftProvider = new NewtonsoftJson();
// Using System.Text.Json provider
var systemTextJsonProvider = new SystemTextJson();
// Both implement ISerializationProvider
public void ProcessData(ISerializationProvider provider)
{
// Serialize an object to TextWriter
var data = new { Name = "John", Age = 30 };
using var writer = new StringWriter();
bool success = provider.TrySerialize(data, writer);
if (success)
{
string json = writer.ToString();
Console.WriteLine(json);
}
// Deserialize from byte span
byte[] jsonBytes = Encoding.UTF8.GetBytes("{\"Name\":\"Jane\",\"Age\":25}");
var result = provider.Deserialize<Person>(jsonBytes.AsSpan());
}
Dependency Injection
using Microsoft.Extensions.DependencyInjection;
using ktsu.Abstractions;
using ktsu.SerializationProvider;
var services = new ServiceCollection();
// Register your preferred provider
services.AddSingleton<ISerializationProvider, NewtonsoftJson>();
// or
services.AddSingleton<ISerializationProvider, SystemTextJson>();
var serviceProvider = services.BuildServiceProvider();
var serializer = serviceProvider.GetRequiredService<ISerializationProvider>();
๐ง API Reference
ISerializationProvider Interface
Both providers implement the ktsu.Abstractions.ISerializationProvider
interface:
public interface ISerializationProvider
{
T? Deserialize<T>(ReadOnlySpan<byte> data);
bool TrySerialize(object obj, TextWriter writer);
}
Methods
Deserialize<T>(ReadOnlySpan<byte> data)
- Deserializes UTF-8 encoded JSON byte data into a specified type
- Returns
default(T)
if data is empty or deserialization fails - Handles common exceptions gracefully
TrySerialize(object obj, TextWriter writer)
- Attempts to serialize an object to JSON and write to the specified TextWriter
- Returns
true
if successful,false
otherwise - Handles serialization exceptions gracefully
๐ฏ Features
- Consistent API - Same interface regardless of underlying JSON library
- Error Handling - Graceful handling of serialization/deserialization errors
- Performance - Optimized for common use cases
- Multi-Target - Supports .NET 9.0, 8.0, 7.0, 6.0, and .NET Standard 2.1
- Dependency Injection Ready - Easy integration with DI containers
๐งช Error Handling
Both providers handle errors gracefully:
- Deserialization: Returns
default(T)
on failure (empty data, invalid JSON, etc.) - Serialization: Returns
false
on failure, with no exceptions thrown
// Safe deserialization - won't throw
var result = provider.Deserialize<MyClass>(invalidJsonBytes);
if (result == null)
{
// Handle deserialization failure
}
// Safe serialization - won't throw
using var writer = new StringWriter();
if (!provider.TrySerialize(problematicObject, writer))
{
// Handle serialization failure
}
๐ Migration Between Providers
Since both providers implement the same interface, switching between them is seamless:
// Easy to switch providers
ISerializationProvider provider = useNewtonsoft
? new NewtonsoftJson()
: new SystemTextJson();
๐ Requirements
- .NET 9.0, 8.0, 7.0, 6.0, or .NET Standard 2.1
- ktsu.Abstractions package
๐ License
Licensed under the MIT License. See LICENSE.md for details.
๐ค Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- ktsu.Abstractions (>= 1.0.7)
- System.Text.Json (>= 9.0.8)
-
net6.0
- ktsu.Abstractions (>= 1.0.7)
- System.Text.Json (>= 9.0.8)
-
net7.0
- ktsu.Abstractions (>= 1.0.7)
- System.Text.Json (>= 9.0.8)
-
net8.0
- ktsu.Abstractions (>= 1.0.7)
- System.Text.Json (>= 9.0.8)
-
net9.0
- ktsu.Abstractions (>= 1.0.7)
- System.Text.Json (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.1 | 96 | 8/14/2025 |
1.0.1-pre.1 | 95 | 8/12/2025 |
1.0.0 | 89 | 8/11/2025 |
## v1.0.1 (patch)
Changes since v1.0.0:
- Add SHA384 and SHA512 hash providers, along with FNV1_32, FNV1a_32, FNV1_64, and FNV1a_64 implementations. Update Common.sln and add corresponding unit tests for all new providers. Enhance existing tests for dependency injection and serialization. Include necessary project files and suppressions for compatibility. ([@matt-edmondson](https://github.com/matt-edmondson))
## v1.0.1-pre.1 (prerelease)
Changes since v1.0.0:
## v1.0.0 (major)
- Add MD5HashProvider implementation and project files ([@matt-edmondson](https://github.com/matt-edmondson))
- Initial commit ([@matt-edmondson](https://github.com/matt-edmondson))