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