- 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 [alex@erudika.com]
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <P extends ParaObject>
PfromJSON(String json)Converts a JSON string to a domain object.static Map<String,String>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 StringgetAppidFromAuthHeader(String authorization)Returns the app identifier by parsing the Authorization.static Map<String,Class<? extends ParaObject>>getCoreClassesMap()Searches through the Para core package andConfig.CORE_PACKAGE_NAMEpackage forParaObjectsubclasses and adds their names them to the map.static Map<String,String>getCoreParaTypes()Returns a map of the core data types declared in Para Core only.static Map<String,String>getCoreTypes()Returns a map of the core data types.static com.fasterxml.jackson.databind.ObjectMappergetJsonMapper()A JacksonObjectMapper.static com.fasterxml.jackson.databind.ObjectReadergetJsonReader(Class<?> type)A Jackson JSON reader.static com.fasterxml.jackson.databind.ObjectWritergetJsonWriter()A Jackson JSON writer.static com.fasterxml.jackson.databind.ObjectWritergetJsonWriterNoIdent()A Jackson JSON writer.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>toClass(String type)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 realParaObjectsubclass.static <P extends ParaObject>
StringtoJSON(P obj)Converts a domain object to JSON.static <P extends ParaObject>
PtoObject(App app, String type)Constructs a new instance of a core object.static booleantypesMatch(ParaObject so)Checks if the type of an object matches its real Class name.
-
-
-
Method Detail
-
getJsonMapper
public static com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()
A JacksonObjectMapper.- 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(com.erudika.para.core.ParaObject, java.lang.Class, boolean)
-
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(com.erudika.para.core.ParaObject, java.lang.Class, boolean)
-
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(com.erudika.para.core.ParaObject, java.lang.Class, boolean)
-
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.
IfflattenNestedObjectsToStringis 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
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(com.erudika.para.core.ParaObject, java.util.Map, java.lang.Class)
-
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
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 objecttype- 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)
Converts a class name to a real Class object.- Parameters:
type- the simple name of a class- Returns:
- the Class object or
Syspropif the class was not found. - See Also:
Class.forName(java.lang.String)
-
toClass
public static Class<? extends ParaObject> toClass(String type, Class<? extends ParaObject> defaultClass)
Converts a class name to a realParaObjectsubclass. Defaults toSyspropif 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:
Class.forName(java.lang.String),Sysprop
-
getCoreClassesMap
public static Map<String,Class<? extends ParaObject>> getCoreClassesMap()
Searches through the Para core package andConfig.CORE_PACKAGE_NAMEpackage forParaObjectsubclasses 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 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
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
-
-