luajit-plus
前提条件
安装软件包要求用户具有 root 权限
机器必须能够访问以下的域名以下载软件包和 GPG 秘钥:
你可以通过执行以下命令验证域名可访问性:
curl https://openresty.org/package/pubkey.gpg curl https://pkg2.openresty.com/xray-priv-libs/
二进制安装包仓库
首先我们需要配置二进制安装包的仓库,按照以下命令进行配置。(命令中的 CLIENT_TOKEN 需要替换成订阅邮件中的有效 Token)
针对 Ubuntu/Debian/CentOS/Rocky/Red Hat Enterprise Linux/Fedora OS/Alibaba Cloud Linux/Tencent Linux 这些系统:
curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com/scripts/get-xray-priv-lib-repo.sh
sudo bash get-xray-priv-lib-repo.sh -l luajit-plus -t CLIENT_TOKEN
针对 Alpine 操作系统:
curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com/scripts/get-xray-priv-lib-repo.sh
sudo sh get-xray-priv-lib-repo.sh -l luajit-plus -t CLIENT_TOKEN
安装
对于使用 yum 包管理器的操作系统,执行以下命令进行私有库的安装。
sudo yum install --disablerepo="*" --enablerepo=luajit-plus -y openresty-luajit-plus openresty-luajit-plus-devel
对于使用 dnf 包管理器的操作系统,执行以下命令进行私有库的安装。
sudo dnf install --disablerepo="*" --enablerepo=luajit-plus -y openresty-luajit-plus openresty-luajit-plus-devel
对于使用 apt 包管理器的操作系统,执行以下命令进行私有库的安装。
sudo apt-get install -y openresty-luajit-plus openresty-luajit-plus-dev
对于 alpine 操作系统,执行以下命令进行私有库的安装。
sudo apk add openresty-luajit-plus openresty-luajit-plus-dev
CJSON 兼容性更新
此兼容性更新在 luajit-plus plus-v2.1-20261004 中首次引入。请升级至该版本或更高版本以使用以下行为。
数字编码精度
cjson.encode 默认使用 14 位有效数字,与 lua-cjson 保持一致。这是预期的兼容行为;需要超过 14 位有效数字的数值可能被舍入,也可能使用科学计数法输出。
local cjson = require "cjson"
print(cjson.encode_number_precision()) -- 14
print(cjson.encode(1000000000000001)) -- 1e+15
可以使用 encode_number_precision 将精度设置为 1 至 16,但这不保证所有浮点数都能无损往返转换。必须逐位保留的长整数标识符应使用字符串表示。部分旧版本默认输出的数字文本可能不同,升级时应检查依赖精确输出文本的签名、缓存键和测试。
解码与验证的兼容数字规则
在默认的 decode_invalid_numbers(true) 配置下,decode、validate 和 decode_file 都接受 lua-cjson 支持的非标准数字写法,例如 [01]、[1.]、[+1] 和 [0x10]。其中 [0x10] 解码后得到包含数字 16 的数组。
local cjson = require "cjson"
print(cjson.validate("[0x10]")) -- true
print(cjson.decode("[0x10]")[1]) -- 16
-- 如果 /data/example.json 的内容为 [0x10]:
print(cjson.decode_file("/data/example.json")[1]) -- 16
这些写法不是标准 JSON,因此 validate 返回 true 不代表输入符合严格的 JSON 标准。兼容解析遵守当前实例的配置,包括 decode_invalid_numbers、decode_allow_comment 和 decode_allow_trailing_comma;例如,设置 decode_invalid_numbers(false) 后,以上三个接口都会拒绝 [0x10]。
对于语法错误或超过实例的 decode_max_depth 限制,validate 返回 false,decode 和 decode_file 抛出相同的解析错误;使用 cjson.safe 时,后两个接口返回 nil, err。标准数字与兼容数字采用相同的深度限制。通过 new() 创建的实例保有独立配置。相同规则也适用于 jit.cjson 和 jit.cjson.safe。
validate 只做验证,不会构造解码结果,包括验证兼容数字写法时。decode_file 只读取文件一次,并保留内容供兼容解析使用。这两个接口当前在解释器中执行,包括从 JIT 热循环中调用时。
更多 API 用法参见 JIT CJSON 手册。
重新编译 OpenResty
可参照 installation 进行编译,执行 ./configure 时在 –with-luajit 参数后面加上 luajit-plus 的安装路径。
wget https://openresty.org/download/$pkg.tar.gz
tar -xzvf $pkg.tar.gz
cd $pkg
./configure --prefix=/opt/openresty \
--with-luajit=/usr/local/openresty/luajit-plus \
...
make
make install