APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.utils

public Class JsonWriter

extends Writer
Class Inheritance
Imports
java.io.IOException, .Writer, java.math.BigDecimal, .BigInteger, java.util.regex.Pattern

Builder API for emitting JSON to a Writer.
Author
Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum

Field Summary

Modifier and TypeField and Description
private static final int
private int
private static final int
private boolean
private static final int
private static final int
private static final int
private JsonWriter.OutputType
private boolean
private final IntArray
pack-priv Writer
Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
public
protected
JsonWriter()

A writer must be set before use.

Method Summary

Modifier and TypeMethod and Description
public JsonWriter
public JsonWriter
array(String name)

public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Closes the stream, flushing it first.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flushes the stream.

public Writer
public JsonWriter
json(String json)

Writes the specified JSON string, without quoting or escaping.

public JsonWriter
json(String name, String json)

Writes the specified JSON string, without quoting or escaping.

public JsonWriter
name(String name)

private void
public JsonWriter
public JsonWriter
object(String name)

public JsonWriter
pop()

private void
public JsonWriter
set(String name, Object value)

Prefer calling the more specific set() methods.

public JsonWriter
set(String name, String value)

public JsonWriter
set(String name, boolean value)

public JsonWriter
set(String name, int value)

public JsonWriter
set(String name, long value)

public JsonWriter
set(String name, float value)

public JsonWriter
set(String name, double value)

public void
setOutputType(JsonWriter.OutputType outputType)

Sets the type of JSON output.

public void
setQuoteLongValues(boolean quoteLongValues)

When true, long, double, BigInteger, BigDecimal types are output as strings to prevent truncation in languages like JavaScript and PHP.

public void
setWriter(Writer writer)

public JsonWriter
value(Object value)

Prefer calling the more specific value() methods.

public JsonWriter
value(String value)

public JsonWriter
value(boolean value)

public JsonWriter
value(int value)

public JsonWriter
value(long value)

public JsonWriter
value(float value)

public JsonWriter
value(double value)

public void
write(char[] cbuf, int off, int len)

Implements abstract java.io.Writer.write.

Writes a portion of an array of characters.

Inherited from java.io.Writer:
appendappendappendnullWriterwritewritewritewrite

Field Detail

arrayback to summary
private static final int array
currentback to summary
private int current
isObjectback to summary
private static final int isObject
namedback to summary
private boolean named
needsCommaback to summary
private static final int needsComma
noneback to summary
private static final int none
objectback to summary
private static final int object
outputTypeback to summary
private JsonWriter.OutputType outputType
quoteLongValuesback to summary
private boolean quoteLongValues
stackback to summary
private final IntArray stack
writerback to summary
pack-priv Writer writer

Constructor Detail

JsonWriterback to summary
public JsonWriter(Writer writer)
JsonWriterback to summary
protected JsonWriter()

A writer must be set before use.

Method Detail

arrayback to summary
public JsonWriter array() throws IOException
arrayback to summary
public JsonWriter array(String name) throws IOException
closeback to summary
public void close() throws IOException

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Doc from java.io.Writer.close.

Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.

Exceptions
IOException:
If an I/O error occurs
flushback to summary
public void flush() throws IOException

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Doc from java.io.Writer.flush.

Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Exceptions
IOException:
If an I/O error occurs
getWriterback to summary
public Writer getWriter()
jsonback to summary
public JsonWriter json(String json) throws IOException

Writes the specified JSON string, without quoting or escaping.

jsonback to summary
public JsonWriter json(String name, String json) throws IOException

Writes the specified JSON string, without quoting or escaping.

nameback to summary
public JsonWriter name(String name) throws IOException
nameValueback to summary
private void nameValue(String name) throws IOException
objectback to summary
public JsonWriter object() throws IOException
objectback to summary
public JsonWriter object(String name) throws IOException
popback to summary
public JsonWriter pop() throws IOException
requireCommaOrNameback to summary
private void requireCommaOrName() throws IOException
setback to summary
public JsonWriter set(String name, Object value) throws IOException

