JsonMigrations 3.0.0
dotnet add package JsonMigrations --version 3.0.0
NuGet\Install-Package JsonMigrations -Version 3.0.0
<PackageReference Include="JsonMigrations" Version="3.0.0" />
<PackageVersion Include="JsonMigrations" Version="3.0.0" />
<PackageReference Include="JsonMigrations" />
paket add JsonMigrations --version 3.0.0
#r "nuget: JsonMigrations, 3.0.0"
#:package JsonMigrations@3.0.0
#addin nuget:?package=JsonMigrations&version=3.0.0
#tool nuget:?package=JsonMigrations&version=3.0.0
JsonMigrations
A .NET library for incrementally migrating JSON files (such as appsettings.json) without needing to overwrite them entirely. This is useful when JSON files contain installation-specific values that cannot be known in advance.
Migrations are plain C# classes that run in a defined order. After each run the library records which migration was last applied, so only newer migrations execute on subsequent runs.
Getting Started
Installation
Install via NuGet:
dotnet add package JsonMigrations
Requirements: .NET 6.0 or later
Quick Example
using System.Text.Encodings.Web;
using System.Text.Json;
using JsonMigrations;
using JsonMigrations.Enums;
// 1. Configure options (optional — defaults shown below)
var options = new JsonMigratorOptions()
{
InvalidJsonAction = InvalidJsonAction.Throw,
FileNotExistAction = FileAction.Throw,
MigrationFaultAction = MigrationAction.Throw,
JsonSerializerOptions = new JsonSerializerOptions()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
WriteIndented = true,
}
};
JsonMigrator.SetOptions(options);
// 2. Register migrations by assembly or namespace
JsonMigrator.AddJsonMigrations(typeof(MyFirstMigration).Assembly);
// or: JsonMigrator.AddJsonMigrations(typeof(MyFirstMigration).Namespace!);
// 3. Run migrations on one or more files
JsonMigrator.Migrate("AppsettingsProgram", "appsettings.json", "appsettings.development.json");
A complete working example is available in the Example project.
Writing Migrations
Create a class that implements IJsonMigration:
using System.Text.Json;
using System.Text.Json.Nodes;
using JsonMigrations.Interfaces;
public class MyFirstMigration : IJsonMigration
{
public int Order => 0;
public string MigrationKey => "AppsettingsProgram";
public void Up(JsonObject jsonObject, JsonSerializerOptions options)
{
jsonObject.TryAdd("ConnectionString", "Server=localhost;Database=mydb");
}
}
| Property | Description |
|---|---|
Order |
Determines execution sequence. Lowest runs first. Each new migration must have a higher order than previous ones. |
MigrationKey |
Groups migrations together. Only migrations matching the key passed to Migrate() will run. |
Up |
The migration logic. Modify the JsonObject to add, update, or remove properties. The JsonSerializerOptions from configuration are passed through. |
After running, a "Migration" property is written to each JSON file with the class name of the last applied migration. On subsequent runs, only migrations with a higher Order are executed.
Configuration
JsonMigratorOptions
| Option | Type | Default | Description |
|---|---|---|---|
MigrationFaultAction |
MigrationAction |
Throw |
What happens when a migration throws an exception. |
InvalidJsonAction |
InvalidJsonAction |
Throw |
What happens when a file contains invalid JSON. |
FileNotExistAction |
FileAction |
Throw |
What happens when a target file does not exist. |
JsonSerializerOptions |
JsonSerializerOptions |
Indented, relaxed encoding | Serializer options used for reading/writing JSON and available inside migrations. |
Enum Values
MigrationAction — behaviour on migration failure:
| Value | Description |
|---|---|
| Ignore | Skips the failed migration and continues with the next one. |
| Throw | Throws the exception. |
| Restore | Restores the JSON file to its state before the migration run started. |
InvalidJsonAction — behaviour on invalid JSON:
| Value | Description |
|---|---|
| None | Skips the file. |
| Throw | Throws an InvalidJsonException. |
FileAction — behaviour when a file does not exist:
| Value | Description |
|---|---|
| None | Skips the file. |
| Create | Creates the file with an empty JSON object ({}). |
| Throw | Throws an exception. |
API Reference
// Configure options (call before Migrate)
JsonMigrator.SetOptions(JsonMigratorOptions options);
// Register migrations from one or more assemblies
JsonMigrator.AddJsonMigrations(params Assembly[] assemblies);
// Register migrations from one or more namespaces
JsonMigrator.AddJsonMigrations(params string[] namespaces);
// Run pending migrations for a key against one or more files
JsonMigrator.Migrate(string migrationKey, params string[] filePaths);
Contributing
See CONTRIBUTING.md for guidelines on how to contribute, including commit conventions and build instructions.
License
See LICENSE.txt for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.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.