Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions httpie/cli/nested_json/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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:
Expand Down
14 changes: 8 additions & 6 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
),
(
Expand Down Expand Up @@ -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}},
},
),
(
Expand Down
Loading