JSON

json.format

JSON を解析して再エンコードして、書式を正規化します。

構文

json.format(json: string, encodeOptions?: EncodeOptions, decodeOptions?: DecodeOptions): string

引数

名前説明
jsonstring解析、フォーマット、縮小、または検証する JSON 文字列。
encodeOptions?EncodeOptions出力の整形やキーの順序などのオプションのエンコード設定。
decodeOptions?DecodeOptionsJSON の読み取り中に使用されるオプションの解析設定。

戻り値

名前説明
formattedstringフォーマットされた JSON 文字列。

説明

JSON を解析して再エンコードして、書式を正規化します。

3 個のパラメータで呼び出します: json, encodeOptions, decodeOptions。引数表では必須値と動作を調整する値を確認できます。

formatted (string) を返します。戻り値表を使って成功値、nil 結果、回復可能なエラーを分けて扱ってください。

既存の JSON を読み取り可能な表現に正規化します。

local compact = '{"b":2,"a":1}'
local formatted = json.format(compact, {
    pretty = true,
    sortKeys = true,
})

print(formatted)

EncodeOptions

JSON のシリアル化とフォーマットを制御します。

pretty? boolean フォーマットされた複数行の JSON 出力を有効にします。sortKeys? boolean エンコード前にオブジェクトキーをソートします。emptyTableAsArray? boolean 空のテーブルをオブジェクトではなく配列としてエンコードします。errorOnUnsupported? boolean サポートされていない値を置き換えるのではなく、エラーを発生させます。encodeInvalidNumbersAsNull? boolean NaN と無限数を JSON null としてエンコードします。maxDepth? integer ネストの最大の深さを 1 ~ 4096 の範囲で設定します。indent? string インデント文字列を設定します。32 文字に制限されます。

DecodeOptions

JSON 解析動作を制御します。

useNull? boolean nil の代わりにライブラリの null センチネルを使用して JSON null を表します。maxDepth? integer ネストの最大の深さを 1 ~ 4096 の範囲で設定します。