Morcatko.AspNetCore.JsonMergePatch.Document 6.0.0

.NET 6.0
dotnet add package Morcatko.AspNetCore.JsonMergePatch.Document --version 6.0.0
NuGet\Install-Package Morcatko.AspNetCore.JsonMergePatch.Document -Version 6.0.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="Morcatko.AspNetCore.JsonMergePatch.Document" Version="6.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Morcatko.AspNetCore.JsonMergePatch.Document --version 6.0.0
#r "nuget: Morcatko.AspNetCore.JsonMergePatch.Document, 6.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Morcatko.AspNetCore.JsonMergePatch.Document as a Cake Addin
#addin nuget:?package=Morcatko.AspNetCore.JsonMergePatch.Document&version=6.0.0

// Install Morcatko.AspNetCore.JsonMergePatch.Document as a Cake Tool
#tool nuget:?package=Morcatko.AspNetCore.JsonMergePatch.Document&version=6.0.0

JSON Merge Patch support for ASP.NET Core

Nuget - Morcatko.AspNetCore.JsonMergePatch (ASP.NET Core 2.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson (ASP.NET Core 3+)
Nuget - Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3+)
Nuget - Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3+ - base package)

JSON Merge Patch

  • RFC 7396
  • performs partial resource update similar to JSON Patch
  • Supports Swagger
  • netstandard 2.0
C# object:
var backendModel = new Model()
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Gun"
}

JSON Merge Patch:
{
    "Weapon": "Knife"
}

resulting C# object:
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Knife"
}

How to

See 2.1-testApp, 3.0 testApp or 6.0/testApp for sample

  1. Install nuget (Use JsonMergePathch version same as your .NET) .
  1. Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()                         // or .AddMvcCore()
        //.AddJsonMergePatch();           // 2.x
        //.AddNewtonsoftJsonMergePatch(); // 3+ (Newtonsoft.Josn)
        //.AddSystemTextJsonMergePatch(); // 3+ (System.Text)
    ...
}
  1. Use in your controller
using Morcatko.AspNetCore.JsonMergePatch;

[HttpPatch]
[Consumes(JsonMergePatchDocument.ContentType)]
public void Patch([FromBody] JsonMergePatchDocument<Model> patch)
{
    ...
    patch.ApplyTo(backendModel);
    ...
}

You can apply a patch to a different Type (be carefull, all C# static typing is ignored) - see #16 for more details.

BackendModel backendModel;
JsonMergePatch<DtoModel> patch;
patch.ApplyToT(backendModel)
  1. Swagger config (optional)

copy & paste this class into your app

services.AddSwaggerGen(c =>
    {
        c.OperationFilter<JsonMergePatchDocumentOperationFilter>();
    });

Options

    services
        .AddMvc()
        .AddJsonMergePatch(o => ....)
  • bool EnableDelete - Deletes items when target object is Dictionary and patched value is null

How to - unit testing

See tests in ...Builder.Json.Simple class for more examples

Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json

public void UnitTest()
{
    var model = new Model();
    var patch1 = PatchBuidler.Build<Model>("{ integer: 1}");
    ...
    or
    ...
    var original = new Model();
    var patched = new Model() { Integer = 1};
    var patch2 = PatchBuilder.Build(original, patched);
}

Known issues/Not working

  • ModelState.IsValid is false when a required property is missing
  • Enums with [EnumMember(Value = "....")] attribute
Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Morcatko.AspNetCore.JsonMergePatch.Document:

Package Downloads
Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson

JSON Merge Patch support for ASP.NET Core 6.X (Newtonsoft.Json)

Morcatko.AspNetCore.JsonMergePatch.SystemText

JSON Merge Patch support for ASP.NET Core 6.X (System.Text.Json)

joerage.AspNetCore.JsonMergePatch.NewtonsoftJson

JSON Merge Patch support for ASP.NET Core 3.X (Newtonsoft.Json)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.0 81,737 4/29/2022
6.0.0-alpha01 19,762 1/21/2022
3.0.7 213,071 3/24/2021
3.0.6 40,980 11/16/2020
3.0.5 10,978 9/30/2020
3.0.4 18,767 8/26/2020
3.0.3 1,653 8/7/2020
3.0.2 9,907 6/3/2020
3.0.1 154,366 3/24/2020
3.0.0 12,350 2/18/2020
3.0.0-Release0.0 974 11/27/2019
3.0.0-preview9.0 452 9/6/2019