jsondiffpatch.net/README.md

178 lines
4.2 KiB
Markdown
Raw Permalink Normal View History

2016-01-25 21:43:14 -05:00
# jsondiffpatch.net
2016-02-05 14:46:56 -05:00
<!--- badges -->
2018-03-03 21:50:02 -05:00
[![Build status](https://ci.appveyor.com/api/projects/status/aavhn0lwas0j29gy?svg=true)](https://ci.appveyor.com/project/wbish/jsondiffpatch-net)
2016-02-09 14:22:36 -05:00
[![NuGet](https://img.shields.io/nuget/v/JsonDiffPatch.Net.svg)](https://www.nuget.org/packages/JsonDiffPatch.Net/)
2016-02-05 14:46:56 -05:00
2016-02-06 21:08:07 -05:00
JSON object diffs and reversible patching ([jsondiffpatch](https://github.com/benjamine/jsondiffpatch) compatible)
2016-02-04 02:00:59 -05:00
2020-04-01 13:21:35 -04:00
## Installing
Install from [jsondiffpatch.net](https://www.nuget.org/packages/JsonDiffPatch.Net/) nuget website, or run the following command:
``` PowerShell
Install-Package JsonDiffPatch.Net
````
2016-02-04 02:00:59 -05:00
## Usage
2016-02-15 23:40:06 -05:00
The library has support for the following 3 operations: Diff, Patch and Unpatch.
2016-02-04 02:00:59 -05:00
### Diff
Diff two json objects
``` C#
2016-02-04 17:25:57 -05:00
var jdp = new JsonDiffPatch();
2016-02-04 02:00:59 -05:00
var left = JToken.Parse(@"{ ""key"": false }");
var right = JToken.Parse(@"{ ""key"": true }");
2020-04-01 13:21:35 -04:00
2016-02-04 17:25:57 -05:00
JToken patch = jdp.Diff(left, right);
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
Console.WriteLine(patch.ToString());
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
// Output:
// {
// "key": [false, true]
// }
```
### Patch
Patch a left object with a patch document
``` C#
2016-02-04 17:25:57 -05:00
var jdp = new JsonDiffPatch();
2016-02-04 02:00:59 -05:00
var left = JToken.Parse(@"{ ""key"": false }");
var right = JToken.Parse(@"{ ""key"": true }");
2016-02-04 17:25:57 -05:00
JToken patch = jdp.Diff(left, right);
2020-04-01 13:21:35 -04:00
2016-02-04 17:25:57 -05:00
var output = jdp.Patch(left, patch);
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
Console.WriteLine(output.ToString());
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
// Output:
// {
// "key": true
// }
```
### Unpatch
2016-02-09 14:22:36 -05:00
Unpatch a right object with a patch document
2016-02-04 02:00:59 -05:00
``` C#
2016-02-04 17:25:57 -05:00
var jdp = new JsonDiffPatch();
2016-02-04 02:00:59 -05:00
var left = JToken.Parse(@"{ ""key"": false }");
var right = JToken.Parse(@"{ ""key"": true }");
2016-02-04 17:25:57 -05:00
JToken patch = jdp.Diff(left, right);
2020-04-01 13:21:35 -04:00
2016-02-04 17:25:57 -05:00
var output = jdp.Unpatch(right, patch);
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
Console.WriteLine(output.ToString());
2020-04-01 13:21:35 -04:00
2016-02-04 02:00:59 -05:00
// Output:
// {
// "key": false
// }
```
2016-02-15 23:40:06 -05:00
## Advanced Usage
JsonDiffPatch.Net is designed to handle complex diffs by producing a compact diff object with enough information to patch and unpatch relevant JSON objects. The following are some of the most common cases you may hit when generating a diff:
- Adding, Removing a property from an object
- Changing the property value or even value type
- Inserting and shifting elements in an array
- Efficient string diffing using google-diff-match-patch
- Nested object diffs
2020-04-01 13:21:35 -04:00
The full JSON patch document format is documented at https://github.com/benjamine/jsondiffpatch.
2016-02-15 23:40:06 -05:00
``` JavaScript
2020-04-01 13:21:35 -04:00
var left =
2016-02-15 23:40:06 -05:00
{
"id": 100,
"revision": 5,
"items": [
"car",
"bus"
],
"tagline": "I can't do it. This text is too long for me to handle! Please help me JsonDiffPatch!",
"author": "wbish"
}
var right =
{
"id": 100,
"revision": 6,
"items": [
"bike",
"bus",
"car"
],
"tagline": "I can do it. This text is not too long. Thanks JsonDiffPatch!",
"author": {
"first": "w",
"last": "bish"
}
}
var jdp = new JsonDiffPatch();
var output = jdp.Diff(left, right);
// Output:
{
"revision": [ // Changed the value of a property
5, // Old value
6 // New value
],
"items": { // Inserted and moved items in an array
"0": [
"bike"
],
"_t": "a",
"_1": [
"",
1,
3
]
},
"tagline": [ // A long string diff using google-diff-match-patch
"@@ -2,10 +2,8 @@\n can\n-'t\n do \n@@ -23,49 +23,28 @@\n is \n+not \n too long\n- for me to handle! Please help me\n+. Thanks\n Jso\n",
0,
2
],
"author": [ // Changed the type of the author property from string to object
"wbish",
{
"first": "w",
"last": "bish"
}
]
}
```
2020-04-01 13:21:35 -04:00
## JSON Patches (RFC 6902)
2016-02-06 21:08:07 -05:00
2020-04-01 13:21:35 -04:00
A diff result can be converted into JSON patches, according to the [RFC 6902 spec](https://tools.ietf.org/html/rfc6902).
2016-02-06 21:08:07 -05:00
2020-04-01 13:21:35 -04:00
```csharp
var left = JObject.Parse("{ \"name\": \"Justin\" }");
var right = JObject.Parse("{ \"name\" : \"John\", \"age\": 34 }");
var patch = new JsonDiffPatch().Diff(left, right);
var formatter = new JsonDeltaFormatter();
var operations = formatter.Format(patch);
/*
operations: [
{ "op": "replace", "path": "/name", "value": "John" },
{ "op": "add", "path": "/age", "value": 34 }
]
*/
```
2016-02-04 02:00:59 -05:00
## Attributions
* [jsondiffpatch](https://github.com/benjamine/jsondiffpatch)
* [google-diff-match-patch](https://github.com/google/diff-match-patch)
2016-02-04 02:00:59 -05:00
* [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/)