From a70339f2d997a84d096edab1e8359cc748f12288 Mon Sep 17 00:00:00 2001 From: Matthias Brehler <72845586+mbrehler@users.noreply.github.com> Date: Mon, 2 Nov 2020 12:56:43 -0700 Subject: [PATCH 1/5] Enclose field names containing literals in quotes When creating the text representation of a path, field names that contain lexer literals (like a period ".') should be enclosed in quotes (or brackets). The proposed fix only adds quotes if needed (as opposed to alway). This avoids changing current paths/behavior drastically since it seems no one has run into this so far. Change-Id: Ia6e01f9b8218182aa61d3c39b16d4bfb57e4f593 --- jsonpath_ng/jsonpath.py | 18 +++++++++++++++--- tests/test_jsonpath.py | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/jsonpath_ng/jsonpath.py b/jsonpath_ng/jsonpath.py index 382d707..24e637b 100644 --- a/jsonpath_ng/jsonpath.py +++ b/jsonpath_ng/jsonpath.py @@ -1,8 +1,13 @@ -from __future__ import unicode_literals, print_function, absolute_import, division, generators, nested_scopes +from __future__ import (absolute_import, division, generators, nested_scopes, + print_function, unicode_literals) + import logging +from itertools import * # noqa + import six from six.moves import xrange -from itertools import * # noqa + +from jsonpath_ng.lexer import JsonPathLexer # Get logger name logger = logging.getLogger(__name__) @@ -539,7 +544,14 @@ def filter(self, fn, data): return data def __str__(self): - return ','.join(map(str, self.fields)) + # If any JsonPathLexer.literals are included in field name need quotes + # This avoids unnecessary quotes to keep strings short. + literals = JsonPathLexer.literals + # Test each field whether it contains a literal and only then add quotes + # The test loops over all literals, could possibly optimize to short circuit if one found + fields_as_str = ("'" + str(f) + "'" if any([l in f for l in literals]) else str(f) for f in self.fields) + return ','.join(fields_as_str) + def __repr__(self): return '%s(%s)' % (self.__class__.__name__, ','.join(map(repr, self.fields))) diff --git a/tests/test_jsonpath.py b/tests/test_jsonpath.py index 60670b1..a10bf9e 100644 --- a/tests/test_jsonpath.py +++ b/tests/test_jsonpath.py @@ -232,6 +232,8 @@ def test_child_paths(self): def test_descendants_paths(self): self.check_paths([('foo..baz', {'foo': {'baz': 1, 'bing': {'baz': 2}}}, ['foo.baz', 'foo.bing.baz'] )]) + def test_litreals_in_field_names(self): + self.check_paths([("A.'a.c'", {'A' : {'a.c': 'd'}}, ["A.'a.c'"])]) # # Check the "auto_id_field" feature From 4e5e14be5230debb7ad4ff4f54438ac9e0e80cc3 Mon Sep 17 00:00:00 2001 From: Matthias Brehler Date: Thu, 21 Jan 2021 10:08:44 -0700 Subject: [PATCH 2/5] Bump version to differentiate from mainline Since the pull request for changes has not been merged, enable differentiating which jsonpath-ng is installed. Change-Id: If42cb8aafbb79c8bc7b07a54473eb6ba8da9f50e --- jsonpath_ng/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonpath_ng/__init__.py b/jsonpath_ng/__init__.py index eec4a0f..4aab91b 100644 --- a/jsonpath_ng/__init__.py +++ b/jsonpath_ng/__init__.py @@ -3,4 +3,4 @@ # Current package version -__version__ = '1.5.2' +__version__ = '1.5.2.1' diff --git a/setup.py b/setup.py index 1659f62..d3bb04b 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setuptools.setup( name='jsonpath-ng', - version='1.5.2', + version='1.5.2.1', description=( 'A final implementation of JSONPath for Python that aims to be ' 'standard compliant, including arithmetic and binary comparison ' From 87818d31e0707185402617120d197ece230c3a08 Mon Sep 17 00:00:00 2001 From: Matthias Brehler Date: Tue, 12 Sep 2023 10:28:58 -0600 Subject: [PATCH 3/5] Address feedback from pull request Revert version change, get rid of variable for literals. Change-Id: I381d0a77c6832e96bb0c1f28afc15640c6e953f4 --- jsonpath_ng/__init__.py | 2 +- jsonpath_ng/jsonpath.py | 4 ++-- setup.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/jsonpath_ng/__init__.py b/jsonpath_ng/__init__.py index 7b16030..c3d7c19 100644 --- a/jsonpath_ng/__init__.py +++ b/jsonpath_ng/__init__.py @@ -3,4 +3,4 @@ # Current package version -__version__ = '1.5.3.1' +__version__ = '1.5.3' diff --git a/jsonpath_ng/jsonpath.py b/jsonpath_ng/jsonpath.py index c9047b4..d506302 100644 --- a/jsonpath_ng/jsonpath.py +++ b/jsonpath_ng/jsonpath.py @@ -620,10 +620,10 @@ def filter(self, fn, data): def __str__(self): # If any JsonPathLexer.literals are included in field name need quotes # This avoids unnecessary quotes to keep strings short. - literals = JsonPathLexer.literals # Test each field whether it contains a literal and only then add quotes # The test loops over all literals, could possibly optimize to short circuit if one found - fields_as_str = ("'" + str(f) + "'" if any([l in f for l in literals]) else str(f) for f in self.fields) + fields_as_str = ("'" + str(f) + "'" if any([l in f for l in JsonPathLexer.literals]) else + str(f) for f in self.fields) return ','.join(fields_as_str) diff --git a/setup.py b/setup.py index 2956b1c..8c11909 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setuptools.setup( name='jsonpath-ng', - version='1.5.3.1', + version='1.5.3', description=( 'A final implementation of JSONPath for Python that aims to be ' 'standard compliant, including arithmetic and binary comparison ' From b146c076e5d24eb54a970cdea2576b7e6a150536 Mon Sep 17 00:00:00 2001 From: Matthias Brehler Date: Tue, 19 Sep 2023 15:08:56 -0600 Subject: [PATCH 4/5] Update __init__.py Change-Id: Id767180b08a15aa6769f50dac43d09ceedf2875d --- jsonpath_ng/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonpath_ng/__init__.py b/jsonpath_ng/__init__.py index c3d7c19..689996e 100644 --- a/jsonpath_ng/__init__.py +++ b/jsonpath_ng/__init__.py @@ -3,4 +3,4 @@ # Current package version -__version__ = '1.5.3' +__version__ = '1.6.0.1' From ebfcce0e2e3d578e0597bd000fe936abc92bb20d Mon Sep 17 00:00:00 2001 From: Matthias Brehler Date: Tue, 19 Sep 2023 15:08:56 -0600 Subject: [PATCH 5/5] Update version to 1.6.0.1 Change-Id: Id767180b08a15aa6769f50dac43d09ceedf2875d --- jsonpath_ng/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonpath_ng/__init__.py b/jsonpath_ng/__init__.py index c3d7c19..689996e 100644 --- a/jsonpath_ng/__init__.py +++ b/jsonpath_ng/__init__.py @@ -3,4 +3,4 @@ # Current package version -__version__ = '1.5.3' +__version__ = '1.6.0.1' diff --git a/setup.py b/setup.py index 62e1e6d..8de0f64 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setuptools.setup( name='jsonpath-ng', - version='1.6.0', + version='1.6.0.1', description=( 'A final implementation of JSONPath for Python that aims to be ' 'standard compliant, including arithmetic and binary comparison '