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

public Class Json

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .Reader, .StringWriter, .Writer, java.util.ArrayList, .Arrays, .Collection, .Collections, .HashMap, .Map, com.badlogic.gdx.files.FileHandle, com.badlogic.gdx.utils.IntSet.IntSetIterator, .JsonValue.PrettyPrintSettings, .JsonWriter.OutputType, .ObjectMap.Entry, com.badlogic.gdx.utils.reflect.ArrayReflection, .ClassReflection, .Constructor, .Field, .ReflectionException

Reads/writes Java objects to/from JSON, automatically. See the wiki for usage: https://libgdx.com/wiki/utils/reading-and-writing-json
Author
Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public abstract static class
public static interface
public static interface

Field Summary

Modifier and TypeField and Description
private final ObjectMap<Class<T>, Object[]>
private final ObjectMap<Class<T>, Json.Serializer<T>>
private final ObjectMap<Class<T>, String>
private static final boolean
private Json.Serializer<T>
private boolean
private final Object[]
private final Object[]
private boolean
private boolean
private JsonWriter.OutputType
private boolean
private boolean
private JsonReader
private boolean
private final ObjectMap<String, Class<T>>
private String
private final ObjectMap<Class<T>, OrderedMap<String, Json.FieldMetadata>>
private boolean
private JsonWriter

Constructor Summary

AccessConstructor and Description
public
Json()

public

Method Summary

Modifier and TypeMethod and Description
public void
addClassTag(String tag, Class<T> type)

Sets a tag to use instead of the fully qualifier class name.

private String
convertToString(Enum<E extends Enum<E>> e)

private String
public void
copyFields(Object from, Object to)

Each field on the to object is set to the value for the field with the same name on the from object.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Reader reader)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
Reader reader)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
InputStream input)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
InputStream input)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
FileHandle file)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
FileHandle file)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
char[] data, int offset, int length)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
char[] data, int offset, int length)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
String json)

Returns: May be null.

public <T> T
fromJson(Class<T>
May be null if the type is unknown.
type
,
Class<T> elementType, String json)

Returns: May be null.

public Class<T>
getClass(String tag)

Returns the class for the specified tag, or null.

private Object[]
private OrderedMap<String, Json.FieldMetadata>
getFields(Class<T> type)

public boolean
public JsonReader
public <T> Json.Serializer<T>
public String
getTag(Class<T> type)

Returns the tag for the specified class, or null.

public JsonWriter
protected boolean

Returns:

true if the field name should be ignored and an exception won't be thrown by readFields(Object, JsonValue).
ignoreUnknownField
(Object
The object whose fields are being read.
object
,
JsonValue
The JsonValue with the field name for which there is no matching class field.
value
)

Called for each unknown field name encountered by readFields(Object, JsonValue) when ignoreUnknownFields is false to determine whether the unknown field name should be ignored.

protected Object
newInstance(Class<T> type)

public String
public String
public String
prettyPrint(Object object, int singleLineColumns)

public String
prettyPrint(String json, int singleLineColumns)

public String
public String
public void
readField(Object object, String name, JsonValue jsonData)

public void
readField(Object object, String name, Class<T> elementType, JsonValue jsonData)

public void
readField(Object object, String fieldName, String jsonName, JsonValue jsonData)

public void
readField(Object object, String fieldName, String jsonName, Class<T>
May be null if the type is unknown.
elementType
,
JsonValue jsonMap)

public void
readField(Object
May be null if the field is static.
object
,
Field field, String jsonName, Class<T>
May be null if the type is unknown.
elementType
,
JsonValue jsonMap)

public void
readFields(Object object, JsonValue jsonMap)

public <T> T
readValue(String name, Class<T>
May be null if the type is unknown.
type
,
JsonValue jsonMap)

Returns: May be null.

public <T> T
readValue(String name, Class<T>
May be null if the type is unknown.
type
,
T defaultValue, JsonValue jsonMap)

Returns: May be null.

public <T> T
readValue(String name, Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
JsonValue jsonMap)

Returns: May be null.

public <T> T
readValue(String name, Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
T defaultValue, JsonValue jsonMap)

Returns: May be null.

public <T> T
readValue(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
T defaultValue, JsonValue jsonData)

Returns: May be null.

public <T> T
readValue(Class<T>
May be null if the type is unknown.
type
,
JsonValue jsonData)

Returns: May be null.

public <T> T
readValue(Class<T>
May be null if the type is unknown.
type
,
Class<T>
May be null if the type is unknown.
elementType
,
JsonValue jsonData)

Returns: May be null.

