Class ParaObjectUtils

java.lang.Object
com.erudika.para.core.utils.ParaObjectUtils

public final class ParaObjectUtils extends Object
Contains methods for object/grid mapping, JSON serialization, class scanning and resolution.
Author:
Alex Bogdanovski [[email protected]]
  • Method Details

    • getJsonMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()
      A Jackson ObjectMapper.
      Returns:
      JSON object mapper
    • getJsonReader

      public static com.fasterxml.jackson.databind.ObjectReader getJsonReader(Class<?> type)
      A Jackson JSON reader.
      Parameters:
      type - the type to read
      Returns:
      JSON object reader
    • getJsonWriter

      public static com.fasterxml.jackson.databind.ObjectWriter getJsonWriter()
      A Jackson JSON writer. Pretty print is on.
      Returns:
      JSON object writer
    • getJsonWriterNoIdent

      public static com.fasterxml.jackson.databind.ObjectWriter getJsonWriterNoIdent()
      A Jackson JSON writer. Pretty print is off.
      Returns:
      JSON object writer with indentation disabled
    • getCoreTypes

      public static Map<String,String> getCoreTypes()
      Returns a map of the core data types.
      Returns:
      a map of type plural - type singular form
    • getCoreParaTypes

      public static Map<String,String> getCoreParaTypes()
      Returns a map of the core data types declared in Para Core only.
      Returns:
      a map of type plural - type singular form
    • getAllTypes

      public static Map<String,String> getAllTypes(App app)
      Returns a map of all registered types.
      Parameters:
      app - the app to search for custom types
      Returns:
      a map of plural - singular form of type names
    • getAppidFromAuthHeader

      public static String getAppidFromAuthHeader(String authorization)
      Returns the app identifier by parsing the Authorization.
      Parameters:
      authorization - Authorization header
      Returns:
      appid
    • typesMatch

      public static boolean typesMatch(ParaObject so)
      Checks if the type of an object matches its real Class name.
      Parameters:
      so - an object
      Returns:
      true if the types match
    • getAnnotatedFields

      public static <P extends ParaObject> Map<String,Object> getAnnotatedFields(P pojo)
      Type Parameters:
      P - the object type
      Parameters:
      pojo - the object to convert to a map
      Returns:
      a map of fields and their values
      See Also:
    • getAnnotatedFields

      public static <P extends ParaObject> Map<String,Object> getAnnotatedFields(P pojo, Class<? extends Annotation> filter)
      Type Parameters:
      P - the object type
      Parameters:
      pojo - the object to convert to a map
      filter - a filter annotation. fields that have it will be skipped
      Returns:
      a map of fields and their values
      See Also:
    • getAnnotatedFields

      public static <P extends ParaObject> Map<String,Object> getAnnotatedFields(P pojo, boolean flattenNestedObjectsToString)
      Type Parameters:
      P - the object type
      Parameters:
      pojo - the object to convert to a map
      flattenNestedObjectsToString - flattens nested objects to a JSON string, true by default.
      Returns:
      a map of fields and their values
      See Also:
    • getAnnotatedFields

      public static <P extends ParaObject> Map<String,Object> getAnnotatedFields(P pojo, Class<? extends Annotation> filter, boolean flattenNestedObjectsToString)
      Returns a map of annotated fields of a domain object. Only annotated fields are returned. This method forms the basis of an Object/Grid Mapper. It converts an object to a map of key/value pairs. That map can later be persisted to a data store.
      If flattenNestedObjectsToString is true all field values that are objects (i.e. not primitive types or wrappers) are converted to a JSON string otherwise they are left as they are and will be serialized as regular JSON objects later (structure is preserved). Null is considered a primitive type. Transient fields and serialVersionUID are skipped.
      Type Parameters:
      P - the object type
      Parameters:
      pojo - the object to convert to a map
      filter - a filter annotation. fields that have it will be skipped
      flattenNestedObjectsToString - true if you want to flatten the nested objects to a JSON string.
      Returns:
      a map of fields and their values
    • setAnnotatedFields

      public static <P extends ParaObject> P setAnnotatedFields(Map<String,Object> data)
      Type Parameters:
      P - the object type
      Parameters:
      data - the map of fields/values
      Returns:
      the populated object
      See Also:
    • setAnnotatedFields

      public static <P extends ParaObject> P setAnnotatedFields(P pojo, Map<String,Object> data, Class<? extends Annotation> filter)
      Converts a map of fields/values to a domain object. Only annotated fields are populated. This method forms the basis of an Object/Grid Mapper.
      Map values that are JSON objects are converted to their corresponding Java types. Nulls and primitive types are preserved.
      Type Parameters:
      P - the object type
      Parameters:
      pojo - the object to populate with data
      data - the map of fields/values
      filter - a filter annotation. fields that have it will be skipped
      Returns:
      the populated object
    • toObject

      public static <P extends ParaObject> P toObject(App app, String type)
      Constructs a new instance of a core object.
      Type Parameters:
      P - the object type
      Parameters:
      app - an app object
      type - the simple name of a class
      Returns:
      a new instance of a core class. Defaults to Sysprop.
      See Also:
    • toClass

      public static Class<? extends ParaObject> toClass(String type)
      Converts a class name to a real Class object.
      Parameters:
      type - the simple name of a class
      Returns:
      the Class object or Sysprop if the class was not found.
      See Also:
    • toClass

      public static Class<? extends ParaObject> toClass(String type, Class<? extends ParaObject> defaultClass)
      Converts a class name to a real ParaObject subclass. Defaults to Sysprop if the class was not found in the core package path.
      Parameters:
      type - the simple name of a class
      defaultClass - returns this type if the requested class was not found on the classpath.
      Returns:
      the Class object. Returns null if defaultClass is null.
      See Also:
    • getCoreClassesMap

      public static Map<String,Class<? extends ParaObject>> getCoreClassesMap()
      Searches through the Para core package and Config.CORE_PACKAGE_NAME package for ParaObject subclasses and adds their names them to the map.
      Returns:
      a map of simple class names (lowercase) to class objects
    • fromJSON

      public static <P extends ParaObject> P fromJSON(String json)
      Converts a JSON string to a domain object. If we can't match the JSON to a core object, we fall back to Sysprop.
      Type Parameters:
      P - type of object to convert
      Parameters:
      json - the JSON string
      Returns:
      a core domain object or null if the string was blank
    • toJSON

      public static <P extends ParaObject> String toJSON(P obj)
      Converts a domain object to JSON.
      Type Parameters:
      P - type of object to convert
      Parameters:
      obj - a domain object
      Returns:
      the JSON representation of that object