mirror of
https://github.com/redis/redis.git
synced 2026-04-21 03:01:35 -04:00
## Summary
This PR adds a new configuration option `decode_array_with_array_mt` to
lua_cjson that allows users to control how empty JSON arrays are handled
during encoding/decoding.
## Problem
Currently, lua_cjson has an ambiguity when handling empty tables:
- When decoding an empty JSON array `[]`, it becomes an empty Lua table.
This is mainly because both {} (object) and [] (array) are represented
as tables. The Lua cjson library then decides whether to encode a table
as a JSON object or as a JSON array, depending on its length. If the
length is not 0, it becomes a JSON array; otherwise, it is treated as a
JSON object.
## Solution
Added a new configuration option `decode_array_with_array_mt` (default:
`false` for backward compatibility):
- **When `false` (default)**: Maintains current behavior - empty arrays
decode to Lua table
- **When `true`**: Empty JSON arrays decode to tables with a special
metatable marker `__is_cjson_array`
```lua
-- Usage Example
-- Default behavior without decode_array_with_array_mt (backward compatible)
local arr = cjson.decode("[]") -- plain table {}
cjson.encode(arr) -- produces "{}"
-- Default behavior (backward compatible)
cjson.decode_array_with_array_mt(false)
local arr = cjson.decode("[]") -- plain table {}
cjson.encode(arr) -- produces "{}"
-- New behavior
cjson.decode_array_with_array_mt(true)
local arr = cjson.decode("[]") -- table with __is_cjson_array metatable
cjson.encode(arr) -- produces "[]"
```
## Note
this new Lua cjson API(decode_array_with_array_mt) references from
https://github.com/openresty/lua-cjson
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Ozan Tezcan <ozantezcan@gmail.com>
README for Lua 5.1 See INSTALL for installation instructions. See HISTORY for a summary of changes since the last released version. * What is Lua? ------------ Lua is a powerful, light-weight programming language designed for extending applications. Lua is also frequently used as a general-purpose, stand-alone language. Lua is free software. For complete information, visit Lua's web site at http://www.lua.org/ . For an executive summary, see http://www.lua.org/about.html . Lua has been used in many different projects around the world. For a short list, see http://www.lua.org/uses.html . * Availability ------------ Lua is freely available for both academic and commercial purposes. See COPYRIGHT and http://www.lua.org/license.html for details. Lua can be downloaded at http://www.lua.org/download.html . * Installation ------------ Lua is implemented in pure ANSI C, and compiles unmodified in all known platforms that have an ANSI C compiler. In most Unix-like platforms, simply do "make" with a suitable target. See INSTALL for detailed instructions. * Origin ------ Lua is developed at Lua.org, a laboratory of the Department of Computer Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil). For more information about the authors, see http://www.lua.org/authors.html . (end of README)