Class Json

java.lang.Object
arc.util.serialization.Json

public class Json extends Object
Reads/writes Java objects to/from JSON, automatically. TODO remove and replace with own implementation
  • Constructor Details

  • 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 a SerializationException. Default is true.
    • setIgnoreDeprecated

      public void setIgnoreDeprecated(boolean ignoreDeprecated)
      When true, fields with the Deprecated annotation will not be serialized.
    • setReadDeprecated

      public void setReadDeprecated(boolean readDeprecated)
      When true, fields with the Deprecated annotation will be read (but not written) when setIgnoreDeprecated(boolean) is true.
    • setOutputType

      public void setOutputType(JsonWriter.OutputType outputType)
      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

      public void addClassTag(String tag, Class type)
      Sets a tag to use instead of the fully qualifier class name. This can make the JSON easier to read.
    • getClass

      public Class getClass(String tag)
      Returns the class for the specified tag, or null.
    • getTag

      public String getTag(Class type)
      Returns the tag for the specified class, or null.
    • setTypeName

      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. Set to null to never output this information, but be warned that deserialization may fail. Default is "class".
    • setDefaultSerializer

      public void setDefaultSerializer(Json.Serializer defaultSerializer)
      Sets the serializer to use when the type being deserialized is not known (null).
      Parameters:
      defaultSerializer - May be null.
    • setSerializer

      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.
    • getSerializer

      public <T> Json.Serializer<T> getSerializer(Class<T> type)
    • 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

      public void setElementType(Class type, String fieldName, Class elementType)
      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

      public OrderedMap<String,Json.FieldMetadata> getFields(Class type)
    • toJson

      public String toJson(Object object)
    • toJson

      public String toJson(Object object, Class knownType)
    • toUBJson

      public void toUBJson(Object object, Class knownType, OutputStream stream)
    • toJson

      public String toJson(Object object, Class knownType, Class elementType)
      Parameters:
      knownType - May be null if the type is unknown.
      elementType - May be null if the type is unknown.
    • toJson

      public void toJson(Object object, Fi file)
    • toJson

      public void toJson(Object object, Class knownType, Fi file)
      Parameters:
      knownType - May be null if the type is unknown.
    • toJson

      public void toJson(Object object, Class knownType, Class elementType, Fi file)
      Parameters:
      knownType - May be null if the type is unknown.
      elementType - May be null if the type is unknown.
    • toJson

      public void toJson(Object object, Writer writer)
    • toJson

      public void toJson(Object object, Class knownType, Writer writer)
      Parameters:
      knownType - May be null if the type is unknown.
    • toJson

      public void toJson(Object object, Class knownType, Class elementType, Writer writer)
      Parameters:
      knownType - May be null if the type is unknown.
      elementType - May be null if the type is unknown.
    • getWriter

      public BaseJsonWriter getWriter()
    • setWriter

      public void setWriter(BaseJsonWriter writer)
      Sets the writer where JSON output will be written. This is only necessary when not using the toJson methods.
    • writeFields

      public void writeFields(Object object)
      Writes all fields of the specified object to the current JSON object.
    • writeField

      public void writeField(Object object, String name)
      See Also:
    • writeField

      public void writeField(Object object, String name, Class elementType)
      Parameters:
      elementType - May be null if the type is unknown.
      See Also:
    • writeField

      public void writeField(Object object, String fieldName, String jsonName)
      See Also:
    • writeField

      public void writeField(Object object, String fieldName, String jsonName, Class elementType)
      Writes the specified field to the current JSON object.
      Parameters:
      elementType - May be null if the type is unknown.
    • writeValue

      public void writeValue(String name, Object value)
      Writes the value as a field on the current JSON object, without writing the actual class.
      Parameters:
      value - May be null.
      See Also:
    • writeValue

      public 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.
      Parameters:
      value - May be null.
      knownType - May be null if the type is unknown.
      See Also:
    • writeValue

      public 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. 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

      public void writeValue(Object value)
      Writes the value, without writing the class of the object.
      Parameters:
      value - May be null.
    • writeValue

      public void writeValue(Object value, Class knownType)
      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

      public 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. 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

      public void writeObjectStart(String name)
    • writeObjectStart

      public void writeObjectStart(String name, Class actualType, Class knownType)
      Parameters:
      knownType - May be null if the type is unknown.
    • writeObjectStart

      public void writeObjectStart()
    • writeObjectStart

      public void writeObjectStart(Class actualType, Class knownType)
      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

      public void writeArrayStart(String name)
    • writeArrayStart

      public void writeArrayStart()
    • writeArrayEnd

      public void writeArrayEnd()
    • writeType

      public void writeType(Class type)
    • fromJson

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

      public <T> T fromJson(Class<T> type, Class elementType, Reader reader)
      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, InputStream input)
      Parameters:
      type - May be null if the type is unknown.
      Returns:
      May be null.
    • fromJson

      public <T> T fromJson(Class<T> type, Class elementType, InputStream input)
      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, Fi file)
      Parameters:
      type - May be null if the type is unknown.
      Returns:
      May be null.
    • fromJson

      public <T> T fromJson(Class<T> type, Class elementType, Fi file)
      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, char[] data, int offset, int length)
      Parameters:
      type - May be null if the type is unknown.
      Returns:
      May be null.
    • fromJson

      public <T> T fromJson(Class<T> type, Class elementType, 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.
      Returns:
      May be null.
    • fromJson

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

      public <T> T fromJson(Class<T> type, Class elementType, String json)
      Parameters:
      type - May be null if the type is unknown.
      Returns:
      May be null.
    • readField

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

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

      public void readField(Object object, String fieldName, String jsonName, JsonValue jsonData)
    • 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

      public void readFields(Object object, JsonValue jsonMap)
    • ignoreUnknownField

      protected boolean ignoreUnknownField(Class type, String fieldName)
      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.
      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

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

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

      public <T> T readValue(String name, Class<T> type, Class elementType, 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

      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

      public <T> T readValue(Class<T> type, Class elementType, T defaultValue, JsonValue jsonData)
      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(Class<T> type, JsonValue jsonData)
      Parameters:
      type - May be null if the type is unknown.
      Returns:
      May be null.
    • readValue

      public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData)
    • readValue

      public <T> T readValue(Class<T> type, Class elementType, JsonValue jsonData, Class keytype)
      Parameters:
      type - May be null if the type is unknown.
      elementType - May be null if the type is unknown.
      Returns:
      May be null.
    • copyFields

      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. The to object must have at least all the fields of the from object with the same name and type.
    • copyFields

      public void copyFields(Object from, Object to, boolean setFinals)
    • convertToString

      protected String convertToString(Object object)
    • newInstance

      protected Object newInstance(Class type)
    • prettyPrint

      public String prettyPrint(Object object)
    • prettyPrint

      public String prettyPrint(String json)
    • prettyPrint

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

      public String prettyPrint(String json, int singleLineColumns)
    • prettyPrint

      public String prettyPrint(Object object, JsonValue.PrettyPrintSettings settings)
    • prettyPrint

      public String prettyPrint(String json, JsonValue.PrettyPrintSettings settings)