Palit.AspNetCore.JsonPatch.Extensions.Generate 1.0.0

.NET Standard 2.0
Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate -Version 1.0.0
dotnet add package Palit.AspNetCore.JsonPatch.Extensions.Generate --version 1.0.0
<PackageReference Include="Palit.AspNetCore.JsonPatch.Extensions.Generate" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Palit.AspNetCore.JsonPatch.Extensions.Generate --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Palit.AspNetCore.JsonPatch.Extensions.Generate, 1.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 Palit.AspNetCore.JsonPatch.Extensions.Generate as a Cake Addin
#addin nuget:?package=Palit.AspNetCore.JsonPatch.Extensions.Generate&version=1.0.0

// Install Palit.AspNetCore.JsonPatch.Extensions.Generate as a Cake Tool
#tool nuget:?package=Palit.AspNetCore.JsonPatch.Extensions.Generate&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

JsonPatch Generate Extension

Builds on Microsoft.AspNetCore.JsonPatch to generate JSON patch documents by comparing or observing changes to C# objects.

  • Generate JSON patch by comparing two objects
  • Watch an instance of an object for changes and generate a JSON patch representing those changes


From the package manager console:

PM> Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate

or by simply searching for Palit.AspNetCore.JsonPatch.Extensions.Generate in the package manager UI.

How it's used

Use this library if you want to generate JSON patch documents from C#. I couldn't find a library for this already and it is not built in so I wrote one myself. It can generate the patch document by comparing two objects or by watching changes to an instance.


This library is currently fairly simple. It will not necessarily generate optimal JSON patch documents. Instead, it generates patch documents containing Add, Remove and Replace commands exclusively. In the future it would be nice to work in all the different operations in a more optimal way.

Internally, this library relies on serializer pretty heavily. You can pass in custom serializer settings, but definitely do thorough testing with your unique circumstances. The current tests are pretty simple and do not cover complex situations (like deep nesting).

How-To generate a JSON patch by comparison

var original = new TestClass()
    Id = "id",
    Message = "message",
    DecimalValue = 1.43m,
    GuidValue = Guid.Empty,
    IntList = new List<int>() { 1, 2, 3 }
var modified = new TestClass()
    Id = "new-id",
    Message = null,
    DecimalValue = 1.68m,
    GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"),
    IntList = new List<int>() { 1, 3, 2 }

var generator = new JsonPatchDocumentGenerator();
var patch = generator.Generate(original, modified);

// Modify original with patch.

Assert.Equal(5, patch.Operations.Count);
Assert.Equal(original, modified, new GenericDeepEqualityComparer<TestClass>());

Alternatively, you can pass in your own custom JsonSerializer like so:

var jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
var patch = generator.Generate(original, modified, jsonSerializer);

How to generate a patch document with an observer

var instance = new TestClass
    Id = "id",
    Message = "message",
    GuidValue = Guid.Empty,
    DecimalValue = 1.23m,
    IntList = new List<int> { 1, 2, 3 }

var observer = new JsonPatchDocumentDiffObserver<TestClass>(instance);
instance.Id = "new-id";
instance.Message = "new-message";
instance.GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5");
instance.DecimalValue = 1.89m;
instance.IntList = new List<int> { 3, 2, 1 };

// Point in time snapshot of the changes.
var patch = observer.Generate();

Assert.Equal(5, patch.Operations.Count);



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
.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
1.0.0 40,403 6/4/2018