Package arc.util.serialization
Class Json
java.lang.Object
arc.util.serialization.Json
Reads/writes Java objects to/from JSON, automatically.
TODO remove and replace with own implementation
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static interface
static interface
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addClassTag
(String tag, Class type) Sets a tag to use instead of the fully qualifier class name.protected String
convertToString
(Object object) void
copyFields
(Object from, Object to) Each field on theto
object is set to the value for the field with the same name on thefrom
object.void
copyFields
(Object from, Object to, boolean setFinals) <T> T
<T> T
<T> T
fromJson
(Class<T> type, InputStream input) <T> T
<T> T
<T> T
<T> T
fromJson
(Class<T> type, Class elementType, InputStream input) <T> T
<T> T
<T> T
Returns the class for the specified tag, or null.boolean
<T> Json.Serializer<T>
getSerializer
(Class<T> type) Returns the tag for the specified class, or null.protected boolean
ignoreUnknownField
(Class type, String fieldName) Called for each unknown field name encountered byreadFields(Object, JsonValue)
whenignoreUnknownFields
is false to determine whether the unknown field name should be ignored.protected Object
newInstance
(Class type) prettyPrint
(Object object) prettyPrint
(Object object, int singleLineColumns) prettyPrint
(Object object, JsonValue.PrettyPrintSettings settings) prettyPrint
(String json) prettyPrint
(String json, int singleLineColumns) prettyPrint
(String json, JsonValue.PrettyPrintSettings settings) void
void
void
void
void
void
readFields
(Object object, JsonValue jsonMap) <T> T
<T> T
<T> T
<T> T
<T> T
<T> T
<T> T
<T> T
void
setDefaultSerializer
(Json.Serializer defaultSerializer) Sets the serializer to use when the type being deserialized is not known (null).void
setElementType
(Class type, String fieldName, Class elementType) Sets the type of elements in a struct.void
setEnumNames
(boolean enumNames) When true,Enum.name()
is used to write enum values.void
setIgnoreDeprecated
(boolean ignoreDeprecated) When true, fields with theDeprecated
annotation will not be serialized.void
setIgnoreUnknownFields
(boolean ignoreUnknownFields) When true, fields in the JSON that are not found on the class will not throw aSerializationException
.void
setOutputType
(JsonWriter.OutputType outputType) void
setQuoteLongValues
(boolean quoteLongValues) void
setReadDeprecated
(boolean readDeprecated) When true, fields with theDeprecated
annotation will be read (but not written) whensetIgnoreDeprecated(boolean)
is true.<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.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.void
setUsePrototypes
(boolean usePrototypes) When true, field values that are identical to a newly constructed instance are not written.void
setWriter
(BaseJsonWriter writer) Sets the writer where JSON output will be written.void
void
void
void
void
void
void
toUBJson
(Object object, Class knownType, OutputStream stream) void
void
void
writeArrayStart
(String name) void
writeField
(Object object, String name) void
writeField
(Object object, String name, Class elementType) void
writeField
(Object object, String fieldName, String jsonName) void
writeField
(Object object, String fieldName, String jsonName, Class elementType) Writes the specified field to the current JSON object.void
writeFields
(Object object) Writes all fields of the specified object to the current JSON object.void
void
void
writeObjectStart
(Class actualType, Class knownType) Starts writing an object, writing the actualType to a field if needed.void
writeObjectStart
(String name) void
writeObjectStart
(String name, Class actualType, Class knownType) void
void
writeValue
(Object value) Writes the value, without writing the class of the object.void
writeValue
(Object value, Class knownType) Writes the value, writing the class of the object if it differs from the specified known type.void
writeValue
(Object value, Class knownType, Class elementType) Writes the value, writing the class of the object if it differs from the specified known type.void
writeValue
(String name, Object value) Writes the value as a field on the current JSON object, without writing the actual class.void
writeValue
(String name, Object value, Class 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.void
writeValue
(String name, Object value, Class knownType, Class 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.
-
Constructor Details
-
Json
public Json() -
Json
-
-
Method Details
-
getIgnoreUnknownFields
public boolean getIgnoreUnknownFields() -
setIgnoreUnknownFields
public void setIgnoreUnknownFields(boolean ignoreUnknownFields) When true, fields in the JSON that are not found on the class will not throw aSerializationException
. Default is true. -
setIgnoreDeprecated
public void setIgnoreDeprecated(boolean ignoreDeprecated) When true, fields with theDeprecated
annotation will not be serialized. -
setReadDeprecated
public void setReadDeprecated(boolean readDeprecated) When true, fields with theDeprecated
annotation will be read (but not written) whensetIgnoreDeprecated(boolean)
is true. -
setOutputType
- See Also:
-
setQuoteLongValues
public void setQuoteLongValues(boolean quoteLongValues) - See Also:
-
setEnumNames
public void setEnumNames(boolean enumNames) When true,Enum.name()
is used to write enum values. When false,Enum.toString()
is used which may not be unique. Default is true. -
addClassTag
Sets a tag to use instead of the fully qualifier class name. This can make the JSON easier to read. -
getClass
Returns the class for the specified tag, or null. -
getTag
Returns the tag for the specified class, or null. -
setTypeName
Sets the name of the JSON field to store the Java class name or class tag when required to avoid ambiguity during deserialization. Set to null to never output this information, but be warned that deserialization may fail. Default is "class". -
setDefaultSerializer
Sets the serializer to use when the type being deserialized is not known (null).- Parameters:
defaultSerializer
- May be null.
-
setSerializer
Registers a serializer to use for the specified type instead of the default behavior of serializing all of an objects fields. -
getSerializer
-
setUsePrototypes
public void setUsePrototypes(boolean usePrototypes) When true, field values that are identical to a newly constructed instance are not written. Default is true. -
setElementType
Sets the type of elements in a struct. When the element type is known, the class for each element in the struct does not need to be written unless different from the element type. -
getFields
-
toJson
-
toJson
-
toUBJson
-
toJson
- Parameters:
knownType
- May be null if the type is unknown.elementType
- May be null if the type is unknown.
-
toJson
-
toJson
- Parameters:
knownType
- May be null if the type is unknown.
-
toJson
- Parameters:
knownType
- May be null if the type is unknown.elementType
- May be null if the type is unknown.
-
toJson
-
toJson
- Parameters:
knownType
- May be null if the type is unknown.
-
toJson
- Parameters:
knownType
- May be null if the type is unknown.elementType
- May be null if the type is unknown.
-
getWriter
-
setWriter
Sets the writer where JSON output will be written. This is only necessary when not using the toJson methods. -
writeFields
Writes all fields of the specified object to the current JSON object. -
writeField
-
writeField
- Parameters:
elementType
- May be null if the type is unknown.- See Also:
-
writeField
-
writeField
Writes the specified field to the current JSON object.- Parameters:
elementType
- May be null if the type is unknown.
-
writeValue
Writes the value as a field on the current JSON object, without writing the actual class.- Parameters:
value
- May be null.- See Also:
-
writeValue
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.- Parameters:
value
- May be null.knownType
- May be null if the type is unknown.- See Also:
-
writeValue
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. The specified element type is used as the default type for collections.- Parameters:
value
- May be null.knownType
- May be null if the type is unknown.elementType
- May be null if the type is unknown.
-
writeValue
Writes the value, without writing the class of the object.- Parameters:
value
- May be null.
-
writeValue
Writes the value, writing the class of the object if it differs from the specified known type.- Parameters:
value
- May be null.knownType
- May be null if the type is unknown.
-
writeValue
Writes the value, writing the class of the object if it differs from the specified known type. The specified element type is used as the default type for collections.- Parameters:
value
- May be null.knownType
- May be null if the type is unknown.elementType
- May be null if the type is unknown.
-
writeObjectStart
-
writeObjectStart
- Parameters:
knownType
- May be null if the type is unknown.
-
writeObjectStart
public void writeObjectStart() -
writeObjectStart
Starts writing an object, writing the actualType to a field if needed.- Parameters:
knownType
- May be null if the type is unknown.
-
writeObjectEnd
public void writeObjectEnd() -
writeArrayStart
-
writeArrayStart
public void writeArrayStart() -
writeArrayEnd
public void writeArrayEnd() -
writeType
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
fromJson
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
readField
-
readField
-
readField
-
readField
public void readField(Object object, String fieldName, String jsonName, Class elementType, JsonValue jsonMap) - Parameters:
elementType
- May be null if the type is unknown.
-
readField
public void readField(Object object, Field field, String jsonName, Class elementType, JsonValue jsonMap) - Parameters:
object
- May be null if the field is static.elementType
- May be null if the type is unknown.
-
readFields
-
ignoreUnknownField
Called for each unknown field name encountered byreadFields(Object, JsonValue)
whenignoreUnknownFields
is false to determine whether the unknown field name should be ignored.- Parameters:
type
- The object type being read.fieldName
- A field name encountered in the JSON for which there is no matching class field.- Returns:
- true if the field name should be ignored and an exception won't be thrown by
readFields(Object, JsonValue)
.
-
readValue
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
public <T> T readValue(String name, Class<T> type, Class elementType, T defaultValue, JsonValue jsonMap) - Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
- Parameters:
type
- May be null if the type is unknown.- Returns:
- May be null.
-
readValue
-
readValue
- Parameters:
type
- May be null if the type is unknown.elementType
- May be null if the type is unknown.- Returns:
- May be null.
-
copyFields
Each field on theto
object is set to the value for the field with the same name on thefrom
object. Theto
object must have at least all the fields of thefrom
object with the same name and type. -
copyFields
-
convertToString
-
newInstance
-
prettyPrint
-
prettyPrint
-
prettyPrint
-
prettyPrint
-
prettyPrint
-
prettyPrint
-