JsonFileWrapper 1.1.0

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

JsonFileWrapper

NuGet .NET License

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 .bak files 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

  1. Load: Reads JSON file if it exists, deserializes to type T
  2. Modify: Work with the strongly-typed Data property
  3. Save:
    • Creates backup (.json.bak) of existing file
    • Writes new data to .json file

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 Format settings
  • .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:

  1. Open an issue to discuss major changes
  2. Fork the repository
  3. Create a feature branch
  4. Add/update tests as appropriate
  5. Submit a pull request

Source Code

Full source available on GitHub

License

Licensed under MIT License

Credits


Made with ❤️ for educational purposes

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

Version Downloads Last Updated
1.1.0 176 10/8/2025
1.0.4 1,126 1/21/2022
1.0.3 990 1/21/2022
1.0.2 979 1/14/2022
1.0.1 986 1/14/2022
1.0.0.1 1,184 1/13/2022 1.0.0.1 is deprecated because it has critical bugs.

See CHANGELOG.md for details