From 597e106fd1657d91fc1f4d791cc80105ac8f70e0 Mon Sep 17 00:00:00 2001 From: sahelii Date: Fri, 16 Jan 2026 13:16:27 +0530 Subject: [PATCH 1/3] Add tests for int and float JSON values and fix lexer regex - Add 42 and 3.14 to TEST_JSON_VALUES to test numeric JSON parsing - Fix EnhancedJsonLexer regex to handle prefixes before numeric JSON values - Resolves FIXME comment about missing int & float test coverage --- httpie/output/lexers/json.py | 3 +-- tests/test_json.py | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/httpie/output/lexers/json.py b/httpie/output/lexers/json.py index d26e522f30..9cdf94f71d 100644 --- a/httpie/output/lexers/json.py +++ b/httpie/output/lexers/json.py @@ -20,9 +20,8 @@ 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. ( - fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null).+)', + fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null|-?\d).+)', bygroups(PREFIX_TOKEN, using(JsonLexer)) ), # JSON body. diff --git a/tests/test_json.py b/tests/test_json.py index e758ebe7f4..4d2ccd23f1 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -28,12 +28,13 @@ '}', ] TEST_JSON_VALUES = [ - # FIXME: missing int & float {}, {'a': 0, 'b': 0}, [], ['a', 'b'], 'foo', + 42, + 3.14, True, False, None, From f587abee0bd246908925df03f843b4e796994362 Mon Sep 17 00:00:00 2001 From: sahelii Date: Fri, 16 Jan 2026 14:28:05 +0530 Subject: [PATCH 2/3] Fix regex to properly match full JSON numbers (integers and floats) The previous regex pattern -?\d only matched a single digit, which failed for multi-digit numbers and floats. Also changed .+ to .* to allow standalone numeric JSON values without requiring additional characters. This fixes the CI test failures for numeric JSON values with prefixes. --- httpie/output/lexers/json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpie/output/lexers/json.py b/httpie/output/lexers/json.py index 9cdf94f71d..b3bd152d3a 100644 --- a/httpie/output/lexers/json.py +++ b/httpie/output/lexers/json.py @@ -21,7 +21,7 @@ class EnhancedJsonLexer(RegexLexer): 'root': [ # Eventual non-JSON data prefix followed by actual JSON body. ( - fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null|-?\d).+)', + fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?).*)', bygroups(PREFIX_TOKEN, using(JsonLexer)) ), # JSON body. From 3a3e2455f0772970dac519a927465750c60e2a42 Mon Sep 17 00:00:00 2001 From: sahelii Date: Fri, 16 Jan 2026 15:10:56 +0530 Subject: [PATCH 3/3] Fix regex to handle numeric JSON values with prefixes - Updated EnhancedJsonLexer regex to support standalone int/float JSON values - Added numbers to JSON start pattern detection - Handles edge cases where numbers appear in prefixes (e.g., while(1);) - Most test cases passing (346/350), some edge cases remain for complex prefixes --- httpie/output/lexers/json.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/httpie/output/lexers/json.py b/httpie/output/lexers/json.py index b3bd152d3a..686f02a39c 100644 --- a/httpie/output/lexers/json.py +++ b/httpie/output/lexers/json.py @@ -5,6 +5,7 @@ from pygments.token import Token PREFIX_TOKEN = Token.Error +# Original prefix regex - matches anything except {, [, " PREFIX_REGEX = r'[^{\["]+' @@ -20,8 +21,11 @@ class EnhancedJsonLexer(RegexLexer): tokens = { 'root': [ # Eventual non-JSON data prefix followed by actual JSON body. + # 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|-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?).*)', + r'([^{\["]+?)(?=[{\["tfn]|(?