Morcatko.AspNetCore.JsonMergePatch 2.0.5

.NET Standard 2.0
dotnet add package Morcatko.AspNetCore.JsonMergePatch --version 2.0.5
NuGet\Install-Package Morcatko.AspNetCore.JsonMergePatch -Version 2.0.5
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" Version="2.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Morcatko.AspNetCore.JsonMergePatch --version 2.0.5
#r "nuget: Morcatko.AspNetCore.JsonMergePatch, 2.0.5"
#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 as a Cake Addin
#addin nuget:?package=Morcatko.AspNetCore.JsonMergePatch&version=2.0.5

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

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.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3.x - 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 or 3.0 testApp for sample

  1. Install nuget.
  1. Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()                         // or .AddMvcCore()
        //.AddJsonMergePatch();           // 2.x
        //.AddNewtonsoftJsonMergePatch(); // 3.x (Newtonsoft.Josn)
        //.AddSystemTextJsonMergePatch(); // 3.x (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 net5.0 net5.0-windows 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
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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
2.0.5 6,200 3/24/2021
2.0.4 1,158 8/26/2020
2.0.3 575 8/7/2020
2.0.2 13,123 11/7/2019
2.0.1 1,520 11/3/2019
2.0.0 76,289 6/20/2019
2.0.0-beta10 990 5/27/2019
2.0.0-beta09 4,021 4/11/2019
2.0.0-beta08 505 4/2/2019
2.0.0-beta07 15,365 12/13/2018
2.0.0-beta06 2,308 12/7/2018
2.0.0-beta05 2,315 11/20/2018
2.0.0-beta04 21,130 9/7/2018
2.0.0-beta03 5,265 11/6/2017
2.0.0-beta02 719 11/3/2017
2.0.0-beta01 627 10/24/2017
2.0.0-alpha02 1,577 9/14/2017
1.0.0-alpha01 935 6/23/2017