From 8faa749af272fd07890a818efc0f55fa5f1c6f3a Mon Sep 17 00:00:00 2001 From: Christophe Demko Date: Thu, 17 Jul 2025 16:53:18 +0200 Subject: [PATCH 1/3] Fix single constant case --- jsonpath_ng/jsonpath.py | 2 +- tests/test_jsonpath.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/jsonpath_ng/jsonpath.py b/jsonpath_ng/jsonpath.py index 550b52c..f683dcd 100644 --- a/jsonpath_ng/jsonpath.py +++ b/jsonpath_ng/jsonpath.py @@ -796,7 +796,7 @@ def find(self, datum): return [] # Here's the hack. If it is a dictionary or some kind of constant, # put it in a single-element list - if (isinstance(datum.value, dict) or isinstance(datum.value, int) or isinstance(datum.value, str)): + if (isinstance(datum.value, dict) or isinstance(datum.value, (int, float, str, bool))): return self.find(DatumInContext([datum.value], path=datum.path, context=datum.context)) # Some iterators do not support slicing but we can still diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index abd105e..3ff9c63 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -227,6 +227,10 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_ # Slices # ------ # + ("[*]", 1, [1], ["[0]"]), + ("[*]", 1.2, [1.2], ["[0]"]), + ("[*]", True, [True], ["[0]"]), + ("[*]", "test", ["test"], ["[0]"]), ("[*]", [1, 2, 3], [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[*]", range(1, 4), [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[1:]", [1, 2, 3, 4], [2, 3, 4], ["[1]", "[2]", "[3]"]), From 13357fc6bd4472efe2805fcf68664528d1470187 Mon Sep 17 00:00:00 2001 From: Christophe Demko Date: Thu, 17 Jul 2025 17:07:51 +0200 Subject: [PATCH 2/3] Fix False and None values --- jsonpath_ng/jsonpath.py | 2 +- tests/test_jsonpath.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/jsonpath_ng/jsonpath.py b/jsonpath_ng/jsonpath.py index f683dcd..290fc98 100644 --- a/jsonpath_ng/jsonpath.py +++ b/jsonpath_ng/jsonpath.py @@ -792,7 +792,7 @@ def find(self, datum): datum = DatumInContext.wrap(datum) # Used for catching null value instead of empty list in path - if not datum.value: + if datum.value is None: return [] # Here's the hack. If it is a dictionary or some kind of constant, # put it in a single-element list diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 3ff9c63..95db585 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -230,7 +230,9 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_ ("[*]", 1, [1], ["[0]"]), ("[*]", 1.2, [1.2], ["[0]"]), ("[*]", True, [True], ["[0]"]), + ("[*]", False, [False], ["[0]"]), ("[*]", "test", ["test"], ["[0]"]), + ("[*]", None, [], []), ("[*]", [1, 2, 3], [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[*]", range(1, 4), [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[1:]", [1, 2, 3, 4], [2, 3, 4], ["[1]", "[2]", "[3]"]), From 3273580b3d56e0d72a8c6ee32b9c1856e69dd6c4 Mon Sep 17 00:00:00 2001 From: Christophe Demko Date: Thu, 17 Jul 2025 17:42:33 +0200 Subject: [PATCH 3/3] Group funny hacks --- tests/test_jsonpath.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 95db585..7ccf900 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -227,12 +227,6 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_ # Slices # ------ # - ("[*]", 1, [1], ["[0]"]), - ("[*]", 1.2, [1.2], ["[0]"]), - ("[*]", True, [True], ["[0]"]), - ("[*]", False, [False], ["[0]"]), - ("[*]", "test", ["test"], ["[0]"]), - ("[*]", None, [], []), ("[*]", [1, 2, 3], [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[*]", range(1, 4), [1, 2, 3], ["[0]", "[1]", "[2]"]), ("[1:]", [1, 2, 3, 4], [2, 3, 4], ["[1]", "[2]", "[3]"]), @@ -247,6 +241,11 @@ def test_update(parse: Callable[[str], JSONPath], expression: str, data, update_ # -------------------- # ("[*]", 1, [1], ["[0]"]), + ("[*]", 1.2, [1.2], ["[0]"]), + ("[*]", True, [True], ["[0]"]), + ("[*]", False, [False], ["[0]"]), + ("[*]", "test", ["test"], ["[0]"]), + ("[*]", None, [], []), ("[0:]", 1, [1], ["[0]"]), ("[*]", {"foo": 1}, [{"foo": 1}], ["[0]"]), ("[*].foo", {"foo": 1}, [1], ["[0].foo"]),