Skip to content

Commit 3b9feef

Browse files
committed
Enhance README with documentation on smart list diffing using object_hash for efficient patches on collections with unique identifiers.
1 parent e08a60a commit 3b9feef

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Features:
2020
- test
2121
- Escaping of "`/`" (by "`~1`") and "`~`" (by "`~0`")
2222
- Allow usage of `-` for appending things to list (Add and Copy operation)
23+
- Smart list diffing with `object_hash` for efficient patches on collections with unique identifiers
2324

2425
## Getting started
2526

@@ -52,6 +53,36 @@ iex> Jsonpatch.diff(source, destination)
5253
]
5354
```
5455

56+
### Smart List Diffing with `object_hash`
57+
58+
Use `object_hash` to generate efficient patches for lists of objects with unique identifiers, producing minimal operations instead of cascading replacements.
59+
60+
```elixir
61+
iex> original = [
62+
%{id: 1, name: "Alice"},
63+
%{id: 2, name: "Bob"}
64+
]
65+
iex> updated = [
66+
%{id: 99, name: "New"},
67+
%{id: 1, name: "Alice"},
68+
%{id: 2, name: "Bob"}
69+
]
70+
71+
# Traditional pairwise diff - multiple replace operations
72+
# >> Jsonpatch.diff(original, updated)
73+
[
74+
%{op: "add", path: "/2", value: %{id: 2, name: "Bob"}}
75+
%{op: "replace", path: "/0", value: %{id: 99, name: "New"}},
76+
%{op: "replace", path: "/1", value: %{id: 1, name: "Alice"}},
77+
]
78+
79+
# With object_hash - single add operation
80+
iex> Jsonpatch.diff(original, updated, object_hash: fn %{id: id} -> id end)
81+
[
82+
%{op: "add", path: "/0", value: %{id: 99, name: "New"}}
83+
]
84+
```
85+
5586
### Apply patches
5687

5788
```elixir
@@ -69,4 +100,4 @@ iex> Jsonpatch.apply_patch(patch, target)
69100

70101
## Important sources
71102
- [Official RFC 6902](https://tools.ietf.org/html/rfc6902)
72-
- [Inspiration: python-json-patch](https://github.com/stefankoegl/python-json-patch)
103+
- [Inspiration: python-json-patch](https://github.com/stefankoegl/python-json-patch)

0 commit comments

Comments
 (0)