Package fr.gouv.vitam.common.json
Class JsonHandler
java.lang.Object
fr.gouv.vitam.common.json.JsonHandler
JSON handler using Json format
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.fasterxml.jackson.core.type.TypeReference<com.fasterxml.jackson.databind.JsonNode>static final String -
Method Summary
Modifier and TypeMethodDescriptioncheckLaxUnicity(String nodeName, com.fasterxml.jackson.databind.JsonNode node) node should have only one property ; simple value is allowedstatic voidcheckNullOrEmpty(String message, com.fasterxml.jackson.databind.JsonNode... nodes) Check if JsonNodes are not null and not emptycheckUnicity(String nodeName, com.fasterxml.jackson.databind.JsonNode node) node should have only one propertystatic com.fasterxml.jackson.databind.node.ArrayNodestatic com.fasterxml.jackson.core.JsonGeneratorCreates a JSON generator for low-level json stream creationstatic com.fasterxml.jackson.core.JsonParserCreate json Parserstatic com.fasterxml.jackson.databind.JsonNodestatic com.fasterxml.jackson.databind.node.ObjectNodestatic com.fasterxml.jackson.databind.node.ArrayNodecreateStringArrayNode(String... values) static com.fasterxml.jackson.databind.node.ArrayNodecreateStringArrayNode(Collection<String> values) static com.fasterxml.jackson.databind.JsonNodeFind node from the simple path separated with "."static byte[]fromPojoToBytes(Object value) static com.fasterxml.jackson.databind.node.JsonNodeFactorystatic com.fasterxml.jackson.databind.JsonNodegetFromBytes(byte[] value) static com.fasterxml.jackson.databind.JsonNodegetFromFile(File file) static <T> TgetFromFile(File file, Class<T> clasz) static <T> TgetFromFileAsTypeReference(File file, com.fasterxml.jackson.core.type.TypeReference<T> valueTypeRef) static <T> TgetFromFileLowerCamelCase(File file, Class<T> clasz) static com.fasterxml.jackson.databind.JsonNodegetFromInputStream(InputStream stream) getFromInputStream, get JsonNode from streamstatic com.fasterxml.jackson.databind.JsonNodegetFromInputStream(InputStream stream1, InputStream stream2) getFromInputStream, get merged JsonNode from streamsstatic <T> TgetFromInputStream(InputStream inputStream, Class<T> clasz) static <T> TgetFromInputStream(InputStream inputStream, Class<T> clasz, Class<?>... parameterClazz) static <T> TgetFromInputStreamAsTypeReference(InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> clasz) static <T> TgetFromInputStreamLowerCamelCase(InputStream inputStream, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) static <T> TgetFromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> clazz) static <T> TgetFromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<?> clasz, Class<?> parameterClazz) static <T> TgetFromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<T> clazz) static <T> List<T>getFromJsonNodeList(List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, com.fasterxml.jackson.core.type.TypeReference<T> clazz) static <T> List<T>getFromJsonNodeList(List<com.fasterxml.jackson.databind.JsonNode> jsonNodes, Class<T> clazz) static <T> TgetFromJsonNodeLowerCamelCase(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<T> clasz) static com.fasterxml.jackson.databind.JsonNodegetFromReader(Reader reader) static <T> TgetFromStrictJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> clazz) static com.fasterxml.jackson.databind.JsonNodegetFromString(String value) static <T> TgetFromString(String value, Class<?> clasz, Class<?> parameterClazz) static <T> TgetFromString(String value, Class<T> clasz) static <T> TgetFromStringAsTypeReference(String value, com.fasterxml.jackson.core.type.TypeReference<T> clasz) static <T> TgetFromStringLowerCamelCase(String value, Class<T> clasz) static StringgetLastFieldName(String nodePath) getMapFromInputStream(InputStream inputStream) getMapFromInputStream(InputStream inputStream, Class<T> parameterClazz) transform an inputStream into a map of template classgetMapFromString(String value) getMapStringFromString(String value) static com.fasterxml.jackson.databind.JsonNodegetNodeByPath(com.fasterxml.jackson.databind.JsonNode node, String fieldPath, boolean deepCopy) Find a node with the given pathstatic com.fasterxml.jackson.databind.JsonNodegetParentNodeByPath(com.fasterxml.jackson.databind.JsonNode node, String fieldPath, boolean deepCopy) Find a parent of the node with the given pathstatic com.fasterxml.jackson.databind.node.ArrayNodegetSubArrayNode(com.fasterxml.jackson.databind.node.ArrayNode array, int offset, int limit) From one ArrayNode, get a new ArrayNode from offset to limit itemsstatic booleanCheck json string is emptystatic booleanisNullOrEmpty(com.fasterxml.jackson.databind.JsonNode jsonNode) Tests if jsonNode is Null or emptystatic booleanisTextNodeNullOrEmpty(com.fasterxml.jackson.databind.JsonNode jsonNode) Tests if a jsonNode asText is Null or emptystatic StringprettyPrint(Object object) static voidremoveFieldFromNode(com.fasterxml.jackson.databind.JsonNode sourceNode, String fieldToExclude, com.fasterxml.jackson.databind.JsonNode nodeValue) static voidsetNodeInPath(com.fasterxml.jackson.databind.node.ObjectNode node, String nodePath, com.fasterxml.jackson.databind.JsonNode value, boolean canCreate) Set a value in a node defined by the given path.static List<com.fasterxml.jackson.databind.JsonNode>toArrayList(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) transform anArrayNode(JSON Array) to anArrayListstatic com.fasterxml.jackson.databind.JsonNodetoJsonNode(Object object) static StringunprettyPrint(Object object) static voidstatic voidwriteAsFile(Object object, File file) static voidwriteAsOutputStream(Object object, OutputStream outputStream) static StringwriteAsString(Object object) static InputStreamwriteToInpustream(Object object) static byte[]writeValueAsBytes(com.fasterxml.jackson.databind.JsonNode json) writeValueAsBytes, from Json to byte[]
-
Field Details
-
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
- 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
- Throws:
InvalidParseOperationException
-
validate
- 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 transformstream2- 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 transformclasz- 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 transformclasz- the instance of target class- Returns:
- the object of type clasz
- Throws:
InvalidParseOperationException- if parse JsonNode object exception occurredcom.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 transformclasz- the instance of target class- Returns:
- the object of type clasz
- Throws:
InvalidParseOperationException- if parse JsonNode object exception occurredcom.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 transformclasz- the instance of target classparameterClazz- 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 transformclasz- 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
- Parameters:
file- to transformclasz- 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 transformvalueTypeRef- 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 transformclasz- 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 transformclazz- 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 getclasz- 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
- Parameters:
object- to transform- Returns:
- the Json representation of the object (shall be prettyPrint)
- Throws:
InvalidParseOperationException- if parse JsonNode object exception occurred
-
prettyPrint
- Parameters:
object- to write- Returns:
- the Json representation of the object in Pretty Print format
-
unprettyPrint
- Parameters:
object- to transform- Returns:
- the Json representation of the object in UnPretty Print format
-
writeAsFile
- Parameters:
object- to writefile- 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 writeoutputStream- the output stream- Throws:
InvalidParseOperationException- if parse JsonNode object exception occurred
-
writeToInpustream
- 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 exceptionnodes- 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 errornode- 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 errornode- 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 transformparameterClazz- 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 transformclasz- 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 transformclasz- the instance of target classparameterClazz- 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 dataoffset- of array to getlimit- 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 performedfieldPath- 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 performedfieldPath- 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 rootNodenodePath- the path of the node that must be updated/createdvalue- The new value of the nodecanCreate- true if missing nodes muse be created. Else an error was thrown for missing nodes- Throws:
InvalidParseOperationException
-
getLastFieldName
-
toArrayList
public static List<com.fasterxml.jackson.databind.JsonNode> toArrayList(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) transform anArrayNode(JSON Array) to anArrayList- Parameters:
arrayNode-ArrayNodeto transform- Returns:
- list corresponding to the arrayNode in parameter
-
isEmpty
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
-