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
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interfacestatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ObjectMap<Class,Json.Serializer> protected static final OrderedMap<Class,Json.Serializer> protected Json.Serializerbooleanbooleanbooleanprotected final OrderedMap<Class,Json.Serializer> booleanbooleanprotected final ObjectMap<Class,OrderedMap<String, Json.FieldMetadata>> booleanprotected BaseJsonWriter -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClassTag(String tag, Class type) Sets a tag to use instead of the fully qualifier class name.protected <T> TconvertFromString(String value, Class type) protected StringconvertToString(Object object, Class knownType) voidcopyFields(Object from, Object to) Each field on thetoobject is set to the value for the field with the same name on thefromobject.voidcopyFields(Object from, Object to, boolean setFinals) protected static ClassfindActualType(Class clazz) <T> Json.Serializer<T>findSerializer(Class<T> type) Try to find a serializer usinggetSerializer(Class)andgetInheritSerializer(Class)<T> T<T> T<T> TfromJson(Class<T> type, InputStream input) <T> T<T> T<T> T<T> TfromJson(Class<T> type, Class elementType, InputStream input) <T> T<T> T<T> T<T> TfromJson(Class<T> type, Class elementType, Class keyType, InputStream input) <T> T<T> T<T> T<T> TReturns the class for the specified tag, or null.boolean<T> Json.Serializer<T>getInheritSerializer(Class<T> type) <T> Json.Serializer<T>getSerializer(Class<T> type) Returns the tag for the specified class, or null.protected booleanignoreUnknownField(Class type, String fieldName) Called for each unknown field name encountered byreadFields(Object, JsonValue)whenignoreUnknownFieldsis false to determine whether the unknown field name should be ignored.booleanprotected static ObjectnewInstance(Class type) prettyPrint(Object object) prettyPrint(Object object, JsonWriter.OutputType outputType) prettyPrint(String json) prettyPrint(String json, JsonWriter.OutputType outputType) <T> T<T> T<T> T<T> T<T> TreadField(Class type, String fieldName, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) voidvoidreadField(Object object, Field field, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) voidvoidvoidvoidvoidvoidreadField(Object object, String fieldName, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) <T> T<T> TreadFields(Class type, JsonValue jsonMap) voidreadFields(Object object, JsonValue jsonMap) <T> T<T> T<T> T<T> T<T> T<T> T<T> T<T> TvoidsetDefaultSerializer(Json.Serializer defaultSerializer) Sets the serializer to use when the type being deserialized is not known (null).voidsetElementType(Class type, String fieldName, Class elementType) Sets the type of elements in a struct.voidsetEnumNames(boolean enumNames) When true,Enum.name()is used to write enum values.voidsetIgnoreDeprecated(boolean ignoreDeprecated) When true, fields with theDeprecatedannotation will not be serialized.voidsetIgnoreUnknownFields(boolean ignoreUnknownFields) When true, fields in the JSON that are not found on the class will not throw aSerializationException.<T> voidsetInheritSerializer(Class<T> type, Json.Serializer<T> serializer) voidsetOutputType(JsonWriter.OutputType outputType) voidsetQuoteLongValues(boolean quoteLongValues) voidsetReadDeprecated(boolean readDeprecated) When true, fields with theDeprecatedannotation will be read (but not written) whensetIgnoreDeprecated(boolean)is true.<T> voidsetSerializer(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.voidsetTypeName(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.voidsetUsePrototypes(boolean usePrototypes) When true, field values that are identical to a newly constructed instance are not written.voidsetWriter(BaseJsonWriter writer) Sets the writer where JSON output will be written.voidvoidvoidvoidvoidvoidvoidprotected voidvoidtoJsonValue(Object object) toJsonValue(Object object, Class knownType) toJsonValue(Object object, Class knownType, Class elementType) toJsonValue(Object object, Class knownType, Class elementType, Class keyType) voidtoUBJson(Object object, OutputStream stream) voidtoUBJson(Object object, Class knownType, OutputStream stream) voidtoUBJson(Object object, Class knownType, Class elementType, OutputStream stream) voidvoidvoidvoidwriteArrayStart(String name) voidwriteField(Object object, String name) voidwriteField(Object object, String name, Class elementType) voidwriteField(Object object, String fieldName, String jsonName) voidwriteField(Object object, String fieldName, String jsonName, Class elementType) Writes the specified field to the current JSON object.voidwriteFields(Object object) Writes all fields of the specified object to the current JSON object.voidvoidvoidwriteObjectStart(Class actualType, Class knownType) Starts writing an object, writing the actualType to a field if needed.voidwriteObjectStart(String name) voidwriteObjectStart(String name, Class actualType, Class knownType) voidvoidwriteValue(Object value) Writes the value, without writing the class of the object.voidwriteValue(Object value, Class knownType) Writes the value, writing the class of the object if it differs from the specified known type.voidwriteValue(Object value, Class knownType, Class elementType) Writes the value, writing the class of the object if it differs from the specified known type.voidwriteValue(Object value, Class knownType, Class elementType, Class keyType) Writes the value, writing the class of the object if it differs from the specified known type.voidwriteValue(String name, Object value) Writes the value as a field on the current JSON object, without writing the actual class.voidwriteValue(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.voidwriteValue(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.
-
Field Details
-
defaultInheritSerializers
-
typeToFields
-
tagToClass
-
classToTag
-
classToSerializer
-
inheritSerializers
-
classToDefaultValues
-
writer
-
typeName
-
usePrototypes
public boolean usePrototypes -
outputType
-
quoteLongValues
public boolean quoteLongValues -
ignoreUnknownFields
public boolean ignoreUnknownFields -
ignoreDeprecated
public boolean ignoreDeprecated -
readDeprecated
public boolean readDeprecated -
enumNames
public boolean enumNames -
defaultSerializer
-
-
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 theDeprecatedannotation will not be serialized. -
setReadDeprecated
public void setReadDeprecated(boolean readDeprecated) When true, fields with theDeprecatedannotation will be read (but not written) whensetIgnoreDeprecated(boolean)is true. -
setOutputType
- See Also:
-
getOutputType
-
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". -
getTypeName
-
isTypeNameSet
public boolean isTypeNameSet() -
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
-
findSerializer
Try to find a serializer usinggetSerializer(Class)andgetInheritSerializer(Class) -
setInheritSerializer
-
getInheritSerializer
-
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
-
toUBJson
-
toUBJson
-
toUBJson
-
toUBJson
public void toUBJson(Object object, Class knownType, Class elementType, Class keyType, OutputStream stream) -
toJsonValue
-
toJsonValue
-
toJsonValue
-
toJsonValue
-
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
- Parameters:
knownType- May be null if the type is unknown.elementType- May be null if the type is unknown.keyType- 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
- Parameters:
knownType- May be null if the type is unknown.elementType- May be null if the type is unknown.keyType- 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
protected void toJson(Object object, Class knownType, Class elementType, Class keyType, BaseJsonWriter writer) -
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.
-
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. And the specified key type is used as the default key type for maps.- 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.elementType- May be null if the type is unknown.keyType- 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.elementType- May be null if the type is unknown.keyType- 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.elementType- May be null if the type is unknown.keyType- 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
public <T> T fromJson(Class<T> type, Class elementType, Class keyType, char[] data, int offset, int length) - Parameters:
type- May be null if the type is unknown.elementType- May be null if the type is unknown.keyType- 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.
-
fromJson
- Parameters:
type- May be null if the type is unknown.elementType- May be null if the type is unknown.keyType- May be null if the type is unknown.- Returns:
- May be null.
-
readField
-
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, String fieldName, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) - Parameters:
elementType- May be null if the type is unknown.keyType- 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.
-
readField
public void readField(Object object, Field field, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) - Parameters:
object- May be null if the field is static.elementType- May be null if the type is unknown.keyType- May be null if the type is unknown.
-
readFields
-
readField
-
readField
-
readField
-
readField
public <T> T readField(Class type, String fieldName, String jsonName, Class elementType, JsonValue jsonMap) - Parameters:
elementType- May be null if the type is unknown.
-
readField
public <T> T readField(Class type, String fieldName, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) - Parameters:
elementType- May be null if the type is unknown.keyType- May be null if the type is unknown.
-
readField
- Parameters:
object- May be null if the field is static.elementType- May be null if the type is unknown.
-
readField
public <T> T readField(Field field, String jsonName, Class elementType, Class keyType, JsonValue jsonMap) - Parameters:
object- May be null if the field is static.elementType- May be null if the type is unknown.keyType- May be null if the type is unknown.
-
readFields
- Returns:
- the read fields, instead of setting them on the object
-
ignoreUnknownField
Called for each unknown field name encountered byreadFields(Object, JsonValue)whenignoreUnknownFieldsis 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 thetoobject is set to the value for the field with the same name on thefromobject. Thetoobject must have at least all the fields of thefromobject with the same name and type. -
copyFields
-
convertToString
-
convertFromString
-
newInstance
-
findActualType
-
prettyPrint
-
prettyPrint
-
prettyPrint
-
prettyPrint
-