Class JsonHandler

java.lang.Object
fr.gouv.vitam.common.json.JsonHandler

public final class JsonHandler extends Object
JSON handler using Json format
  • Field Details

    • JSON_SET_FOR_ACTION_DSL_REGEX

      public static final String JSON_SET_FOR_ACTION_DSL_REGEX
      See Also:
    • JSON_NODE_TYPE_REFERENCE

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

    • 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
    • createStringArrayNode

      public static com.fasterxml.jackson.databind.node.ArrayNode createStringArrayNode(String... values)
      Returns:
      a string ArrayNode filled with provided values
    • createStringArrayNode

      public static com.fasterxml.jackson.databind.node.ArrayNode createStringArrayNode(Collection<String> values)
      Returns:
      a string ArrayNode filled with provided values
    • createNullNode

      public static com.fasterxml.jackson.databind.JsonNode createNullNode()
      Returns:
      an null Node
    • getFromString

      public static com.fasterxml.jackson.databind.JsonNode getFromString(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
    • fromPojoToBytes

      public static byte[] fromPojoToBytes(Object value) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • validate

      public static void validate(String value) throws InvalidParseOperationException
      Parameters:
      value - in format String to transform
      Throws:
      InvalidParseOperationException - if parse JsonNode object exception occurred
    • createJsonGenerator

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

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

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

      public static com.fasterxml.jackson.databind.JsonNode getFromReader(Reader reader) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromInputStream

      public static com.fasterxml.jackson.databind.JsonNode getFromInputStream(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(InputStream stream1, 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(String value, 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(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(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(String value, Class<?> clasz, 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(String value, 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(File file, 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(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(File file, 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, 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
    • getFromJsonNode

      public static <T> T getFromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<?> clasz, Class<?> parameterClazz) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromJsonNodeLowerCamelCase

      public static <T> T getFromJsonNodeLowerCamelCase(com.fasterxml.jackson.databind.JsonNode jsonNode, 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
    • getFromJsonNode

      public static <T> T getFromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> clazz) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromStrictJsonNode

      public static <T> T getFromStrictJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> clazz) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromJsonNodeList

      public static <T> List<T> getFromJsonNodeList(List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, Class<T> clazz) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromJsonNodeList

      public static <T> List<T> getFromJsonNodeList(List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, com.fasterxml.jackson.core.type.TypeReference<T> clazz) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • toJsonNode

      public static com.fasterxml.jackson.databind.JsonNode toJsonNode(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 String writeAsString(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 String prettyPrint(Object object)
      Parameters:
      object - to write
      Returns:
      the Json representation of the object in Pretty Print format
    • unprettyPrint

      public static String unprettyPrint(Object object)
      Parameters:
      object - to transform
      Returns:
      the Json representation of the object in UnPretty Print format
    • writeAsFile

      public static void writeAsFile(Object object, File file) throws InvalidParseOperationException
      Parameters:
      object - to write
      file - to write object
      Throws:
      InvalidParseOperationException - if parse JsonNode object exception occurred
    • writeAsOutputStream

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

      public static InputStream writeToInpustream(Object object) throws InvalidParseOperationException
      Parameters:
      object -
      Returns:
      the InputStream for this object
      Throws:
      InvalidParseOperationException
    • checkNullOrEmpty

      public static void checkNullOrEmpty(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:
      IllegalArgumentException - if nodes are null or empty
    • checkUnicity

      public static Map.Entry<String,com.fasterxml.jackson.databind.JsonNode> checkUnicity(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 Map.Entry<String,com.fasterxml.jackson.databind.JsonNode> checkLaxUnicity(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 Map<String,Object> getMapFromString(String value) throws InvalidParseOperationException
      Parameters:
      value - to transform
      Returns:
      the corresponding HashMap
      Throws:
      InvalidParseOperationException - if parse JsonNode object exception occurred
    • getMapStringFromString

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

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

      public static <T> Map<String,T> getMapFromInputStream(InputStream inputStream, 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(InputStream inputStream, 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
    • getFromInputStreamLowerCamelCase

      public static <T> T getFromInputStreamLowerCamelCase(InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) throws InvalidParseOperationException
      Throws:
      InvalidParseOperationException
    • getFromInputStream

      public static <T> T getFromInputStream(InputStream inputStream, Class<T> clasz, 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, 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, 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, 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 String getLastFieldName(String nodePath)
    • toArrayList

      public static 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
    • isEmpty

      public static boolean isEmpty(String jsonString) throws InvalidParseOperationException
      Check json string is empty
      Parameters:
      jsonString - json string
      Throws:
      InvalidParseOperationException
    • findNode

      public static com.fasterxml.jackson.databind.JsonNode findNode(com.fasterxml.jackson.databind.JsonNode rootNode, 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, String fieldToExclude, com.fasterxml.jackson.databind.JsonNode nodeValue)
    • isTextNodeNullOrEmpty

      public static boolean isTextNodeNullOrEmpty(com.fasterxml.jackson.databind.JsonNode jsonNode)
      Tests if a jsonNode asText is Null or empty
      Parameters:
      jsonNode -
      Returns:
      true if jsonNode asText is null or empty