# Method Standards.JSON.encode()

Method encode

  `string encode(int|float|string|array|mapping|object val, void|int flags, void|function(:void)|object|program|string callback)`

Description

  Encodes a value to a JSON string.

Parameter `val`

  The value to encode. It can contain integers, floats (except the special numbers NaN and infinity), strings, arrays, mappings with string indices, and the special object values null, true and false defined in this module (or really any object that implements an `encode_json` callback or is handled by the supplied callback argument).

Parameter `flags`

  Flag bit field to control formatting. See ASCII_ONLY, HUMAN_READABLE and PIKE_CANONICAL for further details.

Parameter `callback`

  A function that will be called for types that can not be encoded otherwise. It will be called with the value to be encoded as the first argument, and optionally with flags and indent arguments. If a string is supplied, it will be used to replace the value verbatim. The callback must return a string or throw an error.

Note

  8-bit and wider characters in input strings are neither escaped nor utf-8 encoded by default. string_to_utf8 can be used safely on the returned string to get a valid transport encoded JSON string. See escape_string for further details on string escaping.

See also

  escape_string