Oakrey.Applications.Json 3.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Oakrey.Applications.Json --version 3.0.4
                    
NuGet\Install-Package Oakrey.Applications.Json -Version 3.0.4
                    
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="Oakrey.Applications.Json" Version="3.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oakrey.Applications.Json" Version="3.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Oakrey.Applications.Json" />
                    
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 Oakrey.Applications.Json --version 3.0.4
                    
#r "nuget: Oakrey.Applications.Json, 3.0.4"
                    
#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 Oakrey.Applications.Json@3.0.4
                    
#: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=Oakrey.Applications.Json&version=3.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Oakrey.Applications.Json&version=3.0.4
                    
Install as a Cake Tool

Oakrey.Applications.Json

A .NET 10 class library that provides stream-based JSON serialization and deserialization through a single injectable service interface. It uses System.Text.Json internally, delegates all file I/O to IFileService, and instruments every operation with OpenTelemetry tracing and structured logging.

Main features

  • Deserialize a JSON file to a typed object, synchronously or asynchronously with CancellationToken support
  • Serialize a typed object to a JSON file, synchronously or asynchronously
  • Batch-load all JSON files from a directory into a List<T> asynchronously
  • Batch-save a collection of IName objects to individual files named after each object
  • Stream-based I/O � no intermediate string allocation during serialization or deserialization
  • All key methods are virtual on JsonService, allowing subclass overrides
  • Every operation emits an OpenTelemetry activity span and structured log entries

Architecture

classDiagram
    class IJsonService {
        +LoadJson~T~(fileName) T
        +LoadJsonAsync~T~(fileName, ct) Task~T~
        +LoadObjectsFromJsonFilesAsync~T~(folder, extension, ct) Task~List~T~~
        +SaveJson~T~(value, fileName)
        +SaveJsonAsync~T~(value, fileName)
        +SaveJsonFiles~T~(jsons, folder, extension)
    }

    class JsonService {
        #IFileService fileService
        #ILogger logger
        #ITracing tracer
        +JsonService()
        +JsonService(IFileService)
    }

    class IFileService {
        +LoadFileStream(fileName) FileStream
        +CreateSaveStream(fileName) FileStream
        +GetFilesPaths(folder, extension) string[]
    }

    IJsonService <|.. JsonService
    JsonService --> IFileService

Requirements

  • .NET 10 or higher
  • Oakrey.Applications.Files (transitively provides IFileService)
  • Oakrey.Collections 2.0.0 or higher (provides IName used by SaveJsonFiles)

Installation

NuGet Package Manager

  1. In Visual Studio open Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  2. Search for Oakrey.Applications.Json and click Install.

.NET CLI

dotnet add package Oakrey.Applications.Json

Package Manager Console

Install-Package Oakrey.Applications.Json

Configuration

JsonService can be constructed with or without a custom IFileService. Register with the DI container of your choice:

// Uses the default FileService internally
services.AddSingleton<IJsonService, JsonService>();

// Bring your own IFileService
services.AddSingleton<IFileService, FileService>();
services.AddSingleton<IJsonService, JsonService>();

Oakrey.Log and Oakrey.Telemetry must be configured separately before JsonService is resolved.

Example usage

public class ConfigRepository(IJsonService jsonService)
{
    private const string ConfigFolder = "config";
    private const string Extension = "json";

    // Load a single config file
    public AppConfig Load(string fileName)
    {
        return jsonService.LoadJson<AppConfig>(fileName);
    }

    // Load a single config file asynchronously
    public async Task<AppConfig> LoadAsync(string fileName, CancellationToken ct)
    {
        return await jsonService.LoadJsonAsync<AppConfig>(fileName, ct);
    }

    // Load all config files from a directory
    public async Task<List<AppConfig>> LoadAllAsync(CancellationToken ct)
    {
        return await jsonService.LoadObjectsFromJsonFilesAsync<AppConfig>(ConfigFolder, Extension, ct);
    }

    // Save a single config
    public void Save(AppConfig config, string fileName)
    {
        jsonService.SaveJson(config, fileName);
    }

    // Batch-save named configs � each file is named after config.Name
    public void SaveAll(IEnumerable<AppConfig> configs)
    {
        jsonService.SaveJsonFiles(configs, ConfigFolder, Extension);
    }
}

AppConfig must implement Oakrey.Collections.IName (exposes a Name property) to be usable with SaveJsonFiles.

Development notes

  • LoadJson, LoadJsonAsync, and SaveJson are declared virtual on JsonService, making the class suitable as a base for domain-specific JSON services that need to apply custom JsonSerializerOptions or pre/post-processing.
  • SaveJsonFiles calls SaveJsonAsync internally, which is async void. Exceptions thrown inside async void propagate to the synchronization context and cannot be awaited. Prefer SaveJson or a custom override when reliable error propagation is required.
  • LoadObjectsFromJsonFilesAsync enumerates files sequentially rather than in parallel to keep memory pressure predictable for large directories.
  • Serialization and deserialization operate directly on FileStream instances obtained from IFileService, avoiding intermediate string or byte-array buffers.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author: Oakrey
Repository: https://dev.azure.com/oakrey/OpenPackages/_git/ApplicationServices
Project URL: http://www.oakrey.cz/opkg_applications

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

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Oakrey.Applications.Json:

Package Downloads
Oakrey.Applications.Settings

WPF-only (.NET 10 Windows) library providing ISettingsService with persistent and volatile typed key-value settings, SettingsBase for bindable ViewModelBase-derived settings classes, CallerMemberName key inference, built-in window bounds restore via RestoreBounds, and a one-call ConfigureSettingService DI extension.

Oakrey.Applications.Base

A foundational .NET library for building modular WPF applications. Provides application lifecycle management, MVVM ViewModel resolution, structured logging, telemetry, and sequential or parallel service preloading with full unhandled-exception coverage.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.0.0 102 5/22/2026
3.0.4 102 5/22/2026
3.0.3 118 5/15/2026
3.0.2 130 3/13/2026
3.0.1 152 2/11/2026
3.0.0 438 11/18/2025
2.1.2 200 10/10/2025
2.1.1 249 9/29/2025
2.1.0 222 9/24/2025
2.0.0 349 6/9/2025
1.0.0 301 4/17/2025