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

> Serializuje Lua value do JSON string przy uzyciu Real JSON encoder. json.stringify jest zarejestrowane do tej samej function.

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

  ## Argumenty

  <ParamField path={"value"} type={"any"} required>
    Value do encode. nil, booleans, numbers, strings, tables i json.null sa obslugiwane bezposrednio. Unsupported Lua types sa encode jako null, chyba ze errorOnUnsupported jest true.
  </ParamField>

  <ParamField path={"options"} type={"EncodeOptions"}>
    Optional encode options. boolean przelacza pretty output. number wlacza pretty output z taka liczba spaces clamp do 0.16. string wlacza pretty output z tym indent. table moze ustawic pretty, sortKeys, emptyTableAsArray, errorOnUnsupported, encodeInvalidNumbersAsNull, maxDepth i indent.
  </ParamField>

  ## Zwroty

  <ResponseField name={"json"} type={"string"}>
    Encoded JSON string. Encoding errors sa rzucane jako Luau errors.
  </ResponseField>

  ## Typy

  ### EncodeOptions

  Kontroluje serializację i formatowanie JSON.

  | Name                         | Type      | Required | Opis                                                                            |
  | ---------------------------- | --------- | -------- | ------------------------------------------------------------------------------- |
  | `pretty`                     | `boolean` | No       | Włącza sformatowane, wielowierszowe wyjście JSON.                               |
  | `sortKeys`                   | `boolean` | No       | Sortuje klucze obiektów przed kodowaniem.                                       |
  | `emptyTableAsArray`          | `boolean` | No       | Koduje pustą tabelę jako tablicę zamiast obiektu.                               |
  | `errorOnUnsupported`         | `boolean` | No       | Zgłasza błąd zamiast zastępować nieobsługiwane wartości.                        |
  | `encodeInvalidNumbersAsNull` | `boolean` | No       | Koduje liczby NaN i nieskończone jako JSON null.                                |
  | `maxDepth`                   | `integer` | No       | Ustawia maksymalną głębokość zagnieżdżenia, zaciśniętą w zakresie od 1 do 4096. |
  | `indent`                     | `string`  | No       | Ustawia ciąg wcięcia ograniczony do 32 znaków.                                  |

  ## Przykład

  Encode JSON-compatible values i uzyj json.array, gdy empty array musi pozostac 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>
