> ## 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.encode

> Serialise un Lua value en JSON string avec Real JSON encoder. json.stringify est enregistre sur la meme function.

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

  ## Arguments

  <ParamField path={"value"} type={"any"} required>
    Value a encode. nil, booleans, numbers, strings, tables et json.null sont traites directement. Unsupported Lua types sont encodes en null sauf si errorOnUnsupported est true.
  </ParamField>

  <ParamField path={"options"} type={"EncodeOptions"}>
    Optional encode options. Un boolean active/desactive pretty output. Une number active pretty output avec ce nombre de spaces clamp a 0.16. Un string active pretty output avec cet indent. Une table peut definir pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth et indent.
  </ParamField>

  ## Retours

  <ResponseField name={"json"} type={"string"}>
    Encoded JSON string. Encoding errors sont lancees comme Luau errors.
  </ResponseField>

  ## Types

  ### EncodeOptions

  Contrôle la sérialisation et le formatage JSON.

  | Name                         | Type      | Required | Description                                                              |
  | ---------------------------- | --------- | -------- | ------------------------------------------------------------------------ |
  | `pretty`                     | `boolean` | No       | Permet une sortie JSON formatée et multiligne.                           |
  | `sortKeys`                   | `boolean` | No       | Trie les clés d'objet avant l'encodage.                                  |
  | `emptyTableAsArray`          | `boolean` | No       | Encode une table vide sous forme de tableau au lieu d'un objet.          |
  | `errorOnUnsupported`         | `boolean` | No       | Génère une erreur au lieu de remplacer des valeurs non prises en charge. |
  | `encodeInvalidNumbersAsNull` | `boolean` | No       | Encode NaN et les nombres infinis en JSON null.                          |
  | `maxDepth`                   | `integer` | No       | Définit la profondeur d'imbrication maximale, comprise entre 1 et 4 096. |
  | `indent`                     | `string`  | No       | Définit la chaîne d'indentation, limitée à 32 caractères.                |

  ## Exemple

  Encode JSON-compatible values en utilisant json.array lorsqu un empty array doit rester un array.

  ```lua theme={null}
  local payload = {
      name = "Real",
      enabled = true,
      tags = json.array({ "api", "docs" }),
      missing = json.null,
  }

  local text = json.encode(payload, {
      sortKeys = true,
  })

  print(text)
  ```
</div>
