Palit.AspNetCore.JsonPatch.Extensions.Generate
1.0.0
dotnet add package Palit.AspNetCore.JsonPatch.Extensions.Generate --version 1.0.0
NuGet\Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate -Version 1.0.0
<PackageReference Include="Palit.AspNetCore.JsonPatch.Extensions.Generate" Version="1.0.0" />
paket add Palit.AspNetCore.JsonPatch.Extensions.Generate --version 1.0.0
#r "nuget: Palit.AspNetCore.JsonPatch.Extensions.Generate, 1.0.0"
// 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
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
Installation
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.
Gotchas
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 JSON.net 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.
patch.ApplyTo(original);
Assert.NotNull(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.NotNull(patch);
Assert.Equal(5, patch.Operations.Count);
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.JsonPatch (>= 2.1.0)
- Newtonsoft.Json (>= 11.0.2)
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 | 347,689 | 6/4/2018 |