> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# json.pretty

> 使用与 json.encodePretty 相同的 function 将 Lua value serializes 为 readable formatted JSON string。

<div className="real-function-page">
  ```lua theme={null}
  json.pretty(value: any, options?: EncodeOptions): string
  ```

  ## 参数

  <ParamField path={"value"} type={"any"} required>
    要 encoded 的 value。nil、booleans、numbers、strings、tables 和 json.null 会直接处理。Unsupported Lua types 会 encoded 为 null，除非 errorOnUnsupported 为 true。
  </ParamField>

  <ParamField path={"options"} type={"EncodeOptions"}>
    Optional encode options。json.pretty 读取与 json.encodePretty 相同的 options，然后强制 pretty 为 true。没有 options 时使用 two-space indent。number 设置 indent width 并 clamp 到 0.16，string 设置 indent text，table 可以设置 sortKeys、emptyTableAsArray、errorOnUnsupported、encodeInvalidNumbersAsNull、maxDepth 和 indent。
  </ParamField>

  ## 返回值

  <ResponseField name={"json"} type={"string"}>
    Readable formatted JSON string。Encoding errors 会作为 Luau errors 抛出。
  </ResponseField>

  ## 类型

  ### EncodeOptions

  控制 JSON 序列化和格式设置。

  | Name                         | Type      | Required | 说明                       |
  | ---------------------------- | --------- | -------- | ------------------------ |
  | `pretty`                     | `boolean` | No       | 启用格式化的多行 JSON 输出。        |
  | `sortKeys`                   | `boolean` | No       | 在编码之前对对象键进行排序。           |
  | `emptyTableAsArray`          | `boolean` | No       | 将空表编码为数组而不是对象。           |
  | `errorOnUnsupported`         | `boolean` | No       | 引发错误而不是替换不受支持的值。         |
  | `encodeInvalidNumbersAsNull` | `boolean` | No       | 将 NaN 和无限数编码为 JSON null。 |
  | `maxDepth`                   | `integer` | No       | 设置最大嵌套深度，范围为 1 到 4096。   |
  | `indent`                     | `string`  | No       | 设置缩进字符串，限制为 32 个字符。      |

  ## 示例

  使用 json.encodePretty alias 将 JSON-compatible values encode 为 readable formatted JSON。

  ```lua theme={null}
  local text = json.pretty({
      category = "JSON",
      tags = json.array({ "pretty", "alias" }),
      missing = json.null,
  }, {
      sortKeys = true,
  })

  print(text)
  ```
</div>
