Class JsonUtils

java.lang.Object
de.iip_ecosphere.platform.support.json.JsonUtils

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

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Property exclusion modifier to simulate JsonIgnoreProperties.
    static class 
    Renames properties.
    static class 
    Generic enum deserializer.
    static class 
    Specifies the mapping of an enumeration for serialization/deserialization.
    static class 
    A property naming strategy exactly using the given names as JSON and Java field/getter/setter names.
    static class 
    A handler for optional fields.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver
    A type resolver for "Impl" classes in "iip.".
    private static final com.fasterxml.jackson.databind.ObjectMapper
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.databind.ObjectMapper
    configureFor(com.fasterxml.jackson.databind.ObjectMapper mapper, Class<?> cls)
    Configures the given class for through the abstracted annotations.
    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.
    static <T> IOIterator<T>
    createIterator(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream stream, Class<T> cls)
    Creates an iterator over the information in stream assuming a heterogeneous collection of cls.
    static com.fasterxml.jackson.databind.ObjectMapper
    declareEnums(com.fasterxml.jackson.databind.ObjectMapper mapper, Json.EnumMapping<?>... mappings)
    Declares enumerations on the specified mapper.
    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.
    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.
    static String
    escape(String input)
    Escapes an input string for JSON.
    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 on fieldNames to be excluded.
    static <R> R
    fromJson(Object json, Class<R> cls)
    Reads an Object from a JSON string.
    private static void
    handleAnnotations(String propName, AccessibleObject obj, Set<String> ignores, Map<String,String> renames)
    Handles supported annotations on an accessible object.
    static com.fasterxml.jackson.databind.ObjectMapper
    handleIipDataClasses(com.fasterxml.jackson.databind.ObjectMapper mapper)
    Configures a Jackson object mapper for IIP conventions.
    static String
    toJson(com.fasterxml.jackson.databind.ObjectMapper mapper, Object object)
    Turns object into JSON using mapper.
    static String
    Turns an object to JSON.
    static String
    Unescapes an input string from JSON.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • toJson

      public static String toJson(Object obj)
      Turns an object to 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

      public static <R> R fromJson(Object json, Class<R> cls)
      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:
    • escape

      public static String escape(String input)
      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

      public static String unescape(String input)
      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 configured
      cls - 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 name
      obj - the accessible object
      ignores - the properties to ignore
      renames - 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 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
    • 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 on fieldNames to be excluded.
      Parameters:
      mapper - the mapper
      fieldNames - the field names
      Returns:
      the object writer
    • 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, Json.EnumMapping<?>... mappings)
      Declares enumerations on the specified mapper.
      Parameters:
      mapper - the mapper
      mappings - 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 configure
      ignore - , 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 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