From 213f1941bfa3be7d73994aeb2795c138a4964249 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Wed, 20 May 2026 12:38:36 +0800 Subject: [PATCH 1/3] fix: mark decoded arrays with cjson array metatable --- lib/resty/simdjson/decoder.lua | 3 ++- t/02-decode.t | 15 +++++++++--- t/09-empty_array.t | 42 ++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/resty/simdjson/decoder.lua b/lib/resty/simdjson/decoder.lua index 774ed88..2f12899 100644 --- a/lib/resty/simdjson/decoder.lua +++ b/lib/resty/simdjson/decoder.lua @@ -5,6 +5,7 @@ local _MT = { __index = _M, } local ffi = require("ffi") local table_new = require("table.new") local C = require("resty.simdjson.cdefs") +local cjson_array_mt = require("cjson").array_mt local type = type @@ -110,7 +111,7 @@ function _M:_build_array(count) local err local n = 1 - local tbl = table_new(count, 0) + local tbl = setmetatable(table_new(count, 0), cjson_array_mt) local ops = self.ops local yieldable = self.yieldable diff --git a/t/02-decode.t b/t/02-decode.t index 14ac484..1e0af63 100644 --- a/t/02-decode.t +++ b/t/02-decode.t @@ -159,6 +159,7 @@ ok --- config location = /t { content_by_lua_block { + local cjson = require("cjson") local simdjson = require("resty.simdjson") local parser = simdjson.new() @@ -166,19 +167,28 @@ ok local v = parser:decode("[1, 2, 3]") assert(type(v) == "table") + assert(getmetatable(v) == cjson.array_mt) assert(#v == 3) assert(v[1] == 1 and v[2] == 2 and v[3] == 3) local v = parser:decode("[true, 2, \"abc\"]") assert(type(v) == "table") + assert(getmetatable(v) == cjson.array_mt) assert(#v == 3) assert(v[1] == true and v[2] == 2 and v[3] == "abc") local v = parser:decode("[1, null, 3]") assert(type(v) == "table") + assert(getmetatable(v) == cjson.array_mt) assert(#v == 3) assert(v[1] == 1 and v[2] == ngx.null and v[3] == 3) + local v = parser:decode("[[1], []]") + assert(type(v) == "table") + assert(getmetatable(v) == cjson.array_mt) + assert(getmetatable(v[1]) == cjson.array_mt) + assert(getmetatable(v[2]) == cjson.array_mt) + ngx.say("ok") } } @@ -198,6 +208,7 @@ ok --- config location = /t { content_by_lua_block { + local cjson = require("cjson") local simdjson = require("resty.simdjson") local parser = simdjson.new() @@ -205,6 +216,7 @@ ok local v = parser:decode([[{"a":1, "b":true, "c":"string"}]]) assert(type(v) == "table") + assert(getmetatable(v) ~= cjson.array_mt) assert(v.a == 1 and v.b == true and v.c == "string") local v = parser:decode([[{"a":1.0, "b":null, "c":false}]]) @@ -459,6 +471,3 @@ simdjson: error: STRING_ERROR: Problem while parsing a string [error] [warn] [crit] - - - diff --git a/t/09-empty_array.t b/t/09-empty_array.t index f79e1af..f4f4513 100644 --- a/t/09-empty_array.t +++ b/t/09-empty_array.t @@ -54,6 +54,46 @@ ok +=== TEST 4: cjson encode decoded empty array +--- http_config eval: $::HttpConfig +--- config + location = /t { + content_by_lua_block { + local cjson = require("cjson") + local simdjson = require("resty.simdjson") + + local parser = simdjson.new() + assert(parser) + + local v = parser:decode("[]") + assert(type(v) == "table") + assert(getmetatable(v) == cjson.array_mt) + assert(cjson.encode(v) == "[]") + + local v = parser:decode([[{"arr":[]}]]) + assert(type(v) == "table") + assert(getmetatable(v.arr) == cjson.array_mt) + assert(cjson.encode(v) == [[{"arr":[]}]]) + + local v = parser:decode("{}") + assert(type(v) == "table") + assert(getmetatable(v) ~= cjson.array_mt) + assert(cjson.encode(v) == "{}") + + ngx.say("ok") + } + } +--- request +GET /t +--- response_body +ok +--- no_error_log +[error] +[warn] +[crit] + + + === TEST 2: cjson empty_array userdata --- http_config eval: $::HttpConfig --- config @@ -111,5 +151,3 @@ ok [warn] [crit] - - From 37908f37efc47b13ffb487af124cea36f9c57570 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Wed, 20 May 2026 12:44:05 +0800 Subject: [PATCH 2/3] test: keep empty array cases in order --- t/09-empty_array.t | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/t/09-empty_array.t b/t/09-empty_array.t index f4f4513..3c868fd 100644 --- a/t/09-empty_array.t +++ b/t/09-empty_array.t @@ -54,7 +54,7 @@ ok -=== TEST 4: cjson encode decoded empty array +=== TEST 2: cjson encode decoded empty array --- http_config eval: $::HttpConfig --- config location = /t { @@ -94,7 +94,7 @@ ok -=== TEST 2: cjson empty_array userdata +=== TEST 3: cjson empty_array userdata --- http_config eval: $::HttpConfig --- config location = /t { @@ -123,7 +123,7 @@ ok -=== TEST 3: cjson empty_array_mt +=== TEST 4: cjson empty_array_mt --- http_config eval: $::HttpConfig --- config location = /t { @@ -150,4 +150,3 @@ ok [error] [warn] [crit] - From aa3816eb0a7d71cc04ebab6b9ce87cf722079a95 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Wed, 20 May 2026 12:50:46 +0800 Subject: [PATCH 3/3] ci: use ubuntu latest for tests --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 79a37d7..c0328e3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -11,7 +11,7 @@ concurrency: jobs: tests: name: Tests - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest strategy: matrix: