Class JsonOutput
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.
- Since:
- 1.8.0
- See Also:
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringprettyPrint(String jsonPayload) Pretty print a JSON payload.static StringprettyPrint(String jsonPayload, boolean disableUnicodeEscaping) Pretty print a JSON payload.static StringprettyPrint(String jsonPayload, boolean disableUnicodeEscaping, int maxNestingDepth) Pretty print a JSON payload, bounding its nesting depth explicitly.static Stringstatic Stringstatic Stringstatic Stringstatic Stringstatic Stringstatic Stringstatic Stringstatic StringFormat a calendar instance that is parseable from JavaScript, according to ISO-8601.static StringFormat a date that is parseable from JavaScript, according to ISO-8601.static Stringstatic Stringstatic JsonOutput.JsonUnescapedunescaped(CharSequence text) Obtains JSON unescaped text for the given text
-
Constructor Details
-
JsonOutput
public JsonOutput()
-
-
Method Details
-
toJson
- Returns:
- "true" or "false" for a boolean value
-
toJson
- Returns:
- a string representation for a number
- Throws:
JsonException- if the number is infinite or not a number.
-
toJson
- Returns:
- a JSON string representation of the character
-
toJson
- Returns:
- a properly encoded string with escape sequences
-
toJson
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
-
toJson
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
-
toJson
- Returns:
- the string representation of an uuid
-
toJson
- Returns:
- the string representation of the URL
-
toJson
- Returns:
- an object representation of a closure
-
toJson
- Returns:
- an object representation of an Expando
-
toJson
- Returns:
- "null" for a null value, or a JSON array representation for a collection, array, iterator or enumeration, or representation for other object.
-
toJson
- Returns:
- a JSON object representation for a map
-
prettyPrint
Pretty print a JSON payload.Nesting depth is bounded by the
groovy.json.maxNestingDepthsystem property (defaultBaseJsonParser.DEFAULT_MAX_NESTING_DEPTH), matching the bound the slurpers enforce, so a payload this method will format is oneJsonSlurperwould parse. A payload nested deeper — or left unbalanced, which the lexer cannot distinguish from nesting — is rejected with aJsonExceptionrather than expanded into an output quadratically larger than itself. A property value of0or 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.
-
prettyPrint
Pretty print a JSON payload.Nesting depth is bounded by the
groovy.json.maxNestingDepthsystem property (defaultBaseJsonParser.DEFAULT_MAX_NESTING_DEPTH), matching the bound the slurpers enforce, so a payload this method will format is oneJsonSlurperwould parse. A payload nested deeper — or left unbalanced, which the lexer cannot distinguish from nesting — is rejected with aJsonExceptionrather than expanded into an output quadratically larger than itself. A property value of0or less disables the check.Use
prettyPrint(String, boolean, int)to bound a single call instead.- Parameters:
jsonPayload- a JSON payloaddisableUnicodeEscaping- whether to disable unicode escaping- Returns:
- a pretty representation of JSON payload.
- Since:
- 4.0.19
-
prettyPrint
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 aJsonException. Pass0or less to disable the check.The other overloads take this bound from the
groovy.json.maxNestingDepthsystem property, which applies process-wide; prefer this method to format a deeply nested payload without changing how the rest of the process parses JSON.- Parameters:
jsonPayload- a JSON payloaddisableUnicodeEscaping- whether to disable unicode escapingmaxNestingDepth- the maximum nesting depth to accept;0or less disables the check- Returns:
- a pretty representation of JSON payload.
- Throws:
JsonException- if the payload nests deeper thanmaxNestingDepth- Since:
- 6.0.0
-
unescaped
Obtains JSON unescaped text for the given text- Parameters:
text- The text- Returns:
- The unescaped text
-