Class AutoJSON

java.lang.Object
dev.drtheo.autojson.AutoJSON
All Implemented Interfaces:
DelegateLogger, Logger, SchemaHolder

public class AutoJSON extends Object implements SchemaHolder, DelegateLogger
  • Constructor Details

    • AutoJSON

      public AutoJSON()
  • Method Details

    • isBuiltIn

      public static boolean isBuiltIn(Type type)
      Returns:
      true when the type is a primitive (boxed or not) or if it's a String.
    • setupLogger

      protected Logger setupLogger()
    • setLogMisingEntries

      public void setLogMisingEntries(boolean logMisingEntries)
      Parameters:
      logMisingEntries - whether to log missing entries.
      See Also:
    • logMissingEntries

      public boolean logMissingEntries()
      Returns:
      Whether to log missing object entries. Used by BakedClassAutoSchema.
    • setSafeInstancing

      public void setSafeInstancing(boolean safeInstancing)
      Parameters:
      safeInstancing - whether to use safe instancing for all objects.
      See Also:
    • safeInstancing

      public boolean safeInstancing(Class<?> type)
      Returns:
      Whether to use safe instancing for the type.
      See Also:
    • setLayer

      public void setLayer(int layer)
      Parameters:
      layer - the new layer(s) of this AutoJSON instance.
      See Also:
    • addLayer

      public void addLayer(int layer)
      Parameters:
      layer - the layer(s) to add to this AutoJSON instance.
      See Also:
    • getLayer

      public int getLayer()
      Returns:
      the current layers of this AutoJSON instance.
      API Note:
      Read here for more info about layers.
    • defaultSchemas

      protected void defaultSchemas()
      Registers the default schemas (like UUIDSchema for UUIDs and such).
    • defaultTemplates

      protected void defaultTemplates()
      Registers the default templates (like JavaMapSchema for Maps and such).
    • shouldExclude

      public boolean shouldExclude(Field field, int layer)
      Returns:
      Whether to exclude the layer.
      API Note:
      Read here for more info about layers.
    • template

      public <T> void template(Class<? super T> type, AutoJSON.TemplateCreator<T> func)
      Registers a new template. Used for generics, when the type erasure is in action.
      Type Parameters:
      T - the type this template supports.
      Parameters:
      type - the base type.
      func - the lambda that creates a new schema based on the generic type.
      See Also:
    • schema

      public <T> Schema<T> schema(Type type, Schema<T> schema)
      Registers a new schema.
      Type Parameters:
      T - the type this schema supports.
      Parameters:
      type - the base type.
      schema - the schema used for serialization of this type.
      See Also:
    • schema

      public <T> Schema<T> schema(Type type)
      Specified by:
      schema in interface SchemaHolder
      Type Parameters:
      T - the type the schema should be able to deserialize.
      Parameters:
      type - the type for this schema. Can be a generic.
      Returns:
      the schema used for serialization of this type. Returns null if the type is a primitive or a built-in type.
      See Also:
      Implementation Note:
      By default, this method checks whether the type is a built-in, in which case it returns null. Otherwise, it checks the cache and creates a new schema if it's absent via UnsafeUtil.computeIfAbsent(Map, Object, Function) and createSchema(Type).
    • createSchema

      protected <T> Schema<T> createSchema(Type type)
    • toJson

      public <F, T> T toJson(JsonAdapter<F,T> adapter, Object obj)
      Type Parameters:
      F - the type the adapter deserializes from.
      T - the type the adapter serializes to.
      Parameters:
      adapter - the adapter used to serialize the object.
      obj - the object to serialize.
      Returns:
      the serialized object.
      Implementation Note:
      Equivalent to calling toJson(JsonAdapter, Object, Class) with the class parameter being obj.getClass().
    • toJson

      public <F, T> T toJson(JsonAdapter<F,T> adapter, Object obj, Class<?> clazz)
      Type Parameters:
      F - the type the adapter serializes from.
      T - the type the adapter serializes to.
      Parameters:
      adapter - the adapter used to serialize the object.
      obj - the object to serialize.
      clazz - the type to serialize via.
      Returns:
      the serialized object.
      Implementation Note:
      Equivalent to calling JsonAdapter.toJson(Object, Type).
    • fromJson

      public <F, T> F fromJson(JsonAdapter<F,T> adapter, T obj, Class<F> clazz)
      Type Parameters:
      F - the type the adapter deserializes to.
      T - the type the adapter deserializes from.
      Parameters:
      adapter - the adapter used to serialize the object.
      obj - the object to deserialize.
      clazz - the type to deserialize via.
      Returns:
      the deserialized object.
      Implementation Note:
      Equivalent to calling JsonAdapter.fromJson(Object, Class).
    • logger

      public Logger logger()
      Specified by:
      logger in interface DelegateLogger