Package: groovy.json

## [Java] Class JsonOutput

- groovy.json.JsonOutput

-     public class JsonOutput
      extends Object

  Class responsible for the actual String serialization of the possible values of a JSON structure. This class can also be used as a category to add `toJson()` methods to various types.

  This class does not provide the ability to customize the resulting output. A JsonGenerator can be used if the ability to alter the resulting output is required.

  See Also:
    JsonGenerator
  Since:
    1.8.0

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `static class` | `JsonOutput.JsonUnescaped` | Represents unescaped JSON

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public static String` | `prettyPrint(String jsonPayload)` Pretty print a JSON payload.
    `` | `public static String` | `prettyPrint(String jsonPayload, boolean disableUnicodeEscaping)` Pretty print a JSON payload.
    `` | `public static String` | `prettyPrint(String jsonPayload, boolean disableUnicodeEscaping, int maxNestingDepth)` Pretty print a JSON payload, bounding its nesting depth explicitly.
    `` | `public static String` | `toJson(Boolean bool)` Returns: "true" or "false" for a boolean value
    `` | `public static String` | `toJson(Number n)` Returns: a string representation for a number
    `` | `public static String` | `toJson(Character c)` Returns: a JSON string representation of the character
    `` | `public static String` | `toJson(String s)` Returns: a properly encoded string with escape sequences
    `` | `public static String` | `toJson(Date date)` Format a date that is parseable from JavaScript, according to ISO-8601.
    `` | `public static String` | `toJson(Calendar cal)` Format a calendar instance that is parseable from JavaScript, according to ISO-8601.
    `` | `public static String` | `toJson(UUID uuid)` Returns: the string representation of an uuid
    `` | `public static String` | `toJson(URL url)` Returns: the string representation of the URL
    `` | `public static String` | `toJson(Closure closure)` Returns: an object representation of a closure
    `` | `public static String` | `toJson(Expando expando)` Returns: an object representation of an Expando
    `` | `public static String` | `toJson(Object object)` Returns: "null" for a null value, or a JSON array representation for a collection, array, iterator or enumeration, or representation for other object.
    `` | `public static String` | `toJson(Map m)` Returns: a JSON object representation for a map
    `` | `public static JsonOutput.JsonUnescaped` | `unescaped(CharSequence text)` Obtains JSON unescaped text for the given text

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### public static String prettyPrint(String jsonPayload)

      Pretty print a JSON payload.

      Nesting depth is bounded by the `groovy.json.maxNestingDepth` system property (default BaseJsonParser.DEFAULT_MAX_NESTING_DEPTH), matching the bound the slurpers enforce, so a payload this method will format is one JsonSlurper would parse. A payload nested deeper — or left unbalanced, which the lexer cannot distinguish from nesting — is rejected with a JsonException rather than expanded into an output quadratically larger than itself. A property value of `0` or less disables the check.

      Use prettyPrint(String, boolean, int) to bound a single call instead.

      Parameters:
        `jsonPayload` - a JSON payload
      Returns:
        a pretty representation of JSON payload.

    - #### public static String prettyPrint(String jsonPayload, boolean disableUnicodeEscaping)

      Pretty print a JSON payload.

      Nesting depth is bounded by the `groovy.json.maxNestingDepth` system property (default BaseJsonParser.DEFAULT_MAX_NESTING_DEPTH), matching the bound the slurpers enforce, so a payload this method will format is one JsonSlurper would parse. A payload nested deeper — or left unbalanced, which the lexer cannot distinguish from nesting — is rejected with a JsonException rather than expanded into an output quadratically larger than itself. A property value of `0` or less disables the check.

      Use prettyPrint(String, boolean, int) to bound a single call instead.

      Parameters:
        `jsonPayload` - a JSON payload
        `disableUnicodeEscaping` - whether to disable unicode escaping
      Returns:
        a pretty representation of JSON payload.
      Since:
        4.0.19

    - #### public static String prettyPrint(String jsonPayload, boolean disableUnicodeEscaping, int maxNestingDepth)

      Pretty print a JSON payload, bounding its nesting depth explicitly.

      Every nesting level adds to the indent of each line it encloses, so output size grows with the square of the payload's nesting and an unbounded payload can exhaust memory. A payload nested deeper than `maxNestingDepth` — or left unbalanced, which the lexer cannot distinguish from nesting — is rejected with a JsonException. Pass `0` or less to disable the check.

      The other overloads take this bound from the `groovy.json.maxNestingDepth` system property, which applies process-wide; prefer this method to format a deeply nested payload without changing how the rest of the process parses JSON.

      throws:
        JsonException if the payload nests deeper than `maxNestingDepth`
      Parameters:
        `jsonPayload` - a JSON payload
        `disableUnicodeEscaping` - whether to disable unicode escaping
        `maxNestingDepth` - the maximum nesting depth to accept; `0` or less disables the check
      Returns:
        a pretty representation of JSON payload.
      Since:
        6.0.0

    - #### public static String toJson(Boolean bool)

      Returns:
        "true" or "false" for a boolean value

    - #### public static String toJson(Number n)

      throws:
        JsonException if the number is infinite or not a number.
      Returns:
        a string representation for a number

    - #### public static String toJson(Character c)

      Returns:
        a JSON string representation of the character

    - #### public static String toJson(String s)

      Returns:
        a properly encoded string with escape sequences

    - #### public static String toJson(Date date)

      Format a date that is parseable from JavaScript, according to ISO-8601.

      Parameters:
        `date` - the date to format to a JSON string
      Returns:
        a formatted date in the form of a string

    - #### public static String toJson(Calendar cal)

      Format a calendar instance that is parseable from JavaScript, according to ISO-8601.

      Parameters:
        `cal` - the calendar to format to a JSON string
      Returns:
        a formatted date in the form of a string

    - #### public static String toJson(UUID uuid)

      Returns:
        the string representation of an uuid

    - #### public static String toJson(URL url)

      Returns:
        the string representation of the URL

    - #### public static String toJson(Closure closure)

      Returns:
        an object representation of a closure

    - #### public static String toJson(Expando expando)

      Returns:
        an object representation of an Expando

    - #### public static String toJson(Object object)

      Returns:
        "null" for a null value, or a JSON array representation for a collection, array, iterator or enumeration, or representation for other object.

    - #### public static String toJson(Map m)

      Returns:
        a JSON object representation for a map

    - #### public static JsonOutput.JsonUnescaped unescaped(CharSequence text)

      Obtains JSON unescaped text for the given text

      Parameters:
        `text` - The text
      Returns:
        The unescaped text