Class JsonUtils
java.lang.Object
de.iip_ecosphere.platform.support.json.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 simulateJsonIgnoreProperties.static classRenames properties.static classGeneric enum deserializer.static classSpecifies the mapping of an enumeration for serialization/deserialization.static classA property naming strategy exactly using the given names as JSON and Java field/getter/setter names.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.".private static final com.fasterxml.jackson.databind.ObjectMapper -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.ObjectMapperconfigureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?> cls) Configures the given class for through the abstracted annotations.static com.fasterxml.jackson.databind.ObjectMapperconfigureLazy(com.fasterxml.jackson.databind.ObjectMapper mapper, Set<Object> ignore) Configures the given mapper for lazy serialization ignoring given classes and members.static <T> 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, 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.static StringEscapes an input string for JSON.static com.fasterxml.jackson.databind.ObjectMapperexceptFields(com.fasterxml.jackson.databind.ObjectMapper mapper, String... fieldNames) Returns an object writer for a mapper that applies a filter onfieldNamesto be excluded.static <R> RReads an Object from a JSON string.private static voidhandleAnnotations(String propName, AccessibleObject obj, Set<String> ignores, Map<String, String> renames) Handles supported annotations on an accessible object.static com.fasterxml.jackson.databind.ObjectMapperhandleIipDataClasses(com.fasterxml.jackson.databind.ObjectMapper mapper) Configures a Jackson object mapper for IIP conventions.static <R> List<R> listFromJson(Object json, Class<R> cls) Reads a typed List from a JSON string.static <K,V> Map <K, V> mapFromJson(Object json, Class<K> keyCls, Class<K> valueCls) Reads a typed Map from a JSON string.static StringTurnsobjectinto JSON usingmapper.static StringTurns anobjectto JSON.static StringUnescapes an input string from JSON.
-
Field Details
-
IIP_TYPE_RESOLVER
public static final com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver IIP_TYPE_RESOLVERA type resolver for "Impl" classes in "iip.". -
MAPPER
private static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
-
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
toJson
Turns anobjectto JSON.- Parameters:
obj- the object (may be null), must have getters/setters for all attributes and a no-arg constructor no-arg constructor- Returns:
- the JSON string or an empty string in case of problems/no address
- See Also:
-
fromJson
Reads an Object from a JSON string.- Type Parameters:
R- the object type, must have getters/setters for all attributes and a no-arg constructor- Parameters:
json- the JSON value (usually a String)cls- the class of the type to read- Returns:
- the server address or null if reading fails
- See Also:
-
listFromJson
Reads a typed List from a JSON string.- Type Parameters:
R- the entity type- Parameters:
json- the JSON value (usually a String)cls- the class of the entity type to read- Returns:
- the list or null if reading fails
- See Also:
-
mapFromJson
Reads a typed Map from a JSON string.- Type Parameters:
K- the key typeV- the value type- Parameters:
json- the JSON value (usually a String)keyCls- the class of the key type to readvalueCls- the class of the value type to read- Returns:
- the map or null if reading fails
- See Also:
-
escape
Escapes an input string for JSON. Taken over from Stackoverflow and Lemur Project. The respective methods from Apache Commons Lang3 are too slow for our purpose.- Parameters:
input- the input string- Returns:
- the escaped string
-
unescape
Unescapes an input string from JSON. Taken over from Stackoverflow and Lemur Project. The respective methods from Apache Commons Lang3 are too slow for our purpose.- Parameters:
input- the input string- Returns:
- the unescaped string
-
configureFor
public static com.fasterxml.jackson.databind.ObjectMapper configureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?> cls) Configures the given class for through the abstracted annotations.- Parameters:
mapper- the mapper to be configuredcls- the class to be configured- Returns:
mapper
-
handleAnnotations
private static void handleAnnotations(String propName, AccessibleObject obj, Set<String> ignores, Map<String, String> renames) Handles supported annotations on an accessible object.- Parameters:
propName- the property nameobj- the accessible objectignores- the properties to ignorerenames- the property renamings (original name, new name)
-
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
-
exceptFields
public static com.fasterxml.jackson.databind.ObjectMapper exceptFields(com.fasterxml.jackson.databind.ObjectMapper mapper, String... fieldNames) Returns an object writer for a mapper that applies a filter onfieldNamesto be excluded.- Parameters:
mapper- the mapperfieldNames- the field names- Returns:
- the object writer
-
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, Json.EnumMapping<?>... mappings) Declares enumerations on the specified mapper.- Parameters:
mapper- the mappermappings- the enumeration mappings- Returns:
mapper
-
configureLazy
public static com.fasterxml.jackson.databind.ObjectMapper configureLazy(com.fasterxml.jackson.databind.ObjectMapper mapper, Set<Object> ignore) Configures the given mapper for lazy serialization ignoring given classes and members.- Parameters:
mapper- the mapper to configureignore- , classes (also as return types) and (reflection) fields that shall be ignored- Returns:
- the object mapper
-
createIterator
public static <T> 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
-