Class JsonHelper
- java.lang.Object
-
- de.captaingoldfish.scim.sdk.common.utils.JsonHelper
-
public final class JsonHelper extends Object
author Pascal Knueppel
created at: 28.09.2019 - 00:05
-
-
Constructor Summary
Constructors Constructor Description JsonHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static com.fasterxml.jackson.databind.JsonNode
addAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, com.fasterxml.jackson.databind.JsonNode newAttriute)
will remove an attribute from the given jsonNodestatic com.fasterxml.jackson.databind.JsonNode
addAttributeToArray(com.fasterxml.jackson.databind.JsonNode jsonArray, com.fasterxml.jackson.databind.JsonNode newAttriute)
will remove an attribute from the given jsonNodestatic <T extends com.fasterxml.jackson.databind.node.ObjectNode>
TcopyResourceToObject(com.fasterxml.jackson.databind.JsonNode resource, Class<T> type)
creates a new instance of the given type and moves the content from the resource into the new nodestatic Optional<com.fasterxml.jackson.databind.node.ArrayNode>
getArrayAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
tries to get an array from the given json nodestatic Optional<com.fasterxml.jackson.databind.node.ObjectNode>
getObjectAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
tries to get an json object from the given json nodestatic Optional<String>
getSimpleAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
will get a string attribute with the given name from the given json nodestatic <T> Optional<T>
getSimpleAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name, Class<T> type)
will get a string attribute with the given name from the given json nodestatic Optional<List<String>>
getSimpleAttributeArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
extracts a list of simple attributes from the given json nodestatic <T> Optional<List<T>>
getSimpleAttributeArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, Class<T> type)
extracts a list of simple attributes from the given json nodestatic Optional<com.fasterxml.jackson.databind.JsonNode>
getSimpleAttributeByName(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
will extract a scim attribute by its scim-name.static boolean
isEmpty(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.xstatic boolean
isValidJson(String json)
validates if the given string structure is valid json or notstatic com.fasterxml.jackson.databind.JsonNode
loadJsonDocument(File file)
will read a json document from a filestatic <T extends com.fasterxml.jackson.databind.node.ObjectNode>
TloadJsonDocument(File file, Class<T> type)
will read a json document from a filestatic com.fasterxml.jackson.databind.JsonNode
loadJsonDocument(String classPathLocation)
will read a json document from the classpathstatic <T extends com.fasterxml.jackson.databind.node.ObjectNode>
TloadJsonDocument(String classPathLocation, Class<T> type)
will read a json document from the classpathstatic com.fasterxml.jackson.databind.JsonNode
readJsonDocument(String jsonDocument)
will read a json document from the given stringstatic <T extends com.fasterxml.jackson.databind.node.ObjectNode>
TreadJsonDocument(String jsonDocument, Class<T> type)
will read a json document from the given stringstatic com.fasterxml.jackson.databind.JsonNode
removeAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
will remove an attribute from the given jsonNodestatic com.fasterxml.jackson.databind.JsonNode
removeSimpleAttributeFromArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String value)
will remove a simple value from a simple array node in the given json documentstatic com.fasterxml.jackson.databind.JsonNode
replaceNode(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, com.fasterxml.jackson.databind.JsonNode replaceNode)
will remove an attribute from the given jsonNodestatic String
toJsonString(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.xstatic String
toPrettyJsonString(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.xstatic <T> com.fasterxml.jackson.databind.JsonNode
writeValue(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, T value)
will remove an attribute from the given jsonNode
-
-
-
Method Detail
-
loadJsonDocument
public static com.fasterxml.jackson.databind.JsonNode loadJsonDocument(String classPathLocation)
will read a json document from the classpath- Parameters:
classPathLocation
- the location of the document- Returns:
- the parsed json document
-
loadJsonDocument
public static <T extends com.fasterxml.jackson.databind.node.ObjectNode> T loadJsonDocument(String classPathLocation, Class<T> type)
will read a json document from the classpath- Parameters:
classPathLocation
- the location of the document- Returns:
- the parsed json document
-
loadJsonDocument
public static <T extends com.fasterxml.jackson.databind.node.ObjectNode> T loadJsonDocument(File file, Class<T> type)
will read a json document from a file- Parameters:
file
- the location of the document- Returns:
- the parsed json document
-
loadJsonDocument
public static com.fasterxml.jackson.databind.JsonNode loadJsonDocument(File file)
will read a json document from a file- Parameters:
file
- the location of the document- Returns:
- the parsed json document
-
readJsonDocument
public static com.fasterxml.jackson.databind.JsonNode readJsonDocument(String jsonDocument)
will read a json document from the given string- Parameters:
jsonDocument
- the direct json representation- Returns:
- the parsed json document
-
readJsonDocument
public static <T extends com.fasterxml.jackson.databind.node.ObjectNode> T readJsonDocument(String jsonDocument, Class<T> type)
will read a json document from the given string- Parameters:
jsonDocument
- the direct json representation- Returns:
- the parsed json document
-
getArrayAttribute
public static Optional<com.fasterxml.jackson.databind.node.ArrayNode> getArrayAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
tries to get an array from the given json node- Parameters:
jsonNode
- the json node from which the array should be extractedname
- the name of the json array attribute- Returns:
- the json array attribute or an empty if the attribute is not present
-
getObjectAttribute
public static Optional<com.fasterxml.jackson.databind.node.ObjectNode> getObjectAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
tries to get an json object from the given json node- Parameters:
jsonNode
- the json node from which the json object should be extractedname
- the name of the json object attribute- Returns:
- the json object attribute or an empty if the attribute is not present
-
getSimpleAttributeArray
public static Optional<List<String>> getSimpleAttributeArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
extracts a list of simple attributes from the given json node- Parameters:
jsonNode
- the json document containing an array with simple attributesattributeName
- the name of the attribute that is an array with simple attributes- Returns:
- a list of attributes or an empty
-
getSimpleAttributeArray
public static <T> Optional<List<T>> getSimpleAttributeArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, Class<T> type)
extracts a list of simple attributes from the given json node- Parameters:
jsonNode
- the json document containing an array with simple attributesattributeName
- the name of the attribute that is an array with simple attributestype
- the type of the values within the array- Returns:
- a list of attributes or an empty
-
getSimpleAttribute
public static Optional<String> getSimpleAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name)
will get a string attribute with the given name from the given json node- Parameters:
jsonNode
- the json node to get the attribute fromname
- the name of the attribute- Returns:
- the value as string or an empty
-
getSimpleAttribute
public static <T> Optional<T> getSimpleAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String name, Class<T> type)
will get a string attribute with the given name from the given json node- Parameters:
jsonNode
- the json node to get the attribute fromname
- the name of the attributetype
- the type of the attribute to return- Returns:
- the value of the given type or an empty
-
removeAttribute
public static com.fasterxml.jackson.databind.JsonNode removeAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
will remove an attribute from the given jsonNode- Parameters:
jsonNode
- the json node from which the attribute should be removedattributeName
- the name of the attribute to remove
-
removeSimpleAttributeFromArray
public static com.fasterxml.jackson.databind.JsonNode removeSimpleAttributeFromArray(com.fasterxml.jackson.databind.JsonNode jsonNode, String value)
will remove a simple value from a simple array node in the given json document- Parameters:
jsonNode
- the array from which the value should be removedvalue
- the value that should be removed from the document
-
addAttribute
public static com.fasterxml.jackson.databind.JsonNode addAttribute(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, com.fasterxml.jackson.databind.JsonNode newAttriute)
will remove an attribute from the given jsonNode- Parameters:
jsonNode
- the json node from which the attribute should be removedattributeName
- the name of the attribute to removenewAttriute
- the new attribute that should be added
-
addAttributeToArray
public static com.fasterxml.jackson.databind.JsonNode addAttributeToArray(com.fasterxml.jackson.databind.JsonNode jsonArray, com.fasterxml.jackson.databind.JsonNode newAttriute)
will remove an attribute from the given jsonNode- Parameters:
jsonArray
- the json node from which the attribute should be removednewAttriute
- the new attribute that should be added
-
writeValue
public static <T> com.fasterxml.jackson.databind.JsonNode writeValue(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, T value)
will remove an attribute from the given jsonNode- Parameters:
jsonNode
- the json node from which the attribute should be removedattributeName
- the name of the attribute to removevalue
- the value of the new replaced node
-
replaceNode
public static com.fasterxml.jackson.databind.JsonNode replaceNode(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName, com.fasterxml.jackson.databind.JsonNode replaceNode)
will remove an attribute from the given jsonNode- Parameters:
jsonNode
- the json node from which the attribute should be removedattributeName
- the name of the attribute to removereplaceNode
- the new node that should be used as replacement
-
copyResourceToObject
public static <T extends com.fasterxml.jackson.databind.node.ObjectNode> T copyResourceToObject(com.fasterxml.jackson.databind.JsonNode resource, Class<T> type)
creates a new instance of the given type and moves the content from the resource into the new node- Parameters:
resource
- the resource that holds the content that must be moved to the new objecttype
- the type from which an instance will be created with a noArgs constructor- Returns:
- a newly created instance with the content of the
resource
-node
-
getSimpleAttributeByName
public static Optional<com.fasterxml.jackson.databind.JsonNode> getSimpleAttributeByName(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
will extract a scim attribute by its scim-name.- Parameters:
attributeName
- the scim name of the attribute e.g. "userName" of "name.givenName"- Returns:
- the json node or an empty
-
isValidJson
public static boolean isValidJson(String json)
validates if the given string structure is valid json or not- Parameters:
json
- the string to validate- Returns:
- true if the given string is a valid json structure, false else
-
toJsonString
public static String toJsonString(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.x
-
toPrettyJsonString
public static String toPrettyJsonString(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.x
-
isEmpty
public static boolean isEmpty(com.fasterxml.jackson.databind.JsonNode jsonNode)
override method for usage with wildfly 18 that still uses jackson 2.9.x
-
-