Class JsonHandler


  • public final class JsonHandler
    extends java.lang.Object
    JSON handler using Json format
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map.Entry<java.lang.String,​com.fasterxml.jackson.databind.JsonNode> checkLaxUnicity​(java.lang.String nodeName, com.fasterxml.jackson.databind.JsonNode node)
      node should have only one property ; simple value is allowed
      static void checkNullOrEmpty​(java.lang.String message, com.fasterxml.jackson.databind.JsonNode... nodes)
      Check if JsonNodes are not null and not empty
      static java.util.Map.Entry<java.lang.String,​com.fasterxml.jackson.databind.JsonNode> checkUnicity​(java.lang.String nodeName, com.fasterxml.jackson.databind.JsonNode node)
      node should have only one property
      static com.fasterxml.jackson.databind.node.ArrayNode createArrayNode()  
      static com.fasterxml.jackson.core.JsonGenerator createJsonGenerator​(java.io.OutputStream os)
      Creates a JSON generator for low-level json stream creation
      static com.fasterxml.jackson.core.JsonParser createJsonParser​(java.io.InputStream in)
      Create json Parser
      static com.fasterxml.jackson.databind.node.ObjectNode createObjectNode()  
      static com.fasterxml.jackson.databind.JsonNode findNode​(com.fasterxml.jackson.databind.JsonNode rootNode, java.lang.String path)
      Find node from the simple path separated with "."
      static byte[] fromPojoToBytes​(java.lang.Object value)  
      static com.fasterxml.jackson.databind.node.JsonNodeFactory getFactory()  
      static com.fasterxml.jackson.databind.JsonNode getFromBytes​(byte[] value)  
      static com.fasterxml.jackson.databind.JsonNode getFromFile​(java.io.File file)  
      static <T> T getFromFile​(java.io.File file, java.lang.Class<T> clasz)  
      static <T> T getFromFileAsTypeReference​(java.io.File file, com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef)  
      static <T> T getFromFileLowerCamelCase​(java.io.File file, java.lang.Class<T> clasz)  
      static com.fasterxml.jackson.databind.JsonNode getFromInputStream​(java.io.InputStream stream)
      getFromInputStream, get JsonNode from stream
      static com.fasterxml.jackson.databind.JsonNode getFromInputStream​(java.io.InputStream stream1, java.io.InputStream stream2)
      getFromInputStream, get merged JsonNode from streams
      static <T> T getFromInputStream​(java.io.InputStream inputStream, java.lang.Class<T> clasz)  
      static <T> T getFromInputStream​(java.io.InputStream inputStream, java.lang.Class<T> clasz, java.lang.Class<?>... parameterClazz)  
      static <T> T getFromInputStreamAsTypeReference​(java.io.InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> clasz)  
      static <T> T getFromInputStreamLowerCamelCase​(java.io.InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)  
      static <T> T getFromJsonNode​(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> clazz)  
      static <T> T getFromJsonNode​(com.fasterxml.jackson.databind.JsonNode jsonNode, java.lang.Class<?> clasz, java.lang.Class<?> parameterClazz)  
      static <T> T getFromJsonNode​(com.fasterxml.jackson.databind.JsonNode jsonNode, java.lang.Class<T> clazz)  
      static <T> java.util.List<T> getFromJsonNodeList​(java.util.List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, com.fasterxml.jackson.core.type.TypeReference<T> clazz)  
      static <T> java.util.List<T> getFromJsonNodeList​(java.util.List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, java.lang.Class<T> clazz)  
      static <T> T getFromJsonNodeLowerCamelCase​(com.fasterxml.jackson.databind.JsonNode jsonNode, java.lang.Class<T> clasz)  
      static com.fasterxml.jackson.databind.JsonNode getFromReader​(java.io.Reader reader)  
      static com.fasterxml.jackson.databind.JsonNode getFromString​(java.lang.String value)  
      static <T> T getFromString​(java.lang.String value, java.lang.Class<?> clasz, java.lang.Class<?> parameterClazz)  
      static <T> T getFromString​(java.lang.String value, java.lang.Class<T> clasz)  
      static <T> T getFromStringAsTypeReference​(java.lang.String value, com.fasterxml.jackson.core.type.TypeReference<T> clasz)  
      static <T> T getFromStringLowerCamelCase​(java.lang.String value, java.lang.Class<T> clasz)  
      static java.lang.String getLastFieldName​(java.lang.String nodePath)  
      static java.util.Map<java.lang.String,​java.lang.Object> getMapFromInputStream​(java.io.InputStream inputStream)  
      static <T> java.util.Map<java.lang.String,​T> getMapFromInputStream​(java.io.InputStream inputStream, java.lang.Class<T> parameterClazz)
      transform an inputStream into a map of template class
      static java.util.Map<java.lang.String,​java.lang.Object> getMapFromString​(java.lang.String value)  
      static java.util.Map<java.lang.String,​java.lang.String> getMapStringFromString​(java.lang.String value)  
      static com.fasterxml.jackson.databind.JsonNode getNodeByPath​(com.fasterxml.jackson.databind.JsonNode node, java.lang.String fieldPath, boolean deepCopy)
      Find a node with the given path
      static com.fasterxml.jackson.databind.JsonNode getParentNodeByPath​(com.fasterxml.jackson.databind.JsonNode node, java.lang.String fieldPath, boolean deepCopy)
      Find a parent of the node with the given path
      static com.fasterxml.jackson.databind.node.ArrayNode getSubArrayNode​(com.fasterxml.jackson.databind.node.ArrayNode array, int offset, int limit)
      From one ArrayNode, get a new ArrayNode from offset to limit items
      static boolean isEmpty​(java.lang.String jsonString)
      Check json string is empty
      static boolean isNullOrEmpty​(com.fasterxml.jackson.databind.JsonNode jsonNode)
      Tests if jsonNode is Null or empty
      static java.lang.String prettyPrint​(java.lang.Object object)  
      static void removeFieldFromNode​(com.fasterxml.jackson.databind.JsonNode sourceNode, java.lang.String fieldToExclude, com.fasterxml.jackson.databind.JsonNode nodeValue)  
      static void setNodeInPath​(com.fasterxml.jackson.databind.node.ObjectNode node, java.lang.String nodePath, com.fasterxml.jackson.databind.JsonNode value, boolean canCreate)
      Set a value in a node defined by the given path.
      static java.util.List<com.fasterxml.jackson.databind.JsonNode> toArrayList​(com.fasterxml.jackson.databind.node.ArrayNode arrayNode)
      transform an ArrayNode (JSON Array) to an ArrayList
      static com.fasterxml.jackson.databind.JsonNode toJsonNode​(java.lang.Object object)  
      static java.lang.String unprettyPrint​(java.lang.Object object)  
      static void validate​(java.lang.String value)  
      static void writeAsFile​(java.lang.Object object, java.io.File file)  
      static void writeAsOutputStream​(java.lang.Object object, java.io.OutputStream outputStream)  
      static java.lang.String writeAsString​(java.lang.Object object)  
      static java.io.InputStream writeToInpustream​(java.lang.Object object)  
      static byte[] writeValueAsBytes​(com.fasterxml.jackson.databind.JsonNode json)
      writeValueAsBytes, from Json to byte[]
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • JSON_SET_FOR_ACTION_DSL_REGEX

        public static final java.lang.String JSON_SET_FOR_ACTION_DSL_REGEX
        See Also:
        Constant Field Values
      • JSON_NODE_TYPE_REFERENCE

        public static final com.fasterxml.jackson.core.type.TypeReference<com.fasterxml.jackson.databind.JsonNode> JSON_NODE_TYPE_REFERENCE
    • Method Detail

      • getFactory

        public static com.fasterxml.jackson.databind.node.JsonNodeFactory getFactory()
        Returns:
        the current factory
      • createObjectNode

        public static com.fasterxml.jackson.databind.node.ObjectNode createObjectNode()
        Returns:
        an empty ObjectNode
      • createArrayNode

        public static com.fasterxml.jackson.databind.node.ArrayNode createArrayNode()
        Returns:
        an empty ArrayNode
      • getFromString

        public static com.fasterxml.jackson.databind.JsonNode getFromString​(java.lang.String value)
                                                                     throws InvalidParseOperationException
        Parameters:
        value - in format String to transform
        Returns:
        the jsonNode (ObjectNode or ArrayNode)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • createJsonGenerator

        public static com.fasterxml.jackson.core.JsonGenerator createJsonGenerator​(java.io.OutputStream os)
                                                                            throws java.io.IOException
        Creates a JSON generator for low-level json stream creation
        Parameters:
        os - the output stream
        Returns:
        JsonGenerator
        Throws:
        java.io.IOException - IOException
      • createJsonParser

        public static com.fasterxml.jackson.core.JsonParser createJsonParser​(java.io.InputStream in)
                                                                      throws java.io.IOException
        Create json Parser
        Parameters:
        in - the inputStream
        Returns:
        createJsonParser
        Throws:
        java.io.IOException - IOException
      • getFromFile

        public static com.fasterxml.jackson.databind.JsonNode getFromFile​(java.io.File file)
                                                                   throws InvalidParseOperationException
        Parameters:
        file - to transform
        Returns:
        the jsonNode (ObjectNode or ArrayNode)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromInputStream

        public static com.fasterxml.jackson.databind.JsonNode getFromInputStream​(java.io.InputStream stream)
                                                                          throws InvalidParseOperationException
        getFromInputStream, get JsonNode from stream
        Parameters:
        stream - to transform
        Returns:
        the jsonNode (ObjectNode or ArrayNode)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromInputStream

        public static com.fasterxml.jackson.databind.JsonNode getFromInputStream​(java.io.InputStream stream1,
                                                                                 java.io.InputStream stream2)
                                                                          throws InvalidParseOperationException
        getFromInputStream, get merged JsonNode from streams
        Parameters:
        stream1 - to transform
        stream2 - to transform and merge with
        Returns:
        the jsonNode (ObjectNode or ArrayNode)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromString

        public static <T> T getFromString​(java.lang.String value,
                                          java.lang.Class<T> clasz)
                                   throws InvalidParseOperationException
        Parameters:
        value - to transform
        clasz - the instance of target class
        Returns:
        the object of type clasz
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromInputStreamAsTypeReference

        public static <T> T getFromInputStreamAsTypeReference​(java.io.InputStream inputStream,
                                                              com.fasterxml.jackson.core.type.TypeReference<T> clasz)
                                                       throws InvalidParseOperationException,
                                                              com.fasterxml.jackson.databind.exc.InvalidFormatException
        Parameters:
        inputStream - to transform
        clasz - the instance of target class
        Returns:
        the object of type clasz
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
        com.fasterxml.jackson.databind.exc.InvalidFormatException
      • getFromStringAsTypeReference

        public static <T> T getFromStringAsTypeReference​(java.lang.String value,
                                                         com.fasterxml.jackson.core.type.TypeReference<T> clasz)
                                                  throws InvalidParseOperationException,
                                                         com.fasterxml.jackson.databind.exc.InvalidFormatException
        Parameters:
        value - to transform
        clasz - the instance of target class
        Returns:
        the object of type clasz
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
        com.fasterxml.jackson.databind.exc.InvalidFormatException
      • getFromString

        public static <T> T getFromString​(java.lang.String value,
                                          java.lang.Class<?> clasz,
                                          java.lang.Class<?> parameterClazz)
                                   throws InvalidParseOperationException
        Parameters:
        value - in format String to transform
        clasz - the instance of target class
        parameterClazz - the the target class template parameters
        Returns:
        the object of type clasz
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromStringLowerCamelCase

        public static <T> T getFromStringLowerCamelCase​(java.lang.String value,
                                                        java.lang.Class<T> clasz)
                                                 throws InvalidParseOperationException
        Parameters:
        value - to transform
        clasz - the instance of target class
        Returns:
        the object of type clasz
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromBytes

        public static com.fasterxml.jackson.databind.JsonNode getFromBytes​(byte[] value)
                                                                    throws InvalidParseOperationException
        Parameters:
        value - in format byte to transform
        Returns:
        the jsonNode (ObjectNode or ArrayNode)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromFile

        public static <T> T getFromFile​(java.io.File file,
                                        java.lang.Class<T> clasz)
                                 throws InvalidParseOperationException
        Parameters:
        file - to transform
        clasz - the instance of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromFileAsTypeReference

        public static <T> T getFromFileAsTypeReference​(java.io.File file,
                                                       com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef)
                                                throws InvalidParseOperationException
        Parameters:
        file - to transform
        valueTypeRef - the type reference of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromFileLowerCamelCase

        public static <T> T getFromFileLowerCamelCase​(java.io.File file,
                                                      java.lang.Class<T> clasz)
                                               throws InvalidParseOperationException
        Parameters:
        file - to transform
        clasz - the instance of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromJsonNode

        public static <T> T getFromJsonNode​(com.fasterxml.jackson.databind.JsonNode jsonNode,
                                            java.lang.Class<T> clazz)
                                     throws InvalidParseOperationException
        Parameters:
        jsonNode - the json object to transform
        clazz - the instance of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromJsonNodeLowerCamelCase

        public static <T> T getFromJsonNodeLowerCamelCase​(com.fasterxml.jackson.databind.JsonNode jsonNode,
                                                          java.lang.Class<T> clasz)
                                                   throws InvalidParseOperationException
        Parameters:
        jsonNode - the json object to get
        clasz - the instance of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • toJsonNode

        public static com.fasterxml.jackson.databind.JsonNode toJsonNode​(java.lang.Object object)
                                                                  throws InvalidParseOperationException
        Parameters:
        object - to transform
        Returns:
        the Json representation of the object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • writeAsString

        public static java.lang.String writeAsString​(java.lang.Object object)
                                              throws InvalidParseOperationException
        Parameters:
        object - to transform
        Returns:
        the Json representation of the object (shall be prettyPrint)
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • prettyPrint

        public static java.lang.String prettyPrint​(java.lang.Object object)
        Parameters:
        object - to write
        Returns:
        the Json representation of the object in Pretty Print format
      • unprettyPrint

        public static java.lang.String unprettyPrint​(java.lang.Object object)
        Parameters:
        object - to transform
        Returns:
        the Json representation of the object in UnPretty Print format
      • writeAsOutputStream

        public static void writeAsOutputStream​(java.lang.Object object,
                                               java.io.OutputStream outputStream)
                                        throws InvalidParseOperationException
        Parameters:
        object - to write
        outputStream - the output stream
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • checkNullOrEmpty

        public static void checkNullOrEmpty​(java.lang.String message,
                                            com.fasterxml.jackson.databind.JsonNode... nodes)
        Check if JsonNodes are not null and not empty
        Parameters:
        message - default message within exception
        nodes - to check
        Throws:
        java.lang.IllegalArgumentException - if nodes are null or empty
      • checkUnicity

        public static java.util.Map.Entry<java.lang.String,​com.fasterxml.jackson.databind.JsonNode> checkUnicity​(java.lang.String nodeName,
                                                                                                                       com.fasterxml.jackson.databind.JsonNode node)
                                                                                                                throws InvalidParseOperationException
        node should have only one property
        Parameters:
        nodeName - name to print in case of error
        node - to check
        Returns:
        the couple property name and property value
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • checkLaxUnicity

        public static java.util.Map.Entry<java.lang.String,​com.fasterxml.jackson.databind.JsonNode> checkLaxUnicity​(java.lang.String nodeName,
                                                                                                                          com.fasterxml.jackson.databind.JsonNode node)
                                                                                                                   throws InvalidParseOperationException
        node should have only one property ; simple value is allowed
        Parameters:
        nodeName - name to print in case of error
        node - to check
        Returns:
        the couple property name and property value
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getMapFromString

        public static java.util.Map<java.lang.String,​java.lang.Object> getMapFromString​(java.lang.String value)
                                                                                       throws InvalidParseOperationException
        Parameters:
        value - to transform
        Returns:
        the corresponding HashMap
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getMapStringFromString

        public static java.util.Map<java.lang.String,​java.lang.String> getMapStringFromString​(java.lang.String value)
                                                                                             throws InvalidParseOperationException
        Parameters:
        value - to transform
        Returns:
        the corresponding HashMap
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getMapFromInputStream

        public static java.util.Map<java.lang.String,​java.lang.Object> getMapFromInputStream​(java.io.InputStream inputStream)
                                                                                            throws InvalidParseOperationException
        Parameters:
        inputStream - to transform
        Returns:
        the corresponding HashMap
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getMapFromInputStream

        public static <T> java.util.Map<java.lang.String,​T> getMapFromInputStream​(java.io.InputStream inputStream,
                                                                                        java.lang.Class<T> parameterClazz)
                                                                                 throws InvalidParseOperationException
        transform an inputStream into a map of template class
        Type Parameters:
        T - the class template
        Parameters:
        inputStream - to transform
        parameterClazz - type of the value on the Map
        Returns:
        the corresponding HashMap
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromInputStream

        public static <T> T getFromInputStream​(java.io.InputStream inputStream,
                                               java.lang.Class<T> clasz)
                                        throws InvalidParseOperationException
        Parameters:
        inputStream - to transform
        clasz - the instance of target class
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getFromInputStream

        public static <T> T getFromInputStream​(java.io.InputStream inputStream,
                                               java.lang.Class<T> clasz,
                                               java.lang.Class<?>... parameterClazz)
                                        throws InvalidParseOperationException
        Parameters:
        inputStream - to transform
        clasz - the instance of target class
        parameterClazz - the the target class template parameters
        Returns:
        the corresponding object
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • getSubArrayNode

        public static com.fasterxml.jackson.databind.node.ArrayNode getSubArrayNode​(com.fasterxml.jackson.databind.node.ArrayNode array,
                                                                                    int offset,
                                                                                    int limit)
        From one ArrayNode, get a new ArrayNode from offset to limit items
        Parameters:
        array - to get node data
        offset - of array to get
        limit - of array to get
        Returns:
        Sub ArrayNode
      • getNodeByPath

        public static com.fasterxml.jackson.databind.JsonNode getNodeByPath​(com.fasterxml.jackson.databind.JsonNode node,
                                                                            java.lang.String fieldPath,
                                                                            boolean deepCopy)
        Find a node with the given path
        Parameters:
        node - the parent Node within the search must be performed
        fieldPath - the field to find in the root. use '.' to get sub-node (ex: parent.child.subNodeName)
        deepCopy - if true, the returned node is a copy of the matching node, else return the original one
        Returns:
        the find node or null if not found.
      • getParentNodeByPath

        public static com.fasterxml.jackson.databind.JsonNode getParentNodeByPath​(com.fasterxml.jackson.databind.JsonNode node,
                                                                                  java.lang.String fieldPath,
                                                                                  boolean deepCopy)
        Find a parent of the node with the given path
        Parameters:
        node - the root Node within the search must be performed
        fieldPath - the field to find in the root. use '.' to get sub-node (ex: ["parent","child","subNodeName"])
        deepCopy - if true, the returned node is a copy of the matching node, else return the original one
        Returns:
        the parent of the node defined by the given path (in the findPath example, return 'child' node)
      • setNodeInPath

        public static void setNodeInPath​(com.fasterxml.jackson.databind.node.ObjectNode node,
                                         java.lang.String nodePath,
                                         com.fasterxml.jackson.databind.JsonNode value,
                                         boolean canCreate)
                                  throws InvalidParseOperationException
        Set a value in a node defined by the given path. Create path nodes if needed
        Parameters:
        node - the rootNode
        nodePath - the path of the node that must be updated/created
        value - The new value of the node
        canCreate - true if missing nodes muse be created. Else an error was thrown for missing nodes
        Throws:
        InvalidParseOperationException
      • getLastFieldName

        public static java.lang.String getLastFieldName​(java.lang.String nodePath)
      • toArrayList

        public static java.util.List<com.fasterxml.jackson.databind.JsonNode> toArrayList​(com.fasterxml.jackson.databind.node.ArrayNode arrayNode)
        transform an ArrayNode (JSON Array) to an ArrayList
        Parameters:
        arrayNode - ArrayNode to transform
        Returns:
        list corresponding to the arrayNode in parameter
      • findNode

        public static com.fasterxml.jackson.databind.JsonNode findNode​(com.fasterxml.jackson.databind.JsonNode rootNode,
                                                                       java.lang.String path)
        Find node from the simple path separated with "."
        Parameters:
        rootNode -
        path - of node to find
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(com.fasterxml.jackson.databind.JsonNode jsonNode)
        Tests if jsonNode is Null or empty
        Parameters:
        jsonNode -
        Returns:
        true if json is null or empty
      • writeValueAsBytes

        public static byte[] writeValueAsBytes​(com.fasterxml.jackson.databind.JsonNode json)
                                        throws InvalidParseOperationException
        writeValueAsBytes, from Json to byte[]
        Parameters:
        json - to write as bytes
        Returns:
        the byte[]
        Throws:
        InvalidParseOperationException - if parse JsonNode object exception occurred
      • removeFieldFromNode

        public static void removeFieldFromNode​(com.fasterxml.jackson.databind.JsonNode sourceNode,
                                               java.lang.String fieldToExclude,
                                               com.fasterxml.jackson.databind.JsonNode nodeValue)