Skip to content

Backslash probably should be treated as an escape character #30

@cragravorem

Description

@cragravorem

jsonpath2 version

0.4.0

Platform Details

sys.platform, sys.version == ('win32', '3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]')

Scenario:

How to query the folowing properties: " (double quote) and \ (backslash)

Steps to Reproduce:

Though some keys containing backslash and double quote can be queried:

>>> [m.current_value for m in Path.parse_str(r'''$["\\\""]''').match({r'\\\"': 42})]
[42]
>>> [m.current_value for m in Path.parse_str(r'''$["\\"]''').match({'\\\\': 42})]
[42]

the fact that backslashes are not treated as an escape symbol probably makes some keys impossible to query.

Expected Result:

Escaped backslash should match a single backslash in a key:

>>> [m.current_value for m in Path.parse_str(r'''$["\\"]''').match({'\\': 42})]
[42]

Escaped double quote should match a double quote in a key

>>> [m.current_value for m in Path.parse_str(r'''$["\""]''').match({'"': 42})]
[42]

Actual Result:

Escaped backslash does not match a single backslash in a key:

>>> [m.current_value for m in Path.parse_str(r'''$["\\"]''').match({'\\': 42})]
[]

but instead matches double backslashes in a key:

>>> [m.current_value for m in Path.parse_str(r'''$["\\"]''').match({'\\\\': 42})]
[42]

thus matching a single backslash seems to be impossible:

>>> [m.current_value for m in Path.parse_str(r'''$["\"]''').match({'\\': 42})]
line 1:2 token recognition error at: '"\"]'
File "antlr4\atn\LexerATNSimulator.py", line 99, in match
  return self.execATN(input, dfa.s0)
File "antlr4\atn\LexerATNSimulator.py", line 191, in execATN
  return self.failOrAccept(self.prevAccept, input, s.configs, t)
File "antlr4\atn\LexerATNSimulator.py", line 250, in failOrAccept
  raise LexerNoViableAltException(self.recog, input, self.startIndex, reach)
antlr4.error.Errors.LexerNoViableAltException: LexerNoViableAltException('"')

During handling of the above exception, another exception occurred:

File "antlr4\BufferedTokenStream.py", line 123, in fetch
  t = self.tokenSource.nextToken()
File "antlr4\Lexer.py", line 130, in nextToken
  self.notifyListeners(e)		# report error
File "antlr4\Lexer.py", line 285, in notifyListeners
  listener.syntaxError(self, None, self._tokenStartLine, self._tokenStartColumn, msg, e)
File "antlr4\error\ErrorListener.py", line 60, in syntaxError
  delegate.syntaxError(recognizer, offendingSymbol, line, column, msg, e)
File "jsonpath2\parser\__init__.py", line 63, in syntaxError
  raise ValueError('line {}:{} {}'.format(line, column, msg))
ValueError: line 1:2 token recognition error at: '"\"]'

Escaped double quote matches a double quote preceded by a backslash:

>>> [m.current_value for m in Path.parse_str(r'''$["\""]''').match({'\\"': 42})]
[42]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions