JsonFileWrapper 1.1.0
dotnet add package JsonFileWrapper --version 1.1.0
NuGet\Install-Package JsonFileWrapper -Version 1.1.0
<PackageReference Include="JsonFileWrapper" Version="1.1.0" />
<PackageVersion Include="JsonFileWrapper" Version="1.1.0" />
<PackageReference Include="JsonFileWrapper" />
paket add JsonFileWrapper --version 1.1.0
#r "nuget: JsonFileWrapper, 1.1.0"
#:package JsonFileWrapper@1.1.0
#addin nuget:?package=JsonFileWrapper&version=1.1.0
#tool nuget:?package=JsonFileWrapper&version=1.1.0
JsonFileWrapper
A simple, educational generic wrapper for JSON file operations in .NET. Simplifies loading and saving strongly-typed objects to JSON files with automatic backup support.
Overview
JsonFileWrapper was created as an educational tool to demonstrate generic classes and the Bridge design pattern. It provides a clean API for persisting objects to JSON files without writing repetitive serialization code.
Features
- ✅ Generic Type Support - Works with any serializable .NET type
- ✅ Automatic Backup - Creates
.bakfiles before overwriting - ✅ Zero External Dependencies - Uses built-in
System.Text.Json - ✅ Simple API - Load, modify, and save in just a few lines
- ✅ Null-Safe - Handles missing files and null data gracefully
- ✅ IDisposable Support - Auto-saves on disposal
Requirements
- .NET 8.0 or higher
Installation
Package Manager Console
Install-Package JsonFileWrapper
.NET CLI
dotnet add package JsonFileWrapper
Package Reference
<PackageReference Include="JsonFileWrapper" Version="1.1.0" />
Quick Start
Define Your Model
public class Person
{
public string Name { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public int Age { get; set; }
public string Movie { get; set; } = string.Empty; // Film de medverkat i
public int Year { get; set; } // Årtal för filmen
}
Load, Modify, and Save
using MarcusMedinaPro.JsonFileWrapper;
// Create or load existing file
var file = new JsonFile<List<Person>>("actors")
{
Data = new List<Person>()
};
// Add data
file.Data.Add(new Person { Name = "Allison", LastName = "Williams", Age = 35, Movie = "M3GAN", Year = 2022 });
file.Data.Add(new Person { Name = "Violet", LastName = "McGraw", Age = 13, Movie = "M3GAN", Year = 2022 });
file.Data.Add(new Person { Name = "Vera", LastName = "Farmiga", Age = 50, Movie = "The Conjuring", Year = 2013 });
file.Data.Add(new Person { Name = "Patrick", LastName = "Wilson", Age = 50, Movie = "The Conjuring", Year = 2013 });
// Save to actors.json
file.Save();
Output (actors.json)
[
{
"Name": "Allison",
"LastName": "Williams",
"Age": 35,
"Movie": "M3GAN",
"Year": 2022
},
{
"Name": "Violet",
"LastName": "McGraw",
"Age": 13,
"Movie": "M3GAN",
"Year": 2022
},
{
"Name": "Vera",
"LastName": "Farmiga",
"Age": 50,
"Movie": "The Conjuring",
"Year": 2013
},
{
"Name": "Patrick",
"LastName": "Wilson",
"Age": 50,
"Movie": "The Conjuring",
"Year": 2013
}
]
Advanced Usage
Custom JSON Formatting
var file = new JsonFile<List<Person>>("data")
{
Format = new JsonSerializerOptions
{
WriteIndented = false, // Compact JSON
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}
};
Implicit Conversion
var file = new JsonFile<List<Person>>("actors");
List<Person> people = file; // Implicit conversion
Auto-Save with IDisposable
using (var file = new JsonFile<List<Person>>("actors"))
{
file.Data.Add(new Person { Name = "New", LastName = "Actor" });
// Automatically saves on dispose
}
API Reference
Constructor
public JsonFile(string filename)
Creates a new instance and attempts to load existing data from {filename}.json.
Properties
| Property | Type | Description |
|---|---|---|
Data |
T? |
The data object being managed |
Filename |
string |
Filename without extension |
Suffix |
string |
File extension (default: "json") |
Format |
JsonSerializerOptions? |
JSON serialization options |
Methods
| Method | Returns | Description |
|---|---|---|
Load() |
T? |
Loads data from file |
Save() |
void |
Saves data to file with backup |
Dispose() |
void |
Saves and disposes resources |
How It Works
- Load: Reads JSON file if it exists, deserializes to type
T - Modify: Work with the strongly-typed
Dataproperty - Save:
- Creates backup (
.json.bak) of existing file - Writes new data to
.jsonfile
- Creates backup (
Error Handling
JsonFileWrapper handles common errors gracefully:
- Missing files: Returns new instance of
T - Corrupt JSON: Logs error and returns new instance
- File access errors: Logs error using
Debug.WriteLine
Educational Purpose
This library demonstrates several C# concepts:
- Generic Classes:
JsonFile<T>works with any type - Design Patterns: Loosely based on the Bridge pattern
- Serialization: Using modern
System.Text.Json - Resource Management: Implementing
IDisposable
Migration from v1.0.x
Version 1.1.0 introduces breaking changes:
- Newtonsoft.Json → System.Text.Json: Update your
Formatsettings - .NET 6 → .NET 8: Update project target framework
- MSTest → xUnit: If using tests, update test framework
Updating JsonSerializerSettings
Before (v1.0.x with Newtonsoft.Json):
Format = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
};
After (v1.1.0 with System.Text.Json):
Format = new JsonSerializerOptions
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
Contributing
Contributions are welcome! Please:
- Open an issue to discuss major changes
- Fork the repository
- Create a feature branch
- Add/update tests as appropriate
- Submit a pull request
Source Code
Full source available on GitHub
License
Licensed under MIT License
Credits
- Author: Marcus Medina
- Icon: Json file Icon by First Styles on Iconscout
Made with ❤️ for educational purposes
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
See CHANGELOG.md for details