public void
setDefaultSerializer(Json.Serializer<T> defaultSerializer)

Sets the serializer to use when the type being deserialized is not known (null).

public void
setDeprecated(Class<T> type, String fieldName, boolean deprecated)

The specified field will be treated as if it has or does not have the Deprecated annotation.

public void
setElementType(Class<T> type, String fieldName, Class<T> elementType)

Sets the type of elements in a collection.

public void
setEnumNames(boolean enumNames)

When true, Enum#name() is used to write enum values.

public void
setIgnoreDeprecated(boolean ignoreDeprecated)

When true, fields with the Deprecated annotation will not be read or written.

public void
setIgnoreUnknownFields(boolean ignoreUnknownFields)

When true, fields in the JSON that are not found on the class will not throw a SerializationException.

public void
public void
setQuoteLongValues(boolean quoteLongValues)

Default is false.

public void
setReadDeprecated(boolean readDeprecated)

When true, fields with the Deprecated annotation will be read (but not written) when setIgnoreDeprecated(boolean) is true.

public void
setReader(JsonReader reader)

Sets the reader to use when reading JSON

public <T> void
setSerializer(Class<T> type, Json.Serializer<T> serializer)

Registers a serializer to use for the specified type instead of the default behavior of serializing all of an objects fields.

public void
setSortFields(boolean sortFields)

When true, fields are sorted alphabetically when written, otherwise the source code order is used.

public void
setTypeName(String typeName)

Sets the name of the JSON field to store the Java class name or class tag when required to avoid ambiguity during deserialization.

public void
setUsePrototypes(boolean usePrototypes)

When true, field values that are identical to a newly constructed instance are not written.

public void
setWriter(Writer writer)

Sets the writer where JSON output will be written.

protected void
sortFields(Class<T> type, Array<String> fieldNames)

Called to sort the fields for a class.

public String
toJson(Object object)

public String
toJson(Object object, Class<T> knownType)

public String
toJson(Object object, Class<T>
May be null if the type is unknown.
knownType
,
Class<T>
May be null if the type is unknown.
elementType
)

public void
toJson(Object object, FileHandle file)

public void
toJson(Object object, Class<T>
May be null if the type is unknown.
knownType
,
FileHandle file)

public void
toJson(Object object, Class<T>
May be null if the type is unknown.
knownType
,
Class<T>
May be null if the type is unknown.
elementType
,
FileHandle file)

public void
toJson(Object object, Writer writer)

public void
toJson(Object object, Class<T>
May be null if the type is unknown.
knownType
,
Writer writer)

public void
toJson(Object object, Class<T>
May be null if the type is unknown.
knownType
,
Class<T>
May be null if the type is unknown.
elementType
,
Writer writer)

public void
public void
public void
public void
writeField(Object object, String name)

public void
writeField(Object object, String name, Class<T>
May be null if the type is unknown.
elementType
)

public void
writeField(Object object, String fieldName, String jsonName)

public void
writeField(Object object, String fieldName, String jsonName, Class<T>
May be null if the type is unknown.
elementType
)

Writes the specified field to the current JSON object.

public void
writeFields(Object object)

Writes all fields of the specified object to the current JSON object.

public void
public void
public void
writeObjectStart(String name, Class<T> actualType, Class<T>
May be null if the type is unknown.
knownType
)

public void
public void
writeObjectStart(Class<T> actualType, Class<T>
May be null if the type is unknown.
knownType
)

Starts writing an object, writing the actualType to a field if needed.

public void
writeType(Class<T> type)

public void
writeValue(String name, Object
May be null.
value
)

Writes the value as a field on the current JSON object, without writing the actual class.

public void
writeValue(String name, Object
May be null.
value
,
Class<T>
May be null if the type is unknown.
knownType
)

Writes the value as a field on the current JSON object, writing the class of the object if it differs from the specified known type.

public void
writeValue(String name, Object
May be null.
value
,
Class<T>
May be null if the type is unknown.
knownType
,
Class<T>
May be null if the type is unknown.
elementType
)

Writes the value as a field on the current JSON object, writing the class of the object if it differs from the specified known type.

public void
writeValue(Object
May be null.
value
)

Writes the value, without writing the class of the object.

public void
writeValue(Object
May be null.
value
,
Class<T>
May be null if the type is unknown.
knownType
)

Writes the value, writing the class of the object if it differs from the specified known type.

public void
writeValue(Object
May be null.
value
,
Class<T>
May be null if the type is unknown.
knownType
,
Class<T>
May be null if the type is unknown.
elementType
)

Writes the value, writing the class of the object if it differs from the specified known type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait