Class ParaObjectUtils


  • public final class ParaObjectUtils
    extends Object
    Contains methods for object/grid mapping, JSON serialization, class scanning and resolution.
    Author:
    Alex Bogdanovski [alex@erudika.com]
    • Method Detail

      • 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 ParaObjectMap<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​(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(java.lang.String)
      • 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:
        Class.forName(java.lang.String), Sysprop
      • 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 ParaObjectString 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