Oakrey.Applications.Json
3.0.4
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
<PackageReference Include="Oakrey.Applications.Json" Version="3.0.4" />
<PackageVersion Include="Oakrey.Applications.Json" Version="3.0.4" />
<PackageReference Include="Oakrey.Applications.Json" />
paket add Oakrey.Applications.Json --version 3.0.4
#r "nuget: Oakrey.Applications.Json, 3.0.4"
#:package Oakrey.Applications.Json@3.0.4
#addin nuget:?package=Oakrey.Applications.Json&version=3.0.4
#tool nuget:?package=Oakrey.Applications.Json&version=3.0.4
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
CancellationTokensupport - 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
INameobjects to individual files named after each object - Stream-based I/O � no intermediate string allocation during serialization or deserialization
- All key methods are
virtualonJsonService, 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 providesIFileService)Oakrey.Collections2.0.0 or higher (providesINameused bySaveJsonFiles)
Installation
NuGet Package Manager
- In Visual Studio open Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for
Oakrey.Applications.Jsonand 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, andSaveJsonare declaredvirtualonJsonService, making the class suitable as a base for domain-specific JSON services that need to apply customJsonSerializerOptionsor pre/post-processing.SaveJsonFilescallsSaveJsonAsyncinternally, which isasync void. Exceptions thrown insideasync voidpropagate to the synchronization context and cannot be awaited. PreferSaveJsonor a custom override when reliable error propagation is required.LoadObjectsFromJsonFilesAsyncenumerates files sequentially rather than in parallel to keep memory pressure predictable for large directories.- Serialization and deserialization operate directly on
FileStreaminstances obtained fromIFileService, 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 | Versions 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. |
-
net10.0
- Oakrey.Applications.Files.Abstractions (>= 3.0.3)
- Oakrey.Applications.Json.Abstractions (>= 3.0.4)
- Oakrey.Log (>= 2.0.1)
- Oakrey.Telemetry (>= 2.0.1)
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.