Skip to content

Commit cb8405e

Browse files
author
Benjamín Eidelman
committed
Update deltas.md
1 parent 4bb02b5 commit cb8405e

1 file changed

Lines changed: 108 additions & 1 deletion

File tree

docs/deltas.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
explain the delta format
1+
Delta Format
2+
============
3+
4+
This page intends to be a reference for JSON format used to represent deltas (ie. the output of ```jsondiffpatch.diff```).
5+
6+
This format was created with a balance between readability and low footprint in mind.
7+
8+
- when diffing 2 objects, the delta will reflect the same object structure (common part on both sides)
9+
- to represent changed parts, arrays and magic numbers are used to keep a low footprint (ie. you won't see verbosity like ```"type": "added")
10+
- keep it pure JSON serializable
11+
12+
A great way to understand this format is using the "Annotated JSON" option in the [Live Demo](http://benjamine.github.com/JsonDiffPatch/demo/index.html), and try the different left/right examples, or edit left/right JSON to see the annotated delta update as your type.
13+
14+
Here's a complete reference of this format.
15+
16+
Added
17+
-----
18+
a value is added, ie. it was ```undefined``` and now has a value.
19+
``` javascript
20+
delta = [newValue ]
21+
```
22+
23+
Modified
24+
-----
25+
a value was replaced by another value
26+
``` javascript
27+
delta = [ oldValue, newValue ]
28+
```
29+
30+
Deleted
31+
-----
32+
value deleted, ie. it had a value and is now ```undefined```
33+
``` javascript
34+
delta = [ oldValue, 0, 0]
35+
```
36+
37+
Object with inner changes
38+
-----
39+
value is an object, and there are nested changes inside its properties
40+
41+
``` javascript
42+
delta = {
43+
property1: innerDelta1,
44+
property2: innerDelta2,
45+
property5: innerDelta5
46+
}
47+
```
48+
49+
> Note: only properties with inner deltas are included
50+
51+
52+
Array with inner changes
53+
-----
54+
value is an array, and there are nested changes inside its items
55+
56+
``` javascript
57+
delta = {
58+
_t: 'a',
59+
index1: innerDelta1,
60+
index2: innerDelta2,
61+
index5: innerDelta5
62+
}
63+
```
64+
65+
> Note: only indices with inner deltas are included
66+
> Note: _t: 'a', indicates this applies to an array, when patching if a regular object (or a value type) is found, an error will be thrown
67+
68+
### Index Notation
69+
70+
Indices on array deltas can be expressed in two ways:
71+
- number: refers to the index in the final (right) state of the array, this is used to indicate items inserted.
72+
- underscore + number: refers to the index in the original (left) state of the array, this is used to indicate items removed, or moved.
73+
74+
### Array Moves
75+
an item was moved to a different position in the same array
76+
``` javascript
77+
delta = [ '', destinationIndex, 3]
78+
```
79+
> Note: '', represents the moved item value, suppresed by default
80+
> Note: 3, is the magical number that indicates "array move"
81+
82+
Text Diffs
83+
----------
84+
85+
If two strigns are compared and they are different, you will see as you expect:
86+
``` javascript
87+
delta = [ "some text", "some text modified" ]
88+
```
89+
But if both strings are long enough, [a text diffing algorythm](https://code.google.com/p/google-diff-match-patch/) will be used to efficiently detect changes in parts of the text.
90+
91+
You can modify the minimum length with:
92+
```
93+
var customDiffPatch = jsondiffpatch.create({
94+
textDiff: {
95+
minLength: 60 // default value
96+
}
97+
});
98+
```
99+
100+
And the delta will look like this:
101+
102+
``` javascript
103+
delta = [ unidiff, 0, 2 ]
104+
105+
```
106+
> Note: 2, is the magical number that indicates "text diff"
107+
> Note: unidiff is actually a character-based variation of Unidiff format that is explained [here](https://code.google.com/p/google-diff-match-patch/wiki/Unidiff)
108+

0 commit comments

Comments
 (0)