Skip to content

jsonpath 0.4.4 can not parse "-" character #53

@ReigenDing

Description

@ReigenDing

jsonpath2 version

0.4.4

Platform Details

ubuntu20.04

Scenario:

import json
import typing

from jsonpath2.node import Node
from jsonpath2.nodes.root import RootNode
from jsonpath2.nodes.subscript import SubscriptNode
from jsonpath2.nodes.terminal import TerminalNode
from jsonpath2.path import Path
from jsonpath2.subscript import Subscript

data = json.loads("""
{
    "va-lues": [
        {"type": 1, "value": 2},
        {"type": 2, "value": 3},
        {"type": 1, "value": 10}
    ]
}
""")

path = Path.parse_str("$.va-lues.*[?(@.type = 1)].value")

def get_subscripts(node: Node) -> typing.List[typing.List[Subscript]]:
    return get_subscripts_(node, [])

def get_subscripts_(node: Node, accumulator: typing.List[typing.List[Subscript]]) -> typing.List[typing.List[Subscript]]:
    if isinstance(node, RootNode):
        return get_subscripts_(node.next_node, accumulator)
    elif isinstance(node, SubscriptNode):
        accumulator.append(node.subscripts)
        return get_subscripts_(node.next_node, accumulator)
    elif isinstance(node, TerminalNode):
        return accumulator

for match_data in path.match(data):
    print(f"Value: {match_data.current_value}")
    print(f"JSONPath: {match_data.node.tojsonpath()}")
    print(f"Subscripts: {get_subscripts(match_data.node)}")
    print("")

Steps to Reproduce:

[If you are filing an issue what are the things we need to do in order to reproduce your problem? How are you using this software or any resources it includes?]

Expected Result:

[What are you expecting to happen as the consequence of above reproduction steps?]

Value: 2
JSONPath: $["va-lues"][0]["value"]
Subscripts: [[<jsonpath2.subscripts.objectindex.ObjectIndexSubscript object at 0x10fe93d30>], [<jsonpath2.subscripts.arrayindex.ArrayIndexSubscript object at 0x10fe9d4f0>], [<jsonpath2.subscripts.objectindex.ObjectIndexSubscript object at 0x10fe93f10>]]

Value: 10
JSONPath: $["va-lues"][2]["value"]
Subscripts: [[<jsonpath2.subscripts.objectindex.ObjectIndexSubscript object at 0x10fe93d30>], [<jsonpath2.subscripts.arrayindex.ArrayIndexSubscript object at 0x10fe9d7f0>], [<jsonpath2.subscripts.objectindex.ObjectIndexSubscript object at 0x10fe93f10>]]

Actual Result:

[What actually happens after the reproduction steps? Include the error output or a link to a gist if possible.]

line 1:4 token recognition error at: '-l'
Traceback (most recent call last):
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/Lexer.py", line 128, in nextToken
    ttype = self._interp.match(self._input, self._mode)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/atn/LexerATNSimulator.py", line 99, in match
    return self.execATN(input, dfa.s0)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/atn/LexerATNSimulator.py", line 191, in execATN
    return self.failOrAccept(self.prevAccept, input, s.configs, t)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/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:

Traceback (most recent call last):
  File "/Users/qiao/workspace-py/json-test/jsonpath2-test.py", line 21, in <module>
    path = Path.parse_str("$.va-lues.*[?(@.type = 1)].value")
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/path.py", line 88, in parse_str
    return cls(_parser.parse_str(*args, **kwargs))
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/__init__.py", line 468, in parse_str
    return _parse_input_stream(input_stream)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/__init__.py", line 445, in _parse_input_stream
    tree = parser.jsonpath()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/JSONPathParser.py", line 227, in jsonpath
    self.subscript()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/JSONPathParser.py", line 436, in subscript
    self.subscriptableBareword()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/JSONPathParser.py", line 662, in subscriptableBareword
    self.match(JSONPathParser.ID)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/Parser.py", line 116, in match
    self.consume()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/Parser.py", line 340, in consume
    self.getInputStream().consume()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 99, in consume
    if self.sync(self.index + 1):
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 111, in sync
    fetched = self.fetch(n)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/BufferedTokenStream.py", line 123, in fetch
    t = self.tokenSource.nextToken()
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/Lexer.py", line 130, in nextToken
    self.notifyListeners(e)             # report error
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/Lexer.py", line 285, in notifyListeners
    listener.syntaxError(self, None, self._tokenStartLine, self._tokenStartColumn, msg, e)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/antlr4/error/ErrorListener.py", line 60, in syntaxError
    delegate.syntaxError(recognizer, offendingSymbol, line, column, msg, e)
  File "/Users/qiao/workspace-py/json-test/venv/lib/python3.8/site-packages/jsonpath2/parser/__init__.py", line 63, in syntaxError
    raise ValueError('line {}:{} {}'.format(line, column, msg))
ValueError: line 1:4 token recognition error at: '-l'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions