java.lang.Object
de.oktoflow.platform.support.json.jackson.JsonUtils

public class JsonUtils extends Object
Some JSON utility methods, also reading/writing of specific types.
Author:
Holger Eichelberger, SSE, Lemur Project (BSD License)
  • Field Details

    • IIP_TYPE_RESOLVER

      public static final com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver IIP_TYPE_RESOLVER
      A 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 configured
      introspector - the current introspector, may be null
      cls - 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
      JsonProperty
      Include) for an accessible object.
      Parameters:
      propName - the property name
      obj - the accessible object
      renames - the property renamings (original name, new name)
      nonNullInclude - the properties to not include if their value is null
    • isRename

      private static boolean isRename(String cfgName, String propName)
      Returns whether the specified configured name cfgName represents a renaming of propName.
      Parameters:
      cfgName - the configured name
      propName - the property name
      Returns:
      true for renaming, false for 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 given fieldNames as optional during deserialization.
      Parameters:
      mapper - the mapper to define the optionals on
      cls - the cls the class fieldNames are member of
      fieldNames - 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 on
      fieldNames - 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 on mapper.
      Parameters:
      mapper - the mapper; the introspector is only set if introspector was null before, else the already set introspector may be reconfigured through configurer
      introspector - the actual introspector, may be null
      configurer - 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 on fieldNames to be excluded.
      Parameters:
      mapper - the mapper
      introspector - the current introspector, may be null
      fieldNames - the field names
      Returns:
      the actual introspector (new if null before or reconfigured)
    • toJson

      public static String toJson(com.fasterxml.jackson.databind.ObjectMapper mapper, Object object)
      Turns object into JSON using mapper.
      Parameters:
      mapper - the object mapper
      object - 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 mapper
      mappings - 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 configure
      introspector - the current introspector, may be null
      ignore - , 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 in stream assuming a heterogeneous collection of cls.
      Type Parameters:
      T - the element type
      Parameters:
      mapper - the object mapper to use
      stream - the input stream to read
      cls - the element type
      Returns:
      an iterator over the element types
      Throws:
      IOException - if accessing the stream fails