public class JsonWriter
extends java.lang.Object
JsonObject
into an Appendable
using JSON string representation format.
This class is thread-safe.
Modifier and Type | Class and Description |
---|---|
protected static class |
JsonWriter.FieldData |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_INDENTATION_SPACES |
static boolean |
DEFAULT_OPEN_BRACES_ON_NEW_LINE |
Constructor and Description |
---|
JsonWriter()
Creates a new instance with opening braces '{' and '[' on the same line
and 2 indentation spaces per indentation level.
|
JsonWriter(boolean openingBracesOnNewLine)
Creates a new instance with a default of 2 indentation spaces per indentation level.
|
JsonWriter(int indentationSpaces)
Creates a new instance with opening braces '{' and '[' on the same line
and the specified indentation spaces per indentation level.
|
JsonWriter(int indentationSpaces,
boolean openingBracesOnNewLine)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
asString(JsonObject jsonObject)
Convenience method to convert the given
JsonObject to a pretty printed
JSON string representation. |
static JsonWriter |
create()
Creates a new instance with opening braces '{' and '[' on the same line.
|
static JsonWriter |
create(int indentationSpaces)
Creates a new instance with opening braces '{' and '[' on the same line
and the specified indentation spaces per indentation level.
|
protected FormattedTextWriter |
createFormattedTextWriterFor(java.lang.Appendable target) |
static JsonWriter |
createWithOpeningBracesOnNewLine()
Creates a new instance with opening braces '{' and '[' on a new line.
|
static JsonWriter |
createWithOpeningBracesOnNewLine(int indentationSpaces)
Creates a new instance with opening braces '{' and '[' on a new line.
|
int |
getIndentationSpaces()
Returns how many spaces per indentation level will be used for formatting.
|
boolean |
isOpeningBracesOnNewLine()
Returns true if opening braces like '{' and '[' are put on a new line.
|
static java.lang.String |
prettyPrint(JsonObject jsonObject)
Convenience method to convert the given
JsonObject to a pretty printed
JSON string representation (with opening braces on same line and 2 spaces indentation). |
void |
write(java.lang.Appendable target,
JsonObject jsonObject)
Writes the given
JsonObject to the specified Appendable with line breaks
and indentation. |
protected void |
writeArrayEnd(FormattedTextWriter writer) |
protected void |
writeArrayStart(FormattedTextWriter writer) |
protected void |
writeField(FormattedTextWriter writer,
JsonWriter.FieldData field) |
protected void |
writeJsonObject(FormattedTextWriter writer,
JsonObject jsonObject) |
protected void |
writeJsonObjectFields(FormattedTextWriter writer,
JsonObject jsonObject) |
protected void |
writeMultipleValues(FormattedTextWriter writer,
java.util.Collection<?> valueCollection) |
protected void |
writeObjectEnd(FormattedTextWriter writer) |
protected void |
writeObjectStart(FormattedTextWriter writer) |
protected void |
writeStart(FormattedTextWriter writer,
JsonSyntaxElement startElement) |
protected void |
writeValue(FormattedTextWriter writer,
java.lang.Object value,
boolean isSingleValue) |
public static final boolean DEFAULT_OPEN_BRACES_ON_NEW_LINE
public static final int DEFAULT_INDENTATION_SPACES
public JsonWriter()
public JsonWriter(boolean openingBracesOnNewLine)
openingBracesOnNewLine
- Defines whether or not opening braces '{' and '[' must be put on new line.public JsonWriter(int indentationSpaces)
indentationSpaces
- The number of spaces per indentation level.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public JsonWriter(int indentationSpaces, boolean openingBracesOnNewLine)
indentationSpaces
- The number of spaces per indentation level.openingBracesOnNewLine
- Defines whether or not opening braces '{' and '[' must be put on new line.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public static JsonWriter create()
public static JsonWriter createWithOpeningBracesOnNewLine()
public static JsonWriter create(int indentationSpaces)
indentationSpaces
- The number of spaces per indentation level.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public static JsonWriter createWithOpeningBracesOnNewLine(int indentationSpaces)
indentationSpaces
- The number of spaces per indentation level.java.lang.IllegalArgumentException
- if the given indentation size is less than 0 or greater than 20.public static java.lang.String prettyPrint(JsonObject jsonObject)
JsonObject
to a pretty printed
JSON string representation (with opening braces on same line and 2 spaces indentation).jsonObject
- The JSON object to convert to a pretty string.public java.lang.String asString(JsonObject jsonObject)
JsonObject
to a pretty printed
JSON string representation.jsonObject
- The JSON object to be converted to a string.public void write(java.lang.Appendable target, JsonObject jsonObject)
JsonObject
to the specified Appendable
with line breaks
and indentation.target
- The target to which write the output.jsonObject
- The object to be written as JSON string representation.protected void writeField(FormattedTextWriter writer, JsonWriter.FieldData field)
protected void writeValue(FormattedTextWriter writer, java.lang.Object value, boolean isSingleValue)
protected void writeJsonObject(FormattedTextWriter writer, JsonObject jsonObject)
protected void writeJsonObjectFields(FormattedTextWriter writer, JsonObject jsonObject)
protected void writeMultipleValues(FormattedTextWriter writer, java.util.Collection<?> valueCollection)
protected void writeObjectStart(FormattedTextWriter writer)
protected void writeObjectEnd(FormattedTextWriter writer)
protected void writeArrayStart(FormattedTextWriter writer)
protected void writeArrayEnd(FormattedTextWriter writer)
protected void writeStart(FormattedTextWriter writer, JsonSyntaxElement startElement)
public int getIndentationSpaces()
public boolean isOpeningBracesOnNewLine()
protected FormattedTextWriter createFormattedTextWriterFor(java.lang.Appendable target)