SimpleHelpers.ObjectDiffPatch#
Simple Object Comparer that generates a Diff between objects and is able to Patch one object to transforms into the other.
ObjectDiffPatch uses Newtonsoft.Json internally to create a JObject that we use to run a simple and reliable deep/recursive object comparison.
Features#
- Deep/recursive comparison
- Reliable
- Diff
- Patch
Installation#
NuGet Package Details#
You can install using NuGet, see SimpleHelpers.ObjectDiffPatch at NuGet.org
PM> Install-Package SimpleHelpers.ObjectDiffPatch
The nuget package contains C# source code.
The source code will be installed in your project with the following file system structure:
|-- <project root>
|-- SimpleHelpers
|-- ObjectDiffPatch.cs
Download#
If you prefer, you can also download the source code: ObjectDiffPatch.cs
API#
GenerateDiff#
Compares two objects and generates the differences between them returning an object listing all changes.
Detected changes are expressed as two Newtonsoft.Json.Linq.JObject, old and new values.
var diff = ObjectDiffPatch.GenerateDiff (originalObj, updatedObj); // original properties values Console.WriteLine (diff.OldValues.ToString()); // updated properties values Console.WriteLine (diff.NewValues.ToString());
Snapshot#
Creates an object snapshots as a Newtonsoft.Json.Linq.JObject.
var snapshot = ObjectDiffPatch.Snapshot (obj); // do something.... do_something (obj); // log changes var diff = ObjectDiffPatch.GenerateDiff (snapshot, obj); if (!diff.AreEqual) { Console.WriteLine (diff.NewValues.ToString()); }
PatchObject#
Modifies an object according to a diff.
var diff = ObjectDiffPatch.GenerateDiff (originalObj, updatedObj); // recreate originalObj from updatedObj var patched = ObjectDiffPatch.PatchObject (updatedObj, diff.OldValues);