Class JsonUtils
java.lang.Object
de.oktoflow.platform.support.json.jackson.JsonUtils
Some JSON utility methods, also reading/writing of specific types.
- Author:
- Holger Eichelberger, SSE, Lemur Project (BSD License)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classProperty exclusion modifier to simulateIgnoreProperties,JsonIgnoreProperties.static classProperty exclusion modifier to simulateInclude.static classRenames properties.static classGeneric enum deserializer.static classSpecifies the mapping of an enumeration for serialization/deserialization.static classWriter to prevent writing null properties if disabled byInclude.static classA property naming strategy exactly using the given names as JSON and Java field/getter/setter names.static classBasic annotation introspector for abstracting oktoflow data annotations, in particularConfiguredNameandIgnore.static classA handler for optional fields. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolverA type resolver for "Impl" classes in "iip.". -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionconfigureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Class<?> cls) Configures the given class for through the abstracted annotations.configureLazy(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Set<Object> ignore) Configures the given mapper for lazy serialization ignoring given classes and members.static <T> de.iip_ecosphere.platform.support.json.IOIterator<T> createIterator(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream stream, Class<T> cls) Creates an iterator over the information instreamassuming a heterogeneous collection ofcls.static com.fasterxml.jackson.databind.ObjectMapperdeclareEnums(com.fasterxml.jackson.databind.ObjectMapper mapper, de.iip_ecosphere.platform.support.json.Json.EnumMapping<?>... mappings) Declares enumerations on the specified mapper.static com.fasterxml.jackson.databind.ObjectMapperdefineFields(com.fasterxml.jackson.databind.ObjectMapper mapper, String... fieldNames) Defines a mapping of JSON names to Java field names using exactly the given names.static com.fasterxml.jackson.databind.ObjectMapperdefineOptionals(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?> cls, String... fieldNames) Defines the givenfieldNamesas optional during deserialization.exceptFields(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, String... fieldNames) Returns an object writer for a mapper that applies a filter onfieldNamesto be excluded.private static voidhandleAnnotations(String propName, AccessibleObject obj, Map<String, String> renames, Set<String> nonNullInclude) Collects data on supported annotations (ConfiguredName,invalid reference
JsonPropertyInclude) for an accessible object.static com.fasterxml.jackson.databind.ObjectMapperhandleIipDataClasses(com.fasterxml.jackson.databind.ObjectMapper mapper) Configures a Jackson object mapper for IIP conventions.private static booleanReturns whether the specified configured namecfgNamerepresents a renaming ofpropName.private static JsonUtils.OktoAnnotationIntrospectorsetAnnotationIntrospector(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Consumer<JsonUtils.OktoAnnotationIntrospector> configurer) Sets the annotation introspector onmapper.static StringTurnsobjectinto JSON usingmapper.
-
Field Details
-
IIP_TYPE_RESOLVER
public static final com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver IIP_TYPE_RESOLVERA type resolver for "Impl" classes in "iip.".
-
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
configureFor
public static JsonUtils.OktoAnnotationIntrospector configureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Class<?> cls) Configures the given class for through the abstracted annotations.- Parameters:
mapper- the mapper to be configuredintrospector- the current introspector, may be nullcls- the class to be configured- Returns:
- the configuring annotation introspector
-
handleAnnotations
private static void handleAnnotations(String propName, AccessibleObject obj, Map<String, String> renames, Set<String> nonNullInclude) Collects data on supported annotations (ConfiguredName,invalid reference
JsonPropertyInclude) for an accessible object.- Parameters:
propName- the property nameobj- the accessible objectrenames- the property renamings (original name, new name)nonNullInclude- the properties to not include if their value is null
-
isRename
Returns whether the specified configured namecfgNamerepresents a renaming ofpropName.- Parameters:
cfgName- the configured namepropName- the property name- Returns:
truefor renaming,falsefor no renaming
-
handleIipDataClasses
public static com.fasterxml.jackson.databind.ObjectMapper handleIipDataClasses(com.fasterxml.jackson.databind.ObjectMapper mapper) Configures a Jackson object mapper for IIP conventions.- Parameters:
mapper- the mapper to be configured- Returns:
mapper
-
defineOptionals
public static com.fasterxml.jackson.databind.ObjectMapper defineOptionals(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?> cls, String... fieldNames) Defines the givenfieldNamesas optional during deserialization.- Parameters:
mapper- the mapper to define the optionals oncls- the cls the classfieldNamesare member offieldNames- the field names (names of Java fields)- Returns:
mapper
-
defineFields
public static com.fasterxml.jackson.databind.ObjectMapper defineFields(com.fasterxml.jackson.databind.ObjectMapper mapper, String... fieldNames) Defines a mapping of JSON names to Java field names using exactly the given names.- Parameters:
mapper- the mapper to define the optionals onfieldNames- the field names (names of JSON/Java fields)- Returns:
mapper
-
setAnnotationIntrospector
private static JsonUtils.OktoAnnotationIntrospector setAnnotationIntrospector(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Consumer<JsonUtils.OktoAnnotationIntrospector> configurer) Sets the annotation introspector onmapper.- Parameters:
mapper- the mapper; the introspector is only set ifintrospectorwas null before, else the already set introspector may be reconfigured throughconfigurerintrospector- the actual introspector, may be nullconfigurer- the configurer function, may be null for none- Returns:
- the actual introspector (new if null before or reconfigured)
-
exceptFields
public static JsonUtils.OktoAnnotationIntrospector exceptFields(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, String... fieldNames) Returns an object writer for a mapper that applies a filter onfieldNamesto be excluded.- Parameters:
mapper- the mapperintrospector- the current introspector, may be nullfieldNames- the field names- Returns:
- the actual introspector (new if null before or reconfigured)
-
toJson
Turnsobjectinto JSON usingmapper.- Parameters:
mapper- the object mapperobject- the object to write (may be null)- Returns:
- the JSON string or an empty string in case of problems/no address
-
declareEnums
public static com.fasterxml.jackson.databind.ObjectMapper declareEnums(com.fasterxml.jackson.databind.ObjectMapper mapper, de.iip_ecosphere.platform.support.json.Json.EnumMapping<?>... mappings) Declares enumerations on the specified mapper.- Parameters:
mapper- the mappermappings- the enumeration mappings- Returns:
mapper
-
configureLazy
public static JsonUtils.OktoAnnotationIntrospector configureLazy(com.fasterxml.jackson.databind.ObjectMapper mapper, JsonUtils.OktoAnnotationIntrospector introspector, Set<Object> ignore) Configures the given mapper for lazy serialization ignoring given classes and members.- Parameters:
mapper- the mapper to configureintrospector- the current introspector, may be nullignore- , classes (also as return types) and (reflection) fields that shall be ignored- Returns:
- the actual introspector (new if null before or reconfigured)
-
createIterator
public static <T> de.iip_ecosphere.platform.support.json.IOIterator<T> createIterator(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream stream, Class<T> cls) throws IOException Creates an iterator over the information instreamassuming a heterogeneous collection ofcls.- Type Parameters:
T- the element type- Parameters:
mapper- the object mapper to usestream- the input stream to readcls- the element type- Returns:
- an iterator over the element types
- Throws:
IOException- if accessing the stream fails
-