fastjsonpatch 0.1.0__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. fastjsonpatch-0.1.3/BENCHMARKS.md +57 -0
  2. fastjsonpatch-0.1.3/CHANGELOG.md +24 -0
  3. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/Cargo.lock +1 -1
  4. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/Cargo.toml +1 -1
  5. fastjsonpatch-0.1.3/PKG-INFO +112 -0
  6. fastjsonpatch-0.1.3/README.md +84 -0
  7. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/pyproject.toml +1 -1
  8. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/src/lib.rs +3 -0
  9. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/test_parity.py +19 -0
  10. fastjsonpatch-0.1.0/CHANGELOG.md +0 -11
  11. fastjsonpatch-0.1.0/PKG-INFO +0 -200
  12. fastjsonpatch-0.1.0/README.md +0 -172
  13. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/.github/workflows/CI.yml +0 -0
  14. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/.gitignore +0 -0
  15. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/LICENSE +0 -0
  16. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/MAINTAINING.md +0 -0
  17. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/THIRD_PARTY_LICENSES.md +0 -0
  18. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/bench/compare.py +0 -0
  19. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/python/fastjsonpatch/__init__.py +0 -0
  20. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/scripts/test_linux_matrix.sh +0 -0
  21. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/scripts/third_party_licenses.py +0 -0
  22. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/rfc6902/LICENSE-APACHE-2.0 +0 -0
  23. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/rfc6902/README.md +0 -0
  24. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/rfc6902/spec_tests.json +0 -0
  25. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/rfc6902/test_rfc6902.py +0 -0
  26. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/rfc6902/tests.json +0 -0
  27. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/upstream/test_upstream.py +0 -0
  28. {fastjsonpatch-0.1.0 → fastjsonpatch-0.1.3}/tests/upstream/tests.js +0 -0
@@ -0,0 +1,57 @@
1
+ # fastjsonpatch benchmarks
2
+
3
+ ## Results
4
+
5
+ Measured on Linux x86_64, Python 3.13 (times are the best of several runs).
6
+
7
+ Real-world data:
8
+
9
+ | scenario | size | jsonpatch | fastjsonpatch | speedup |
10
+ |---|---|---|---|---|
11
+ | Kubernetes OpenAPI v1.33 (3.7 MB) → v1.34 (3.8 MB): make_patch | 283 ops | 136.4 ms | 9.6 ms | **14x** |
12
+ | Kubernetes OpenAPI v1.33 → v1.34: apply | 283 ops | 78.0 ms | 11.4 ms | **7x** |
13
+ | 3-op patch on the v1.34 spec | 3.8 MB | 71.1 ms | 10.1 ms | **7x** |
14
+ | 3-op patch on the v1.34 spec, `in_place=True` | 3.8 MB | 21.2 µs | 4.7 µs | **4x** |
15
+ | PyPI metadata snapshots (8k records): make_patch | 2,487 ops | 255.2 ms | 12.1 ms | **21x** |
16
+ | PyPI metadata snapshots: apply | 2,487 ops | 91.7 ms | 14.0 ms | **7x** |
17
+ | REST resource, apply_patch per request | ×1000 | 47.4 ms | 7.5 ms | **6x** |
18
+ | REST resource, make_patch per request | ×1000 | 74.4 ms | 5.6 ms | **13x** |
19
+
20
+ Synthetic records shaped like `{"id", "name", "score", "tags": [..], "meta": {..}}`:
21
+
22
+ | workload | ops | make_patch: jsonpatch | fastjsonpatch | speedup | apply: jsonpatch | fastjsonpatch | speedup |
23
+ |---|---|---|---|---|---|---|---|
24
+ | 10k records, 100 field edits | 100 | 292.61 ms | 5.26 ms | **56x** | 89.38 ms | 7.90 ms | **11x** |
25
+ | 10k records, insert 1 at front | 49911 | 669.77 ms | 42.78 ms | **16x** | 535.21 ms | 72.35 ms | **7x** |
26
+ | 10k records, delete 50 | 44900 | 636.53 ms | 40.46 ms | **16x** | 563.20 ms | 63.45 ms | **9x** |
27
+ | 10k records, move 20 | 26305 | 434.93 ms | 24.38 ms | **18x** | 325.64 ms | 41.03 ms | **8x** |
28
+ | 2k ints, insert 1 at front | 1 | 17.04 ms | 1.91 ms | **9x** | 581 µs | 21 µs | **27x** |
29
+ | small config (200 keys), 1 edit | 1 | 1.98 ms | 35 µs | **57x** | 464 µs | 32 µs | **15x** |
30
+
31
+ Python 3.14 made `copy.deepcopy` about 1.5x faster, so on 3.14 jsonpatch's `apply` on
32
+ a large document is faster too; on the "10k records, 100 field edits" workload the
33
+ `apply` speedup drops from about 11x to about 6x. The `make_patch` speedup stays about
34
+ the same. Run `python bench/compare.py` (from the source distribution) to measure on
35
+ your own machine.
36
+
37
+ ## Where the time goes
38
+
39
+ **make_patch.** jsonpatch walks both documents in Python and compares every scalar
40
+ with `json.dumps(a) == json.dumps(b)`. Profiling the "10k records" workload with
41
+ cProfile puts about 40% of the time in `json.dumps` and about a third in `isinstance`
42
+ checks against abstract base classes (`MutableMapping`, `MutableSequence`).
43
+ fastjsonpatch walks the documents in Rust over the CPython objects directly (no
44
+ conversion to a Rust data structure), compares values natively with the same rules as the `json.dumps` comparison (`1`, `1.0` and `True`
45
+ are all different, `-0.0 != 0.0`, NaN equals NaN), keeps the current path on a stack,
46
+ and takes a single lockstep pass when two dicts have the same keys in the same order.
47
+ It is a port of jsonpatch's `DiffBuilder`, so it produces the same operations in the
48
+ same order, including detected moves.
49
+
50
+ **apply.** jsonpatch first runs `copy.deepcopy` on the document (so the original is
51
+ not modified), then builds a `JsonPointer` and an operation object for each operation.
52
+ For a small patch on a large document the copy is nearly all of the time.
53
+ fastjsonpatch copies each dict and list with one C call and only recurses into values
54
+ that are themselves containers; it keeps `deepcopy`'s semantics (shared and cyclic
55
+ references are copied once) but skips the bookkeeping for containers referenced only
56
+ once. It then resolves the paths and runs the operations without creating
57
+ `JsonPointer` or operation objects.
@@ -0,0 +1,24 @@
1
+ # Changelog
2
+
3
+ ## 0.1.3 (2026-10-12)
4
+
5
+ - Fix: `replace` with a path ending in `-` that names a dict key (`{"-": 1}`,
6
+ path `/-`) was applied; jsonpatch 1.34 raises `InvalidJsonPatch` for it, and
7
+ fastjsonpatch now does too.
8
+ - Shorter README; full benchmark tables moved to BENCHMARKS.md.
9
+
10
+ ## 0.1.2 (2026-10-11)
11
+
12
+ Documentation only: the README now states only measured facts (including exactly
13
+ which platforms CI tests), and its link to MAINTAINING.md works on PyPI. No code
14
+ changes. (0.1.1 was tagged but not published.)
15
+
16
+ ## 0.1.0 (2026-10-11)
17
+
18
+ First release. Behaves like jsonpatch 1.34 with `make_patch`, `apply`/`apply_patch`
19
+ and patch validation in Rust:
20
+
21
+ - `make_patch` 9-57x faster, `apply` 4-27x faster (see BENCHMARKS.md).
22
+ - Standalone: depends only on `jsonpointer`.
23
+ - Wheels for Linux (glibc and musl, x86_64 and aarch64), macOS (arm64, x86_64) and
24
+ Windows (x64); one abi3 wheel per platform covers Python 3.12+.
@@ -23,7 +23,7 @@ checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
23
23
 