Prefer calling the more specific set() methods.

setback to summary
public JsonWriter set(String name, String value) throws IOException
setback to summary
public JsonWriter set(String name, boolean value) throws IOException
setback to summary
public JsonWriter set(String name, int value) throws IOException
setback to summary
public JsonWriter set(String name, long value) throws IOException
setback to summary
public JsonWriter set(String name, float value) throws IOException
setback to summary
public JsonWriter set(String name, double value) throws IOException
setOutputTypeback to summary
public void setOutputType(JsonWriter.OutputType outputType)

Sets the type of JSON output. Default is OutputType#json.

setQuoteLongValuesback to summary
public void setQuoteLongValues(boolean quoteLongValues)

When true, long, double, BigInteger, BigDecimal types are output as strings to prevent truncation in languages like JavaScript and PHP. This is not necessary when using libgdx, which handles these types without truncation. Default is false.

setWriterback to summary
public void setWriter(Writer writer)
valueback to summary
public JsonWriter value(Object value) throws IOException

Prefer calling the more specific value() methods.

valueback to summary
public JsonWriter value(String value) throws IOException
valueback to summary
public JsonWriter value(boolean value) throws IOException
valueback to summary
public JsonWriter value(int value) throws IOException
valueback to summary
public JsonWriter value(long value) throws IOException
valueback to summary
public JsonWriter value(float value) throws IOException
valueback to summary
public JsonWriter value(double value) throws IOException
writeback to summary
public void write(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Writer.write.

Doc from java.io.Writer.write.

Writes a portion of an array of characters.

Exceptions
IOException:
If an I/O error occurs
com.badlogic.gdx.utils back to summary

public final Enum JsonWriter.OutputType

extends Enum<JsonWriter.OutputType>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final JsonWriter.OutputType
javascript

Like JSON, but names are only double quoted if necessary.

private static final Pattern
public static final JsonWriter.OutputType
json

Normal JSON, with all its double quotes.

public static final JsonWriter.OutputType
minimal

Like JSON, but:

  • Names only require double quotes if they start with space or any of ":,}/ or they contain // or /* or :.
  • Values only require double quotes if they start with space or any of ":,{}[]/ or they contain // or /* or any of }], or they are equal to true, false, or null.
  • Newlines are treated as commas, making commas optional in many cases.
  • C style comments may be used: //... or /*...*/
private static final Pattern
private static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static String
escape(String value, int i)

private static String
public String
public String
public static JsonWriter.OutputType
public static JsonWriter.OutputType[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

javascriptback to summary
public static final JsonWriter.OutputType javascript

Like JSON, but names are only double quoted if necessary.

javascriptPatternback to summary
private static final Pattern javascriptPattern
jsonback to summary
public static final JsonWriter.OutputType json

Normal JSON, with all its double quotes.

minimalback to summary
public static final JsonWriter.OutputType minimal

Like JSON, but:

  • Names only require double quotes if they start with space or any of ":,}/ or they contain // or /* or :.
  • Values only require double quotes if they start with space or any of ":,{}[]/ or they contain // or /* or any of }], or they are equal to true, false, or null.
  • Newlines are treated as commas, making commas optional in many cases.
  • C style comments may be used: //... or /*...*/
minimalNamePatternback to summary
private static final Pattern minimalNamePattern
minimalValuePatternback to summary
private static final Pattern minimalValuePattern

Constructor Detail

OutputTypeback to summary
private OutputType()

Method Detail

escapeback to summary
private static String escape(String value, int i)
escapeQuoteback to summary
private static String escapeQuote(String value)
quoteNameback to summary
public String quoteName(String value)
quoteValueback to summary
public String quoteValue(Object value)
valueOfback to summary
public static JsonWriter.OutputType valueOf(String name)
valuesback to summary
public static JsonWriter.OutputType[] values()