Skip to content

Commit 3aedde1

Browse files
authored
Paper work for 1.2.0 release (#13)
* Improve docs for 1.2.0 * Fix diff docs * Add code owner file
1 parent b25fa6a commit 3aedde1

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @weichch

README.md

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ High-performance, low-allocating JSON objects diff and patch extension for Syste
99
- Target latest .NET Standard and .NET Framework 4.6.1 (for legacy apps) and leverage latest .NET features
1010
- Alternative to [jsondiffpatch.net](https://github.com/wbish/jsondiffpatch.net) which is based on Newtonsoft.Json
1111
- Fast large JSON document diffing with less memory consumption (see [benchmark](https://github.com/weichch/system-text-json-jsondiffpatch/blob/main/Benchmark.md))
12-
- Support smart array diffing (e.g. move detect) using LCS and custom array item matcher
12+
- Support smart array diffing (e.g. move detect) using LCS (Longest Common Subsequence) and custom array item matcher
1313
- _(Only when not using RFC 6902 format)_ Support diffing long text using [google-diff-match-patch](http://code.google.com/p/google-diff-match-patch/), or write your own diff algorithm
1414
- Bonus `JsonNode.DeepClone` and `JsonNode.DeepEquals` methods
15-
- Bouns `JsonValueComparer` that implements semantic comparison of two `JsonValue` objects (including `JsonValue` backed by `JsonElement`)
15+
- Bouns [`JsonValueComparer`](https://github.com/weichch/system-text-json-jsondiffpatch/blob/main/src/SystemTextJson.JsonDiffPatch/JsonValueComparer.cs) that implements semantic comparison of two `JsonValue` objects (including `JsonValue` backed by `JsonElement`)
1616
- JSON assert for xUnit, MSTest v2 and NUnit with customizable delta output
1717

1818
## Install
@@ -25,6 +25,17 @@ High-performance, low-allocating JSON objects diff and patch extension for Syste
2525
### Diff
2626

2727
```csharp
28+
// Diff JsonNode
29+
var node1 = JsonNode.Parse("{\"foo\":\"bar\"}");
30+
var node2 = JsonNode.Parse("{\"baz\":\"qux\", \"foo\":\"bar\"}");
31+
JsonNode? diff = node1.Diff(node2);
32+
// Diff with options
33+
JsonNode? diff = node1.Diff(node2, new JsonDiffOptions
34+
{
35+
JsonElementComparison = JsonElementComparison.Semantic
36+
});
37+
// Diff and convert delta into RFC 6902 JSON Patch format
38+
JsonNode? diff = node1.Diff(node2, new JsonPatchDeltaFormatter());
2839
// Diff JSON files
2940
JsonNode? diff = JsonDiffPatcher.DiffFile(file1, file2);
3041
// Diff Span<byte>
@@ -35,52 +46,44 @@ JsonNode? diff = JsonDiffPatcher.Diff(stream1, stream2);
3546
JsonNode? diff = JsonDiffPatcher.Diff(json1, json2);
3647
// Diff JSON readers
3748
JsonNode? diff = JsonDiffPatcher.Diff(ref reader1, ref reader2);
38-
// Diff JsonNode objects
39-
var node1 = JsonNode.Parse(...);
40-
var node2 = JsonNode.Parse(...);
41-
JsonNode? diff = node1.Diff(node2);
42-
// Diff with options
43-
JsonNode? diff = node1.Diff(node2, new JsonDiffOptions { ... });
44-
// Diff and convert delta into RFC 6902 JSON Patch format
45-
JsonNode? diff = node1.Diff(node2, new JsonPatchDeltaFormatter());
4649
```
4750

4851
### DeepClone
4952

5053
```csharp
51-
var node = JsonNode.Parse(...);
54+
var node = JsonNode.Parse("{\"foo\":\"bar\"}");
5255
JsonNode? cloned = node.DeepClone();
5356
```
5457

5558
### DeepEquals
5659

5760
```csharp
58-
var node1 = JsonNode.Parse(...);
59-
var node2 = JsonNode.Parse(...);
61+
var node1 = JsonNode.Parse("{\"foo\":1.0}");
62+
var node2 = JsonNode.Parse("{\"foo\":1}");
63+
// equal is false
6064
bool equal = node1.DeepEquals(node2);
65+
// semanticEqual is true
6166
bool semanticEqual = node1.DeepEquals(node2, JsonElementComparison.Semantic);
6267
```
6368

6469
### Semantic Value Comparison
6570
```csharp
66-
var node1 = JsonNode.Parse("[\"2019-11-27\"]").First().AsValue();
67-
var node2 = JsonNode.Parse("[\"2019-11-27T00:00:00.000\"]").First().AsValue();
68-
71+
var node1 = JsonNode.Parse("\"2019-11-27\"");
72+
var node2 = JsonNode.Parse("\"2019-11-27T00:00:00.000\"");
6973
// dateCompare is 0
7074
var dateCompare = JsonValueComparer.Compare(node1, node2);
7175

72-
var node3 = JsonNode.Parse("[1]").First().AsValue();
73-
var node4 = JsonNode.Parse("[1.00]").First().AsValue();
74-
76+
var node3 = JsonNode.Parse("1");
77+
var node4 = JsonNode.Parse("1.00");
7578
// numCompare is 0
7679
var numCompare = JsonValueComparer.Compare(node3, node4);
7780
```
7881

7982
### Patch & Unpatch
8083

8184
```csharp
82-
var node1 = JsonNode.Parse(...);
83-
var node2 = JsonNode.Parse(...);
85+
var node1 = JsonNode.Parse("{\"foo\":\"bar\"}");
86+
var node2 = JsonNode.Parse("{\"baz\":\"qux\", \"foo\":\"bar\"}");
8487
JsonNode? diff = node1.Diff(node2);
8588
// In-place patch
8689
JsonDiffPatcher.Patch(ref node1, diff);
@@ -92,11 +95,20 @@ JsonDiffPatcher.ReversePatch(ref node1, diff);
9295
node1.ReversePatchNew(diff);
9396
```
9497

98+
### Default Options
99+
100+
```csharp
101+
JsonDiffPatcher.DefaultOptions = () => new JsonDiffOptions
102+
{
103+
JsonElementComparison = JsonElementComparison.Semantic
104+
};
105+
```
106+
95107
### Assert (Unit Testing)
96108

97109
```csharp
98-
var expected = JsonNode.Parse(...);
99-
var actual = JsonNode.Parse(...);
110+
var expected = JsonNode.Parse("{\"foo\":\"bar\"}");
111+
var actual = JsonNode.Parse("{\"baz\":\"qux\", \"foo\":\"bar3\"}");
100112

101113
// xUnit
102114
JsonAssert.Equal(expected, actual);
@@ -122,18 +134,18 @@ Example output _(when output is enabled)_:
122134
JsonAssert.Equal() failure.
123135
Expected:
124136
{
125-
"foo": "baz"
137+
"foo": "bar"
126138
}
127139
Actual:
128140
{
129-
"foo": "bar",
130-
"baz": "qux"
141+
"baz": "qux",
142+
"foo": "bar"
131143
}
132144
Delta:
133145
{
134146
"foo": [
135-
"baz",
136-
"bar"
147+
"bar",
148+
"bar3"
137149
],
138150
"baz": [
139151
"qux"

src/SystemTextJson.JsonDiffPatch/JsonValueComparisonContext.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,6 @@ public bool DeepEquals(ref JsonValueComparisonContext another, JsonElementCompar
405405
}
406406
}
407407

408-
public static bool IsValueKind(JsonValueKind jsonValueKind)
409-
{
410-
return jsonValueKind is JsonValueKind.Number or JsonValueKind.String or JsonValueKind.True
411-
or JsonValueKind.False or JsonValueKind.Null;
412-
}
413-
414408
private static JsonStringValueKind GetStringValueKind(Type? valueType)
415409
{
416410
if (valueType == typeof(DateTime) || valueType == typeof(DateTimeOffset))

0 commit comments

Comments
 (0)