Class 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 jsonNode
      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
      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
      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
      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
      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
      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
      static Optional<List<String>> getSimpleAttributeArray​(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
      extracts a list of simple attributes from the given json node
      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
      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.
      static boolean isEmpty​(com.fasterxml.jackson.databind.JsonNode jsonNode)
      override method for usage with wildfly 18 that still uses jackson 2.9.x
      static boolean isValidJson​(String json)
      validates if the given string structure is valid json or not
      static com.fasterxml.jackson.databind.JsonNode loadJsonDocument​(File file)
      will read a json document from a file
      static <T extends com.fasterxml.jackson.databind.node.ObjectNode>
      T
      loadJsonDocument​(File file, Class<T> type)
      will read a json document from a file
      static com.fasterxml.jackson.databind.JsonNode loadJsonDocument​(String classPathLocation)
      will read a json document from the classpath
      static <T extends com.fasterxml.jackson.databind.node.ObjectNode>
      T
      loadJsonDocument​(String classPathLocation, Class<T> type)
      will read a json document from the classpath
      static com.fasterxml.jackson.databind.JsonNode readJsonDocument​(String jsonDocument)
      will read a json document from the given string
      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
      static com.fasterxml.jackson.databind.JsonNode removeAttribute​(com.fasterxml.jackson.databind.JsonNode jsonNode, String attributeName)
      will remove an attribute from the given jsonNode
      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
      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
      static String toJsonString​(com.fasterxml.jackson.databind.JsonNode jsonNode)
      override method for usage with wildfly 18 that still uses jackson 2.9.x
      static String toPrettyJsonString​(com.fasterxml.jackson.databind.JsonNode jsonNode)
      override method for usage with wildfly 18 that still uses jackson 2.9.x
      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
    • Constructor Detail

      • JsonHelper

        public JsonHelper()
    • 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 extracted
        name - 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 extracted
        name - 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 attributes
        attributeName - 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 attributes
        attributeName - the name of the attribute that is an array with simple attributes
        type - 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 from
        name - 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 from
        name - the name of the attribute
        type - 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 removed
        attributeName - 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 removed
        value - 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 removed
        attributeName - the name of the attribute to remove
        newAttriute - 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 removed
        newAttriute - 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 removed
        attributeName - the name of the attribute to remove
        value - 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 removed
        attributeName - the name of the attribute to remove
        replaceNode - 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 object
        type - 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