java.lang.Object
de.iip_ecosphere.platform.support.json.Json
test.de.iip_ecosphere.platform.support.json.TestJson
Direct Known Subclasses:
TestJson.TestJson4All

public class TestJson extends de.iip_ecosphere.platform.support.json.Json
Implements the JSON interface by Jackson.
Author:
Holger Eichelberger, SSE
  • 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.".
    • mapper

      private com.fasterxml.jackson.databind.ObjectMapper mapper
    • writer

      private com.fasterxml.jackson.databind.ObjectWriter writer
    • introspector

  • Constructor Details

    • TestJson

      public TestJson()
  • Method Details

    • createInstanceImpl

      public de.iip_ecosphere.platform.support.json.Json createInstanceImpl(boolean considerAnnotations)
      Specified by:
      createInstanceImpl in class de.iip_ecosphere.platform.support.json.Json
    • toJson

      public String toJson(Object obj) throws IOException
      Specified by:
      toJson in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • fromJson

      public <R> R fromJson(Object json, Class<R> cls) throws IOException
      Specified by:
      fromJson in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • listFromJson

      public <R> List<R> listFromJson(Object json, Class<R> cls)
      Specified by:
      listFromJson in class de.iip_ecosphere.platform.support.json.Json
    • mapFromJson

      public <K, V> Map<K,V> mapFromJson(Object json, Class<K> keyCls, Class<K> valueCls)
      Specified by:
      mapFromJson in class de.iip_ecosphere.platform.support.json.Json
    • readValue

      public <T> T readValue(String src, Class<T> cls) throws IOException
      Specified by:
      readValue in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • readValue

      public <T> T readValue(byte[] src, Class<T> valueType) throws IOException
      Specified by:
      readValue in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • writeValueAsBytes

      public byte[] writeValueAsBytes(Object value) throws IOException
      Specified by:
      writeValueAsBytes in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • configureFor

      public static TestJson.OktoAnnotationIntrospector configureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, TestJson.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, 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
    • configureFor

      public de.iip_ecosphere.platform.support.json.Json configureFor(Class<?> cls)
      Specified by:
      configureFor in class de.iip_ecosphere.platform.support.json.Json
    • 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
    • handleIipDataClasses

      public de.iip_ecosphere.platform.support.json.Json handleIipDataClasses()
      Specified by:
      handleIipDataClasses in class de.iip_ecosphere.platform.support.json.Json
    • 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
    • defineOptionals

      public de.iip_ecosphere.platform.support.json.Json defineOptionals(Class<?> cls, String... fieldNames)
      Specified by:
      defineOptionals in class de.iip_ecosphere.platform.support.json.Json
    • 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 TestJson.OktoAnnotationIntrospector setAnnotationIntrospector(com.fasterxml.jackson.databind.ObjectMapper mapper, TestJson.OktoAnnotationIntrospector introspector, Consumer<TestJson.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)
    • defineFields

      public de.iip_ecosphere.platform.support.json.Json defineFields(String... fieldNames)
      Specified by:
      defineFields in class de.iip_ecosphere.platform.support.json.Json
    • exceptFields

      public static TestJson.OktoAnnotationIntrospector exceptFields(com.fasterxml.jackson.databind.ObjectMapper mapper, TestJson.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)
    • exceptFields

      public de.iip_ecosphere.platform.support.json.Json exceptFields(String... fieldNames)
      Specified by:
      exceptFields in class de.iip_ecosphere.platform.support.json.Json
    • configureExceptFieldsFilter

      public de.iip_ecosphere.platform.support.json.Json configureExceptFieldsFilter(String filterId, String... fieldNames)
      Specified by:
      configureExceptFieldsFilter in class de.iip_ecosphere.platform.support.json.Json
    • filterAllExceptFields

      public de.iip_ecosphere.platform.support.json.Json filterAllExceptFields(String... fieldNames)
      Specified by:
      filterAllExceptFields in class de.iip_ecosphere.platform.support.json.Json
    • convertValue

      public <T> T convertValue(Object value, Class<T> cls) throws IllegalArgumentException
      Specified by:
      convertValue in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IllegalArgumentException
    • failOnUnknownProperties

      public de.iip_ecosphere.platform.support.json.Json failOnUnknownProperties(boolean fail)
      Specified by:
      failOnUnknownProperties in class de.iip_ecosphere.platform.support.json.Json
    • createEnumMapping

      public <T> de.iip_ecosphere.platform.support.json.Json.EnumMapping<T> createEnumMapping(Class<T> type, Map<String,T> mapping)
      Specified by:
      createEnumMapping in class de.iip_ecosphere.platform.support.json.Json
    • 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
    • declareEnums

      public de.iip_ecosphere.platform.support.json.Json declareEnums(de.iip_ecosphere.platform.support.json.Json.EnumMapping<?>... mappings)
      Specified by:
      declareEnums in class de.iip_ecosphere.platform.support.json.Json
    • configureLazy

      public static TestJson.OktoAnnotationIntrospector configureLazy(com.fasterxml.jackson.databind.ObjectMapper mapper, TestJson.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)
    • configureLazy

      public de.iip_ecosphere.platform.support.json.Json configureLazy(Set<Object> ignore)
      Specified by:
      configureLazy in class de.iip_ecosphere.platform.support.json.Json
    • 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
    • createIterator

      public <T> de.iip_ecosphere.platform.support.json.IOIterator<T> createIterator(InputStream stream, Class<T> cls) throws IOException
      Specified by:
      createIterator in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • createObjectImpl

      protected de.iip_ecosphere.platform.support.json.JsonObject createObjectImpl(Reader reader) throws IOException
      Specified by:
      createObjectImpl in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • createObjectBuilderImpl

      protected de.iip_ecosphere.platform.support.json.JsonObjectBuilder createObjectBuilderImpl()
      Specified by:
      createObjectBuilderImpl in class de.iip_ecosphere.platform.support.json.Json
    • createArrayBuilderImpl

      protected de.iip_ecosphere.platform.support.json.JsonArrayBuilder createArrayBuilderImpl()
      Specified by:
      createArrayBuilderImpl in class de.iip_ecosphere.platform.support.json.Json
    • parseImpl

      protected de.iip_ecosphere.platform.support.json.JsonIterator parseImpl(String text)
      Specified by:
      parseImpl in class de.iip_ecosphere.platform.support.json.Json
    • parseImpl

      protected de.iip_ecosphere.platform.support.json.JsonIterator parseImpl(byte[] data)
      Specified by:
      parseImpl in class de.iip_ecosphere.platform.support.json.Json
    • writeValueAsString

      public String writeValueAsString(Object value) throws IOException
      Specified by:
      writeValueAsString in class de.iip_ecosphere.platform.support.json.Json
      Throws:
      IOException
    • createGeneratorImpl

      protected de.iip_ecosphere.platform.support.json.JsonGenerator createGeneratorImpl(Writer writer)
      Specified by:
      createGeneratorImpl in class de.iip_ecosphere.platform.support.json.Json