java.lang.Object
com.erudika.para.core.utils.ParaObjectUtils
Contains methods for object/grid mapping, JSON serialization, class scanning and resolution.
- Author:
- Alex Bogdanovski [[email protected]]
-
Method Summary
Modifier and TypeMethodDescriptionstatic <P extends ParaObject>
PConverts a JSON string to a domain object.getAllTypes
(App app) Returns a map of all registered types.static <P extends ParaObject>
Map<String, Object> getAnnotatedFields
(P pojo) static <P extends ParaObject>
Map<String, Object> getAnnotatedFields
(P pojo, boolean flattenNestedObjectsToString) static <P extends ParaObject>
Map<String, Object> getAnnotatedFields
(P pojo, Class<? extends Annotation> filter) 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.static String
getAppidFromAuthHeader
(String authorization) Returns the app identifier by parsing the Authorization.static Map
<String, Class<? extends ParaObject>> Searches through the Para core package andConfig.CORE_PACKAGE_NAME
package forParaObject
subclasses and adds their names them to the map.Returns a map of the core data types declared in Para Core only.Returns a map of the core data types.static com.fasterxml.jackson.databind.ObjectMapper
A JacksonObjectMapper
.static com.fasterxml.jackson.databind.ObjectReader
getJsonReader
(Class<?> type) A Jackson JSON reader.static com.fasterxml.jackson.databind.ObjectWriter
A Jackson JSON writer.static com.fasterxml.jackson.databind.ObjectWriter
A Jackson JSON writer.static void
registerCoreClasses
(Class<? extends ParaObject>... classes) Explicitly registers core classes for reflection.static <P extends ParaObject>
PsetAnnotatedFields
(Map<String, Object> data) static <P extends ParaObject>
PsetAnnotatedFields
(P pojo, Map<String, Object> data, Class<? extends Annotation> filter) Converts a map of fields/values to a domain object.static Class
<? extends ParaObject> Converts a class name to a real Class object.static Class
<? extends ParaObject> toClass
(String type, Class<? extends ParaObject> defaultClass) Converts a class name to a realParaObject
subclass.static <P extends ParaObject>
StringtoJSON
(P obj) Converts a domain object to JSON.static <P extends ParaObject>
PConstructs a new instance of a core object.static boolean
typesMatch
(ParaObject so) Checks if the type of an object matches its real Class name.
-
Method Details
-
getJsonMapper
public static com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()A JacksonObjectMapper
.- Returns:
- JSON object mapper
-
getJsonReader
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
-
getCoreParaTypes
-
getAllTypes
-
getAppidFromAuthHeader
-
typesMatch
Checks if the type of an object matches its real Class name.- Parameters:
so
- an object- Returns:
- true if the types match
-
getAnnotatedFields
- 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 mapfilter
- 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 mapflattenNestedObjectsToString
- 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.
IfflattenNestedObjectsToString
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 mapfilter
- a filter annotation. fields that have it will be skippedflattenNestedObjectsToString
- true if you want to flatten the nested objects to a JSON string.- Returns:
- a map of fields and their values
-
setAnnotatedFields
- 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 datadata
- the map of fields/valuesfilter
- a filter annotation. fields that have it will be skipped- Returns:
- the populated object
-
toObject
Constructs a new instance of a core object.- Type Parameters:
P
- the object type- Parameters:
app
- an app objecttype
- the simple name of a class- Returns:
- a new instance of a core class. Defaults to
Sysprop
. - See Also:
-
toClass
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 realParaObject
subclass. Defaults toSysprop
if the class was not found in the core package path.- Parameters:
type
- the simple name of a classdefaultClass
- 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
Searches through the Para core package andConfig.CORE_PACKAGE_NAME
package forParaObject
subclasses and adds their names them to the map.- Returns:
- a map of simple class names (lowercase) to class objects
-
registerCoreClasses
Explicitly registers core classes for reflection.- Parameters:
classes
- a list of classes
-
fromJSON
Converts a JSON string to a domain object. If we can't match the JSON to a core object, we fall back toSysprop
.- 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
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
-