From e9a39321b12d20089f2b5642b5543717b425938a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 25 Mar 2026 16:42:18 +0300 Subject: [PATCH] json2: fix raw_decode failing to parse valid json content with escapes (fixes #23884) --- vlib/x/json2/scanner_test.v | 9 +++++++++ vlib/x/json2/tests/decoder_test.v | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/vlib/x/json2/scanner_test.v b/vlib/x/json2/scanner_test.v index 69e92dff1..ed6127d5f 100644 --- a/vlib/x/json2/scanner_test.v +++ b/vlib/x/json2/scanner_test.v @@ -30,6 +30,15 @@ fn test_str_valid_unicode_escape_2() { assert tok.lit.bytestr() == '✔' } +fn test_str_valid_escaped_backslashes_before_string_end() { + mut sc := Scanner{ + text: r'"some_value \n [ ] { } ( ) , ; ? * = ! \\@ \\"'.bytes() + } + tok := sc.scan() + assert tok.kind == .str + assert tok.lit.bytestr() == 'some_value \n [ ] { } ( ) , ; ? * = ! \\@ \\' +} + fn test_str_invalid_escape() { mut sc := Scanner{ text: r'"\z"'.bytes() diff --git a/vlib/x/json2/tests/decoder_test.v b/vlib/x/json2/tests/decoder_test.v index d0f225fa8..770fcf3f5 100644 --- a/vlib/x/json2/tests/decoder_test.v +++ b/vlib/x/json2/tests/decoder_test.v @@ -49,6 +49,13 @@ fn test_raw_decode_string_with_dollarsign() { assert str.str() == r'Hello $world' } +fn test_raw_decode_map_with_escaped_backslashes_before_string_end() { + content := r'{"some_name": "some_value \n [ ] { } ( ) , ; ? * = ! \\@ \\"}' + raw_mp := json.decode[json.Any](content)! + mp := raw_mp.as_map() + assert mp['some_name'] or { 0 }.str() == 'some_value \n [ ] { } ( ) , ; ? * = ! \\@ \\' +} + fn test_raw_decode_map_with_whitespaces() { raw_mp := json.decode[json.Any](' \n\t{"name":"Bob","age":20}\n\t')! mp := raw_mp.as_map() -- 2.39.5