ktsu.SerializationProviders.SystemTextJson 1.0.3-pre.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.

No packages depend on ktsu.SerializationProviders.SystemTextJson.

## v1.0.3-pre.1 (prerelease) Changes since v1.0.2: - Sync .github\workflows\dotnet.yml ([@ktsu[bot]](https://github.com/ktsu[bot])) - Sync scripts\PSBuild.psm1 ([@ktsu[bot]](https://github.com/ktsu[bot])) ## v1.0.2 (patch) Changes since v1.0.1: - Add .runsettings file for coverage configuration, update GitHub Actions workflow to handle skipped releases, and enhance PSBuild script for coverage file handling. Update winget manifest script to detect NuGet packages and refine project type checks. ([@matt-edmondson](https://github.com/matt-edmondson)) ## 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) Incremental prerelease update. ## 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))

.NET 6.0

.NET 7.0

.NET 8.0

.NET 9.0

.NET Standard 2.1

Version Downloads Last updated
1.0.3-pre.3 18 11/24/2025
1.0.3-pre.2 14 11/23/2025
1.0.3-pre.1 12 11/23/2025