Skip to content

Enclose field names containing literals in quotes #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions jsonpath_ng/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .jsonpath import * # noqa
from .parser import parse # noqa


# Current package version
__version__ = '1.5.3'
from .jsonpath import * # noqa
from .parser import parse # noqa
# Current package version
__version__ = '1.5.3.1'
14 changes: 12 additions & 2 deletions jsonpath_ng/jsonpath.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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
from jsonpath_ng.lexer import JsonPathLexer

# Get logger name
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -615,7 +618,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)))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setuptools.setup(
name='jsonpath-ng',
version='1.5.3',
version='1.5.3.1',
description=(
'A final implementation of JSONPath for Python that aims to be '
'standard compliant, including arithmetic and binary comparison '
Expand Down
2 changes: 2 additions & 0 deletions tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,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_literals_in_field_names(self):
self.check_paths([("A.'a.c'", {'A' : {'a.c': 'd'}}, ["A.'a.c'"])])

#
# Check the "auto_id_field" feature
Expand Down