From fed8c8356be4d94ceb55f2880bdb77fd9b55ef1e Mon Sep 17 00:00:00 2001 From: skewb1k Date: Tue, 10 Feb 2026 03:59:11 +0300 Subject: feature: add option to allow comments in decode. --- tests/test.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'tests') diff --git a/tests/test.lua b/tests/test.lua index 0513d97..068a9f0 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -333,6 +333,32 @@ local cjson_tests = { json.decode, { [["\uDB00\uD"]] }, false, { "Expected value but found invalid unicode escape code at character 2" } }, + -- Test comments + { 'Set decode_allow_comments(true)', + json.decode_allow_comments, { true }, true, { true } }, + { "Decode single-line comment", + json.decode, { '{//comment\n}' }, true, { {} } }, + { "Decode single-line comment with windows newline", + json.decode, { '{//comment\r\n}' }, true, { {} } }, + { "Decode single-line comment after string value", + json.decode, { '"test // /* */ string"//comment' }, true, { "test // /* */ string" } }, + { "Decode multi-line comment", + json.decode, { '{/* A multi-line\ncomment*/}' }, true, { {} } }, + { "Decode multi-line comment before colon", + json.decode, { '{"a" /* Comment */: 1}' }, true, { { a = 1 } } }, + { "Decode multi-line comment after colon", + json.decode, { '{"a": /* Comment */ 1}' }, true, { { a = 1 } } }, + { "Decode multiple comments in a row", + json.decode, { '/*first*//*second*/{}' }, true, { {} } }, + { "Decode unclosed multi-line comment [throw error]", + json.decode, { '{}/*Unclosed' }, + false, { "Expected the end but found unclosed multi-line comment at character 13" } }, + { "Decode comment inside number [throw error]", + json.decode, { '{"a":1/*x*/0}' }, + false, { "Expected comma or object end but found T_INTEGER at character 12" } }, + { 'Set decode_allow_comments(false)', + json.decode_allow_comments, { false }, true, { false } }, + -- Test indenting { 'Set encode_indent(" ")', json.encode_indent, { " " }, true, { " " } }, -- cgit v1.2.3-55-g6feb