diff --git a/httpie/cli/nested_json/parse.py b/httpie/cli/nested_json/parse.py index 323a22eef1..5acf037b4a 100644 --- a/httpie/cli/nested_json/parse.py +++ b/httpie/cli/nested_json/parse.py @@ -128,6 +128,7 @@ def tokenize(source: str) -> Iterator[Token]: cursor = 0 backslashes = 0 buffer = [] + seen_operator = False def send_buffer() -> Iterator[Token]: nonlocal backslashes @@ -137,15 +138,16 @@ def send_buffer() -> Iterator[Token]: value = ''.join(buffer) kind = TokenKind.TEXT if not backslashes: - for variation, kind in [ - (int, TokenKind.NUMBER), - (check_escaped_int, TokenKind.TEXT), - ]: + variations = [(int, TokenKind.NUMBER)] + if seen_operator: + variations.append((check_escaped_int, TokenKind.TEXT)) + for variation, candidate_kind in variations: try: value = variation(value) except ValueError: continue else: + kind = candidate_kind break yield Token( kind=kind, @@ -163,6 +165,7 @@ def can_advance() -> bool: index = source[cursor] if index in OPERATORS: yield from send_buffer() + seen_operator = True yield Token(OPERATORS[index], index, cursor, cursor + 1) elif index == BACKSLASH and can_advance(): if source[cursor + 1] in SPECIAL_CHARS: diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..42905afebd 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -394,7 +394,7 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value): '\\2012 bleh': 2013, 'bleh \\2012': 2014, }, - '2012': {'x': 2, '[3]': 4}, + r'\2012': {'x': 2, '[3]': 4}, }, ), ( @@ -429,14 +429,16 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value): ), ( [ + r'\0=\0', r'\1=top level int', - r'\\1=escaped top level int', - r'\2[\3][\4]:=5', + r'\\2=escaped top level int', + r'\3[\4][\5]:=5', ], { - '1': 'top level int', - '\\1': 'escaped top level int', - '2': {'3': {'4': 5}}, + r'\0': r'\0', + r'\1': 'top level int', + r'\2': 'escaped top level int', + r'\3': {'4': {'5': 5}}, }, ), (