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
7 changes: 5 additions & 2 deletions httpie/output/lexers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pygments.token import Token

PREFIX_TOKEN = Token.Error
# Original prefix regex - matches anything except {, [, "
PREFIX_REGEX = r'[^{\["]+'


Expand All @@ -20,9 +21,11 @@ class EnhancedJsonLexer(RegexLexer):
tokens = {
'root': [
# Eventual non-JSON data prefix followed by actual JSON body.
# FIX: data prefix + number (integer or float) is not correctly handled.
# Handle numbers specially: match prefix that doesn't end with digits
# when a number follows (to avoid matching numbers in prefixes like while(1);)
(
fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null).+)',
r'([^{\["]+?)(?=[{\["tfn]|(?<![a-zA-Z0-9)])-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?(?![;)\]}]))'
+ r'((?:[{\["]|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?).*)',
bygroups(PREFIX_TOKEN, using(JsonLexer))
),
# JSON body.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
'}',
]
TEST_JSON_VALUES = [
# FIXME: missing int & float
{},
{'a': 0, 'b': 0},
[],
['a', 'b'],
'foo',
42,
3.14,
True,
False,
None,
Expand Down
Loading