24
24
  [[package]]
25
25
  name = "fastjsonpatch"
26
- version = "0.1.0"
26
+ version = "0.1.3"
27
27
  dependencies = [
28
28
  "ahash",
29
29
  "pyo3",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "fastjsonpatch"
3
- version = "0.1.0"
3
+ version = "0.1.3"
4
4
  edition = "2024"
5
5
  description = "Rust core of the fastjsonpatch Python package"
6
6
  license = "BSD-3-Clause"
@@ -0,0 +1,112 @@
1
+ Metadata-Version: 2.4
2
+ Name: fastjsonpatch
3
+ Version: 0.1.3
4
+ Classifier: Development Status :: 4 - Beta
5
+ Classifier: Intended Audience :: Developers
6
+ Classifier: Operating System :: OS Independent
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3 :: Only
9
+ Classifier: Programming Language :: Rust
10
+ Classifier: Programming Language :: Python :: Implementation :: CPython
11
+ Classifier: Topic :: Software Development :: Libraries
12
+ Requires-Dist: jsonpointer>=3.0,<4
13
+ Requires-Dist: pytest ; extra == 'test'
14
+ Requires-Dist: jsonpatch==1.34 ; extra == 'test'
15
+ Provides-Extra: test
16
+ License-File: LICENSE
17
+ License-File: THIRD_PARTY_LICENSES.md
18
+ Summary: Drop-in faster jsonpatch (RFC 6902): make_patch and apply in Rust
19
+ Keywords: json,patch,jsonpatch,rfc6902,diff,rust
20
+ Author-email: bitsbybyun@gmail.com
21
+ License-Expression: BSD-3-Clause
22
+ Requires-Python: >=3.12
23
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
24
+ Project-URL: Changelog, https://github.com/amzntiger/fastjsonpatch/blob/main/CHANGELOG.md
25
+ Project-URL: Issues, https://github.com/amzntiger/fastjsonpatch/issues
26
+ Project-URL: Repository, https://github.com/amzntiger/fastjsonpatch
27
+
28
+ # fastjsonpatch
29
+
30
+ A drop-in replacement for [jsonpatch](https://github.com/stefankoegl/python-json-patch)
31
+ ([RFC 6902](https://www.rfc-editor.org/rfc/rfc6902)) with `make_patch` and `apply`
32
+ written in Rust. It returns the same patches and documents as jsonpatch 1.34 and raises
33
+ exceptions with the same types and messages.
34
+
35
+ ```sh
36
+ pip install fastjsonpatch
37
+ ```
38
+
39
+ ```python
40
+ import fastjsonpatch as jsonpatch # the only line you change
41
+
42
+ src = {"name": "web", "replicas": 3, "ports": [80, 443]}
43
+ dst = {"name": "web", "replicas": 5, "ports": [80, 443, 8080]}
44
+
45
+ patch = jsonpatch.make_patch(src, dst)
46
+ patch.patch
47
+ # [{'op': 'replace', 'path': '/replicas', 'value': 5},
48
+ # {'op': 'add', 'path': '/ports/2', 'value': 8080}]
49
+
50
+ patch.apply(src) == dst # True; src is not modified
51
+ jsonpatch.apply_patch(src, [{"op": "remove", "path": "/ports/0"}])
52
+ # {'name': 'web', 'replicas': 3, 'ports': [443]}
53
+ ```
54
+
55
+ Requires CPython 3.12+. Wheels are published for Linux (x86_64, aarch64; glibc and
56
+ musl), macOS (arm64, x86_64) and Windows (x64). The only dependency is `jsonpointer`.
57
+
58
+ ## Performance
59
+
60
+ Linux x86_64, Python 3.13, best of several runs:
61
+
62
+ | workload | jsonpatch | fastjsonpatch | speedup |
63
+ |---|---|---|---|
64
+ | `make_patch`, Kubernetes OpenAPI v1.33 → v1.34 (3.7 → 3.8 MB, 283 ops) | 136.4 ms | 9.6 ms | **14x** |
65
+ | `apply`, the same 283 ops | 78.0 ms | 11.4 ms | **7x** |
66
+ | `make_patch`, 10k records with 100 field edits | 292.61 ms | 5.26 ms | **56x** |
67
+ | `apply`, the same 100 ops | 89.38 ms | 7.90 ms | **11x** |
68
+ | `make_patch`, small REST resource ×1000 | 74.4 ms | 5.6 ms | **13x** |
69
+ | `apply_patch`, small REST resource ×1000 | 47.4 ms | 7.5 ms | **6x** |
70
+
71
+ Across all measured workloads, `make_patch` is 9-57x faster and `apply` 4-27x (4x is a
72
+ 3-operation patch applied `in_place` to a 3.8 MB document, where no copy is made). On
73
+ Python 3.14, whose `copy.deepcopy` is faster, the `apply` speedup is smaller (about 6x
74
+ instead of 11x on the 10k-record workload). Full tables and an explanation of where
75
+ the time goes are in
76
+ [BENCHMARKS.md](https://github.com/amzntiger/fastjsonpatch/blob/main/BENCHMARKS.md);
77
+ `python bench/compare.py` (in the source distribution) measures on your machine.
78
+
79
+ ## Compatibility
80
+
81
+ fastjsonpatch is jsonpatch 1.34's module with `JsonPatch.__init__` (validation),
82
+ `JsonPatch.from_diff` (`make_patch`) and `JsonPatch.apply` (`apply_patch`) routed to
83
+ Rust; every other public name is jsonpatch's own code. Inputs the Rust code does not
84
+ handle exactly fall back to jsonpatch's original code before anything is modified:
85
+ non-JSON values, dict or list subclasses, nesting deeper than 1000, a custom `dumps=`
86
+ or `json_dumper`, a custom `pointer_cls`, `operations` or `_get_operation` (for
87
+ `apply`), and operations that would fail.
88
+
89
+ Known differences:
90
+
91
+ - The exception classes are fastjsonpatch's own, so
92
+ `except fastjsonpatch.JsonPatchConflict` does not catch an exception raised by the `jsonpatch` package, and vice versa.
93
+ - Values in a document that are not dicts, lists or JSON scalars are copied with
94
+ `copy.deepcopy` one by one, so aliasing between such values is not preserved.
95
+ - `__version__` is fastjsonpatch's version; `__jsonpatch_version__` is `"1.34"`.
96
+ jsonpatch's `__author__` and `__website__` are not defined.
97
+
98
+ ## Testing
99
+
100
+ About 1,580 tests: randomized comparisons against jsonpatch 1.34 (patches, documents
101
+ and errors), jsonpatch's own test suite and doctests, and the
102
+ [json-patch-tests](https://github.com/json-patch/json-patch-tests) RFC 6902 suite.
103
+ CI runs them with Python 3.12-3.14 on Linux (x86_64, aarch64; glibc), macOS (arm64,
104
+ x86_64) and Windows (x64); the musl wheels are built but not tested in CI.
105
+
106
+ ## License
107
+
108
+ BSD-3-Clause. The Python module and the diff algorithm come from jsonpatch
109
+ (Copyright (c) 2011 Stefan Kögl, BSD). Licenses of the Rust crates compiled into the
110
+ wheels are in `THIRD_PARTY_LICENSES.md`. Development and release notes:
111
+ [MAINTAINING.md](https://github.com/amzntiger/fastjsonpatch/blob/main/MAINTAINING.md).
112
+
@@ -0,0 +1,84 @@
1
+ # fastjsonpatch
2
+
3
+ A drop-in replacement for [jsonpatch](https://github.com/stefankoegl/python-json-patch)
4
+ ([RFC 6902](https://www.rfc-editor.org/rfc/rfc6902)) with `make_patch` and `apply`
5
+ written in Rust. It returns the same patches and documents as jsonpatch 1.34 and raises
6
+ exceptions with the same types and messages.
7
+
8
+ ```sh
9
+ pip install fastjsonpatch
10
+ ```
11
+
12
+ ```python
13
+ import fastjsonpatch as jsonpatch # the only line you change
14
+
15
+ src = {"name": "web", "replicas": 3, "ports": [80, 443]}
16
+ dst = {"name": "web", "replicas": 5, "ports": [80, 443, 8080]}
17
+
18
+ patch = jsonpatch.make_patch(src, dst)
19
+ patch.patch
20
+ # [{'op': 'replace', 'path': '/replicas', 'value': 5},
21
+ # {'op': 'add', 'path': '/ports/2', 'value': 8080}]
22
+
23
+ patch.apply(src) == dst # True; src is not modified
24
+ jsonpatch.apply_patch(src, [{"op": "remove", "path": "/ports/0"}])
25
+ # {'name': 'web', 'replicas': 3, 'ports': [443]}
26
+ ```
27
+
28
+ Requires CPython 3.12+. Wheels are published for Linux (x86_64, aarch64; glibc and
29
+ musl), macOS (arm64, x86_64) and Windows (x64). The only dependency is `jsonpointer`.
30
+
31
+ ## Performance
32
+
33
+ Linux x86_64, Python 3.13, best of several runs:
34
+
35
+ | workload | jsonpatch | fastjsonpatch | speedup |
36
+ |---|---|---|---|
37
+ | `make_patch`, Kubernetes OpenAPI v1.33 → v1.34 (3.7 → 3.8 MB, 283 ops) | 136.4 ms | 9.6 ms | **14x** |
38
+ | `apply`, the same 283 ops | 78.0 ms | 11.4 ms | **7x** |
39
+ | `make_patch`, 10k records with 100 field edits | 292.61 ms | 5.26 ms | **56x** |
40
+ | `apply`, the same 100 ops | 89.38 ms | 7.90 ms | **11x** |
41
+ | `make_patch`, small REST resource ×1000 | 74.4 ms | 5.6 ms | **13x** |
42
+ | `apply_patch`, small REST resource ×1000 | 47.4 ms | 7.5 ms | **6x** |
43
+
44
+ Across all measured workloads, `make_patch` is 9-57x faster and `apply` 4-27x (4x is a
45
+ 3-operation patch applied `in_place` to a 3.8 MB document, where no copy is made). On
46
+ Python 3.14, whose `copy.deepcopy` is faster, the `apply` speedup is smaller (about 6x
47
+ instead of 11x on the 10k-record workload). Full tables and an explanation of where
48
+ the time goes are in
49
+ [BENCHMARKS.md](https://github.com/amzntiger/fastjsonpatch/blob/main/BENCHMARKS.md);
50
+ `python bench/compare.py` (in the source distribution) measures on your machine.
51
+
52
+ ## Compatibility
53
+
54
+ fastjsonpatch is jsonpatch 1.34's module with `JsonPatch.__init__` (validation),
55
+ `JsonPatch.from_diff` (`make_patch`) and `JsonPatch.apply` (`apply_patch`) routed to
56
+ Rust; every other public name is jsonpatch's own code. Inputs the Rust code does not
57
+ handle exactly fall back to jsonpatch's original code before anything is modified:
58
+ non-JSON values, dict or list subclasses, nesting deeper than 1000, a custom `dumps=`
59
+ or `json_dumper`, a custom `pointer_cls`, `operations` or `_get_operation` (for
60
+ `apply`), and operations that would fail.
61
+
62
+ Known differences:
63
+
64
+ - The exception classes are fastjsonpatch's own, so
65
+ `except fastjsonpatch.JsonPatchConflict` does not catch an exception raised by the `jsonpatch` package, and vice versa.
66
+ - Values in a document that are not dicts, lists or JSON scalars are copied with
67
+ `copy.deepcopy` one by one, so aliasing between such values is not preserved.
68
+ - `__version__` is fastjsonpatch's version; `__jsonpatch_version__` is `"1.34"`.
69
+ jsonpatch's `__author__` and `__website__` are not defined.
70
+
71
+ ## Testing
72
+
73
+ About 1,580 tests: randomized comparisons against jsonpatch 1.34 (patches, documents
74
+ and errors), jsonpatch's own test suite and doctests, and the
75
+ [json-patch-tests](https://github.com/json-patch/json-patch-tests) RFC 6902 suite.
76
+ CI runs them with Python 3.12-3.14 on Linux (x86_64, aarch64; glibc), macOS (arm64,
77
+ x86_64) and Windows (x64); the musl wheels are built but not tested in CI.
78
+
79
+ ## License
80
+
81
+ BSD-3-Clause. The Python module and the diff algorithm come from jsonpatch
82
+ (Copyright (c) 2011 Stefan Kögl, BSD). Licenses of the Rust crates compiled into the
83
+ wheels are in `THIRD_PARTY_LICENSES.md`. Development and release notes:
84
+ [MAINTAINING.md](https://github.com/amzntiger/fastjsonpatch/blob/main/MAINTAINING.md).
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "fastjsonpatch"
7
- version = "0.1.0"
7
+ version = "0.1.3"
8
8
  description = "Drop-in faster jsonpatch (RFC 6902): make_patch and apply in Rust"
9
9
  readme = "README.md"
10
10
  license = "BSD-3-Clause"
@@ -835,6 +835,9 @@ impl<'py> Applier<'_, 'py> {
835
835
  if parts.is_empty() {
836
836
  return Ok(Some(deepcopy(&v)?));
837
837
  }
838
+ if parts.last().is_some_and(|p| p == "-") {
839
+ return Ok(None); // jsonpatch rejects "-" here even for a dict key
840
+ }
838
841
  let Some(s) = slot(obj, parts)? else { return Ok(None) };
839
842
  if slot_value(&s)?.is_none() {
840
843
  return Ok(None);
@@ -163,6 +163,25 @@ def test_invalid_patches(patch):
163
163
  assert outcome(lambda: fastjsonpatch.JsonPatch(patch)) == outcome(lambda: jsonpatch.JsonPatch(patch))
164
164
 
165
165
 
166
+ @pytest.mark.parametrize("doc, patch", [
167
+ # jsonpatch rejects a final "-" in replace even when it names a dict key
168
+ ({"-": 1}, [{"op": "replace", "path": "/-", "value": 2}]),
169
+ ({"a": {"-": 1}}, [{"op": "replace", "path": "/a/-", "value": 2}]),
170
+ ([1], [{"op": "replace", "path": "/-", "value": 2}]),
171
+ ({"-": 1}, [{"op": "remove", "path": "/-"}]),
172
+ ({"-": 1}, [{"op": "add", "path": "/-", "value": 2}]),
173
+ ({"-": 1}, [{"op": "test", "path": "/-", "value": 1}]),
174
+ ({"-": 1}, [{"op": "move", "from": "/-", "path": "/b"}]),
175
+ ({"-": 1}, [{"op": "copy", "from": "/-", "path": "/b"}]),
176
+ ])
177
+ def test_dash_key(doc, patch):
178
+ for in_place in (False, True):
179
+ a, b = copy.deepcopy(doc), copy.deepcopy(doc)
180
+ assert (outcome(lambda: fastjsonpatch.apply_patch(a, patch, in_place=in_place))
181
+ == outcome(lambda: jsonpatch.apply_patch(b, patch, in_place=in_place)))
182
+ assert repr(a) == repr(b)
183
+
184
+
166
185
  def test_exceptions_have_the_same_hierarchy():
167
186
  with pytest.raises(fastjsonpatch.JsonPatchConflict):
168
187
  fastjsonpatch.apply_patch({}, [{"op": "remove", "path": "/a"}])
@@ -1,11 +0,0 @@
1
- # Changelog
2
-
3
- ## 0.1.0 (2026-10-11)
4
-
5
- First release. Behaves like jsonpatch 1.34 with `make_patch`, `apply`/`apply_patch`
6
- and patch validation in Rust:
7
-
8
- - `make_patch` 9-57x faster, `apply` 6-27x faster (see README).
9
- - Standalone: depends only on `jsonpointer`.
10
- - Wheels for Linux (glibc and musl, x86_64 and aarch64), macOS (arm64, x86_64) and
11
- Windows (x64); one abi3 wheel per platform covers Python 3.12+.
@@ -1,200 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: fastjsonpatch
3
- Version: 0.1.0
4
- Classifier: Development Status :: 4 - Beta
5
- Classifier: Intended Audience :: Developers
6
- Classifier: Operating System :: OS Independent
7
- Classifier: Programming Language :: Python :: 3
8
- Classifier: Programming Language :: Python :: 3 :: Only
9
- Classifier: Programming Language :: Rust
10
- Classifier: Programming Language :: Python :: Implementation :: CPython
11
- Classifier: Topic :: Software Development :: Libraries
12
- Requires-Dist: jsonpointer>=3.0,<4
13
- Requires-Dist: pytest ; extra == 'test'
14
- Requires-Dist: jsonpatch==1.34 ; extra == 'test'
15
- Provides-Extra: test
16
- License-File: LICENSE
17
- License-File: THIRD_PARTY_LICENSES.md
18
- Summary: Drop-in faster jsonpatch (RFC 6902): make_patch and apply in Rust
19
- Keywords: json,patch,jsonpatch,rfc6902,diff,rust
20
- Author-email: bitsbybyun@gmail.com
21
- License-Expression: BSD-3-Clause
22
- Requires-Python: >=3.12
23
- Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
24
- Project-URL: Changelog, https://github.com/amzntiger/fastjsonpatch/blob/main/CHANGELOG.md
25
- Project-URL: Issues, https://github.com/amzntiger/fastjsonpatch/issues
26
- Project-URL: Repository, https://github.com/amzntiger/fastjsonpatch
27
-
28
- # fastjsonpatch
29
-
30
- A drop-in, faster [jsonpatch](https://github.com/stefankoegl/python-json-patch)
31
- ([RFC 6902](https://www.rfc-editor.org/rfc/rfc6902) JSON Patch) for Python.
32
- `make_patch` and `apply` run in Rust and return the **same patches, the same documents
33
- and the same exceptions** as jsonpatch 1.34: typically 10-50x faster to make a patch
34
- and 6-15x faster to apply one.
35
-
36
- ```sh
37
- pip install fastjsonpatch
38
- ```
39
-
40
- ```python
41
- import fastjsonpatch as jsonpatch # the only line you change
42
-
43
- src = {"name": "web", "replicas": 3, "ports": [80, 443]}
44
- dst = {"name": "web", "replicas": 5, "ports": [80, 443, 8080]}
45
-
46
- patch = jsonpatch.make_patch(src, dst)
47
- patch.patch
48
- # [{'op': 'replace', 'path': '/replicas', 'value': 5},
49
- # {'op': 'add', 'path': '/ports/2', 'value': 8080}]
50
-
51
- patch.apply(src) == dst # True; src is not modified
52
- jsonpatch.apply_patch(src, [{"op": "remove", "path": "/ports/0"}])
53
- # {'name': 'web', 'replicas': 3, 'ports': [443]}
54
- ```
55
-
56
- Python 3.12+ (CPython). Prebuilt wheels for Linux (x86_64 and aarch64, glibc and
57
- musl), macOS (Apple Silicon and Intel) and Windows (x64); one wheel per platform covers
58
- every Python from 3.12 on. The only dependency is `jsonpointer`.
59
-
60
- ## Why
61
-
62
- JSON Patch describes a change to a JSON document as a list of operations (`add`,
63
- `remove`, `replace`, `move`, `copy`, `test`). It is how HTTP `PATCH` requests,
64
- Kubernetes and many APIs express partial updates, and how tools store or send the
65
- difference between two versions of a document.
66
-
67
- jsonpatch is the standard Python implementation, and it is written in pure Python.
68
- That is fine for small documents, but the cost grows quickly:
69
-
70
- - `make_patch` on a 10,000-record document takes about 300 ms (on a fast machine),
71
- and diffing two Kubernetes OpenAPI specs (3.7 MB) takes about 140 ms.
72
- - `apply` deep-copies the whole document first, so even a one-operation patch on a
73
- large document costs tens of milliseconds.
74
- - A web service that applies or computes a patch per request spends 40-75 µs on
75
- each call, even for small resources.
76
-
77
- fastjsonpatch keeps jsonpatch's behaviour exactly and moves these hot paths to Rust,
78
- so existing code gets faster without changes.
79
-
80
- ## Speed
81
-
82
- Measured on Linux x86_64, Python 3.13 (times are the best of several runs).
83
-
84
- Real-world data:
85
-
86
- | scenario | size | jsonpatch | fastjsonpatch | speedup |
87
- |---|---|---|---|---|
88
- | Kubernetes OpenAPI v1.33 → v1.34: make_patch | 3.7 MB, 283 ops | 136.4 ms | 9.6 ms | **14x** |
89
- | Kubernetes OpenAPI v1.33 → v1.34: apply | 283 ops | 78.0 ms | 11.4 ms | **7x** |
90
- | 3-op patch on the 3.8 MB spec | | 71.1 ms | 10.1 ms | **7x** |
91
- | 3-op patch on the 3.8 MB spec, `in_place=True` | | 21.2 µs | 4.7 µs | **4x** |
92
- | PyPI metadata snapshots (8k records): make_patch | 2,487 ops | 255.2 ms | 12.1 ms | **21x** |
93
- | PyPI metadata snapshots: apply | 2,487 ops | 91.7 ms | 14.0 ms | **7x** |
94
- | REST resource, apply_patch per request | ×1000 | 47.4 ms | 7.5 ms | **6x** |
95
- | REST resource, make_patch per request | ×1000 | 74.4 ms | 5.6 ms | **13x** |
96
-
97
- Synthetic records shaped like `{"id", "name", "score", "tags": [..], "meta": {..}}`:
98
-
99
- | workload | ops | make_patch: jsonpatch | fastjsonpatch | speedup | apply: jsonpatch | fastjsonpatch | speedup |
100
- |---|---|---|---|---|---|---|---|
101
- | 10k records, 100 field edits | 100 | 292.61 ms | 5.26 ms | **56x** | 89.38 ms | 7.90 ms | **11x** |
102
- | 10k records, insert 1 at front | 49911 | 669.77 ms | 42.78 ms | **16x** | 535.21 ms | 72.35 ms | **7x** |
103
- | 10k records, delete 50 | 44900 | 636.53 ms | 40.46 ms | **16x** | 563.20 ms | 63.45 ms | **9x** |
104
- | 10k records, move 20 | 26305 | 434.93 ms | 24.38 ms | **18x** | 325.64 ms | 41.03 ms | **8x** |
105
- | 2k ints, insert 1 at front | 1 | 17.04 ms | 1.91 ms | **9x** | 581 µs | 21 µs | **27x** |
106
- | small config (200 keys), 1 edit | 1 | 1.98 ms | 35 µs | **57x** | 464 µs | 32 µs | **15x** |
107
-
108
- Python 3.14 made `copy.deepcopy` about 1.6x faster, so on 3.14 jsonpatch's `apply` on
109
- a large document is faster too and the gap shrinks to about 6x. `make_patch` is not
110
- affected. Run `python bench/compare.py` (from the source distribution) to measure on
111
- your own machine.
112
-
113
- ## Why it is faster
114
-
115
- **make_patch.** jsonpatch walks both documents in Python and compares every scalar
116
- with `json.dumps(a) == json.dumps(b)`. That serialisation is about half of the
117
- runtime; the rest is `isinstance` checks against abstract base classes and path
118
- tuples rebuilt at every level. fastjsonpatch walks the documents in Rust over the
119
- CPython objects directly (no conversion to a Rust data structure), compares values
120
- natively with the same rules as the `json.dumps` comparison (`1`, `1.0` and `True`
121
- are all different, `-0.0 != 0.0`, NaN equals NaN), keeps the current path on a stack,
122
- and takes a single lockstep pass when two dicts have the same keys in the same order.
123
- It is a port of jsonpatch's `DiffBuilder`, so it produces the same operations in the
124
- same order, including detected moves.
125
-
126
- **apply.** jsonpatch first runs `copy.deepcopy` on the document (so the original is
127
- not modified), then builds a `JsonPointer` and an operation object for each operation.
128
- For a small patch on a large document the copy is nearly all of the time.
129
- fastjsonpatch copies each dict and list with one C call and only recurses into values
130
- that are themselves containers; it keeps `deepcopy`'s semantics (shared and cyclic
131
- references are copied once) but skips the bookkeeping for containers referenced only
132
- once. It then resolves the paths and runs the operations without creating
133
- intermediate objects.
134
-
135
- ## Compatibility
136
-
137
- fastjsonpatch is jsonpatch's own module (BSD licensed) with three methods routed to
138
- Rust: `JsonPatch.__init__` (validation), `JsonPatch.from_diff` (`make_patch`) and
139
- `JsonPatch.apply` (`apply_patch`). Everything else is jsonpatch's code, so the public
140
- API is identical: `JsonPatch`, `make_patch`, `apply_patch`, `JsonPatch.from_string`,
141
- the operation classes, `DiffBuilder`, `multidict`, and the exceptions
142
- `JsonPatchException`, `InvalidJsonPatch`, `JsonPatchConflict`, `JsonPatchTestFailed`
143
- (with the same hierarchy, and `JsonPointerException` from jsonpointer).
144
-
145
- The Rust code handles the common case and hands anything else back to the original
146
- Python code, so results never differ:
147
-
148
- - **make_patch**: values it cannot reproduce exactly (non-JSON types that need
149
- comparing, dict or list subclasses, a custom `dumps=`, nesting deeper than 1000)
150
- are diffed by the original `DiffBuilder`.
151
- - **apply**: works on plain dicts and lists. An operation that would fail, or that
152
- touches anything else, is run by the original operation classes before anything is
153
- mutated, so errors have the same type and message.
154
- - Subclasses of `JsonPatch` with custom `operations`, `pointer_cls` or `json_dumper`,
155
- and patches that are not lists or tuples, use the original code.
156
-
157
- Known differences:
158
-
159
- - The exception classes belong to fastjsonpatch. An exception raised by the original
160
- `jsonpatch` package (for example inside another library that imports it) is not
161
- caught by `except fastjsonpatch.JsonPatchConflict`, and vice versa.
162
- - A value that is not a dict, list or JSON scalar inside a document being applied to
163
- is copied with `copy.deepcopy` on its own, so aliasing between such values is not
164
- preserved.
165
-
166
- `fastjsonpatch.__jsonpatch_version__` says which jsonpatch release the behaviour
167
- matches (currently `1.34`).
168
-
169
- ## How it is tested
170
-
171
- About 1,570 tests, run on every supported OS and Python version:
172
-
173
- - **Parity:** random documents, mutations and patches (valid and invalid) run through
174
- both fastjsonpatch and jsonpatch 1.34, comparing the resulting patches and documents
175
- by `repr` and errors by type and message. Also covers aliasing, cycles, non-JSON
176
- values, subclasses, deep nesting and every fallback path.
177
- - **jsonpatch's own test suite** and doctests, run against fastjsonpatch.
178
- - **RFC 6902 conformance:** the
179
- [json-patch-tests](https://github.com/json-patch/json-patch-tests) suite.
180
-
181
- The source distribution includes the tests and the benchmark.
182
-
183
- ## Development
184
-
185
- ```sh
186
- pip install maturin
187
- maturin develop --release
188
- pip install "jsonpatch==1.34" pytest # jsonpatch is the reference for the parity tests
189
- pytest tests
190
- python bench/compare.py # speed vs jsonpatch on this machine
191
- ```
192
-
193
- See [MAINTAINING.md](MAINTAINING.md) for releases and upkeep.
194
-
195
- ## License
196
-
197
- BSD-3-Clause. The Python module and the diff algorithm come from jsonpatch
198
- (Copyright (c) 2011 Stefan Kögl, BSD). The licenses of the Rust crates compiled into
199
- the wheels are in `THIRD_PARTY_LICENSES.md`.
200
-
@@ -1,172 +0,0 @@
1
- # fastjsonpatch
2
-
3
- A drop-in, faster [jsonpatch](https://github.com/stefankoegl/python-json-patch)
4
- ([RFC 6902](https://www.rfc-editor.org/rfc/rfc6902) JSON Patch) for Python.
5
- `make_patch` and `apply` run in Rust and return the **same patches, the same documents
6
- and the same exceptions** as jsonpatch 1.34: typically 10-50x faster to make a patch
7
- and 6-15x faster to apply one.
8
-
9
- ```sh
10
- pip install fastjsonpatch
11
- ```
12
-
13
- ```python
14
- import fastjsonpatch as jsonpatch # the only line you change
15
-
16
- src = {"name": "web", "replicas": 3, "ports": [80, 443]}
17
- dst = {"name": "web", "replicas": 5, "ports": [80, 443, 8080]}
18
-
19
- patch = jsonpatch.make_patch(src, dst)
20
- patch.patch
21
- # [{'op': 'replace', 'path': '/replicas', 'value': 5},
22
- # {'op': 'add', 'path': '/ports/2', 'value': 8080}]
23
-
24
- patch.apply(src) == dst # True; src is not modified
25
- jsonpatch.apply_patch(src, [{"op": "remove", "path": "/ports/0"}])
26
- # {'name': 'web', 'replicas': 3, 'ports': [443]}
27
- ```
28
-
29
- Python 3.12+ (CPython). Prebuilt wheels for Linux (x86_64 and aarch64, glibc and
30
- musl), macOS (Apple Silicon and Intel) and Windows (x64); one wheel per platform covers
31
- every Python from 3.12 on. The only dependency is `jsonpointer`.
32
-
33
- ## Why
34
-
35
- JSON Patch describes a change to a JSON document as a list of operations (`add`,
36
- `remove`, `replace`, `move`, `copy`, `test`). It is how HTTP `PATCH` requests,
37
- Kubernetes and many APIs express partial updates, and how tools store or send the
38
- difference between two versions of a document.
39
-
40
- jsonpatch is the standard Python implementation, and it is written in pure Python.
41
- That is fine for small documents, but the cost grows quickly:
42
-
43
- - `make_patch` on a 10,000-record document takes about 300 ms (on a fast machine),
44
- and diffing two Kubernetes OpenAPI specs (3.7 MB) takes about 140 ms.
45
- - `apply` deep-copies the whole document first, so even a one-operation patch on a
46
- large document costs tens of milliseconds.
47
- - A web service that applies or computes a patch per request spends 40-75 µs on
48
- each call, even for small resources.
49
-
50
- fastjsonpatch keeps jsonpatch's behaviour exactly and moves these hot paths to Rust,
51
- so existing code gets faster without changes.
52
-
53
- ## Speed
54
-
55
- Measured on Linux x86_64, Python 3.13 (times are the best of several runs).
56
-
57
- Real-world data:
58
-
59
- | scenario | size | jsonpatch | fastjsonpatch | speedup |
60
- |---|---|---|---|---|
61
- | Kubernetes OpenAPI v1.33 → v1.34: make_patch | 3.7 MB, 283 ops | 136.4 ms | 9.6 ms | **14x** |
62
- | Kubernetes OpenAPI v1.33 → v1.34: apply | 283 ops | 78.0 ms | 11.4 ms | **7x** |
63
- | 3-op patch on the 3.8 MB spec | | 71.1 ms | 10.1 ms | **7x** |
64
- | 3-op patch on the 3.8 MB spec, `in_place=True` | | 21.2 µs | 4.7 µs | **4x** |
65
- | PyPI metadata snapshots (8k records): make_patch | 2,487 ops | 255.2 ms | 12.1 ms | **21x** |
66
- | PyPI metadata snapshots: apply | 2,487 ops | 91.7 ms | 14.0 ms | **7x** |
67
- | REST resource, apply_patch per request | ×1000 | 47.4 ms | 7.5 ms | **6x** |
68
- | REST resource, make_patch per request | ×1000 | 74.4 ms | 5.6 ms | **13x** |
69
-
70
- Synthetic records shaped like `{"id", "name", "score", "tags": [..], "meta": {..}}`:
71
-
72
- | workload | ops | make_patch: jsonpatch | fastjsonpatch | speedup | apply: jsonpatch | fastjsonpatch | speedup |
73
- |---|---|---|---|---|---|---|---|
74
- | 10k records, 100 field edits | 100 | 292.61 ms | 5.26 ms | **56x** | 89.38 ms | 7.90 ms | **11x** |
75
- | 10k records, insert 1 at front | 49911 | 669.77 ms | 42.78 ms | **16x** | 535.21 ms | 72.35 ms | **7x** |
76
- | 10k records, delete 50 | 44900 | 636.53 ms | 40.46 ms | **16x** | 563.20 ms | 63.45 ms | **9x** |
77
- | 10k records, move 20 | 26305 | 434.93 ms | 24.38 ms | **18x** | 325.64 ms | 41.03 ms | **8x** |
78
- | 2k ints, insert 1 at front | 1 | 17.04 ms | 1.91 ms | **9x** | 581 µs | 21 µs | **27x** |
79
- | small config (200 keys), 1 edit | 1 | 1.98 ms | 35 µs | **57x** | 464 µs | 32 µs | **15x** |
80
-
81
- Python 3.14 made `copy.deepcopy` about 1.6x faster, so on 3.14 jsonpatch's `apply` on
82
- a large document is faster too and the gap shrinks to about 6x. `make_patch` is not
83
- affected. Run `python bench/compare.py` (from the source distribution) to measure on
84
- your own machine.
85
-
86
- ## Why it is faster
87
-
88
- **make_patch.** jsonpatch walks both documents in Python and compares every scalar
89
- with `json.dumps(a) == json.dumps(b)`. That serialisation is about half of the
90
- runtime; the rest is `isinstance` checks against abstract base classes and path
91
- tuples rebuilt at every level. fastjsonpatch walks the documents in Rust over the
92
- CPython objects directly (no conversion to a Rust data structure), compares values
93
- natively with the same rules as the `json.dumps` comparison (`1`, `1.0` and `True`
94
- are all different, `-0.0 != 0.0`, NaN equals NaN), keeps the current path on a stack,
95
- and takes a single lockstep pass when two dicts have the same keys in the same order.
96
- It is a port of jsonpatch's `DiffBuilder`, so it produces the same operations in the
97
- same order, including detected moves.
98
-
99
- **apply.** jsonpatch first runs `copy.deepcopy` on the document (so the original is
100
- not modified), then builds a `JsonPointer` and an operation object for each operation.
101
- For a small patch on a large document the copy is nearly all of the time.
102
- fastjsonpatch copies each dict and list with one C call and only recurses into values
103
- that are themselves containers; it keeps `deepcopy`'s semantics (shared and cyclic
104
- references are copied once) but skips the bookkeeping for containers referenced only
105
- once. It then resolves the paths and runs the operations without creating
106
- intermediate objects.
107
-
108
- ## Compatibility
109
-
110
- fastjsonpatch is jsonpatch's own module (BSD licensed) with three methods routed to
111
- Rust: `JsonPatch.__init__` (validation), `JsonPatch.from_diff` (`make_patch`) and
112
- `JsonPatch.apply` (`apply_patch`). Everything else is jsonpatch's code, so the public
113
- API is identical: `JsonPatch`, `make_patch`, `apply_patch`, `JsonPatch.from_string`,
114
- the operation classes, `DiffBuilder`, `multidict`, and the exceptions
115
- `JsonPatchException`, `InvalidJsonPatch`, `JsonPatchConflict`, `JsonPatchTestFailed`
116
- (with the same hierarchy, and `JsonPointerException` from jsonpointer).
117
-
118
- The Rust code handles the common case and hands anything else back to the original
119
- Python code, so results never differ:
120
-
121
- - **make_patch**: values it cannot reproduce exactly (non-JSON types that need
122
- comparing, dict or list subclasses, a custom `dumps=`, nesting deeper than 1000)
123
- are diffed by the original `DiffBuilder`.
124
- - **apply**: works on plain dicts and lists. An operation that would fail, or that
125
- touches anything else, is run by the original operation classes before anything is
126
- mutated, so errors have the same type and message.
127
- - Subclasses of `JsonPatch` with custom `operations`, `pointer_cls` or `json_dumper`,
128
- and patches that are not lists or tuples, use the original code.
129
-
130
- Known differences:
131
-
132
- - The exception classes belong to fastjsonpatch. An exception raised by the original
133
- `jsonpatch` package (for example inside another library that imports it) is not
134
- caught by `except fastjsonpatch.JsonPatchConflict`, and vice versa.
135
- - A value that is not a dict, list or JSON scalar inside a document being applied to
136
- is copied with `copy.deepcopy` on its own, so aliasing between such values is not
137
- preserved.
138
-
139
- `fastjsonpatch.__jsonpatch_version__` says which jsonpatch release the behaviour
140
- matches (currently `1.34`).
141
-
142
- ## How it is tested
143
-
144
- About 1,570 tests, run on every supported OS and Python version:
145
-
146
- - **Parity:** random documents, mutations and patches (valid and invalid) run through
147
- both fastjsonpatch and jsonpatch 1.34, comparing the resulting patches and documents
148
- by `repr` and errors by type and message. Also covers aliasing, cycles, non-JSON
149
- values, subclasses, deep nesting and every fallback path.
150
- - **jsonpatch's own test suite** and doctests, run against fastjsonpatch.
151
- - **RFC 6902 conformance:** the
152
- [json-patch-tests](https://github.com/json-patch/json-patch-tests) suite.
153
-
154
- The source distribution includes the tests and the benchmark.
155
-
156
- ## Development
157
-
158
- ```sh
159
- pip install maturin
160
- maturin develop --release
161
- pip install "jsonpatch==1.34" pytest # jsonpatch is the reference for the parity tests
162
- pytest tests
163
- python bench/compare.py # speed vs jsonpatch on this machine
164
- ```
165
-
166
- See [MAINTAINING.md](MAINTAINING.md) for releases and upkeep.
167
-
168
- ## License
169
-
170
- BSD-3-Clause. The Python module and the diff algorithm come from jsonpatch
171
- (Copyright (c) 2011 Stefan Kögl, BSD). The licenses of the Rust crates compiled into
172
- the wheels are in `THIRD_PARTY_LICENSES.md`.
File without changes
File without changes