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
                    
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.SerializationProviders.SystemTextJson" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ktsu.SerializationProviders.SystemTextJson" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ktsu.SerializationProviders.SystemTextJson" />
                    
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.SerializationProviders.SystemTextJson --version 1.0.1
                    
#r "nuget: ktsu.SerializationProviders.SystemTextJson, 1.0.1"
                    
#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.SerializationProviders.SystemTextJson@1.0.1
                    
#: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.SerializationProviders.SystemTextJson&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ktsu.SerializationProviders.SystemTextJson&version=1.0.1
                    
Install as a Cake Tool

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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))