Class JSONUtil

java.lang.Object
io.strimzi.kafka.oauth.common.JSONUtil

public class JSONUtil extends Object
Helper methods to work with JSON
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.fasterxml.jackson.databind.ObjectMapper
    A Jackson Databind ObjectMapper singleton
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.databind.JsonNode
    asJson(Object value)
    Convert object to JsonNode
    static List<String>
    asListOfString(com.fasterxml.jackson.databind.JsonNode arrayOrString)
    This method takes a JsonNode representing an array, or a string, and converts it into a List of String items.
    static List<String>
    asListOfString(com.fasterxml.jackson.databind.JsonNode arrayOrString, String delimiter)
    This method takes a JsonNode representing an array, or a string, and converts it into a List of String items.
    static Set<com.fasterxml.jackson.databind.JsonNode>
    asSetOfNodes(com.fasterxml.jackson.databind.node.ArrayNode list)
    Convert a JSON array to a Set object
    static String
    getClaimFromJWT(com.fasterxml.jackson.databind.JsonNode node, String... path)
    Get specific claim from token.
    static String
    getClaimFromJWT(String claim, Object token)
    Get specific claim from token.
    static com.fasterxml.jackson.databind.node.ObjectNode
    Create a new ObjectNode
    static <T> T
    readJSON(InputStream is, Class<T> clazz)
    Parse JSON from InputStream into a specified type
    static <T> T
    readJSON(String jsonString, Class<T> clazz)
    Parse JSON from String into a specified type
    static com.fasterxml.jackson.databind.node.ArrayNode
    setArrayOfStringsIfNotNull(com.fasterxml.jackson.databind.JsonNode target, String attrName, Collection<String> elements)
    Set an array attribute on a JSON object to a collection of Strings

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAPPER

      public static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
      A Jackson Databind ObjectMapper singleton
  • Constructor Details

    • JSONUtil

      public JSONUtil()
  • Method Details

    • newObjectNode

      public static com.fasterxml.jackson.databind.node.ObjectNode newObjectNode()
      Create a new ObjectNode
      Returns:
      A new ObjectNode
    • readJSON

      public static <T> T readJSON(InputStream is, Class<T> clazz) throws IOException
      Parse JSON from InputStream into a specified type
      Type Parameters:
      T - Generic type representing a return type
      Parameters:
      is - An InputStream to read JSON from
      clazz - A class representing the type to return (e.g. JsonNode, ObjectNode, ArrayNode, String)
      Returns:
      Parsed JSON as an object
      Throws:
      IOException - If an error occurs while reading or parsing the input
    • readJSON

      public static <T> T readJSON(String jsonString, Class<T> clazz) throws IOException
      Parse JSON from String into a specified type
      Type Parameters:
      T - Generic type representing a return type
      Parameters:
      jsonString - JSON as a String
      clazz - A class representing the type to return (e.g. JsonNode, ObjectNode, ArrayNode, String)
      Returns:
      Parsed JSON as an object
      Throws:
      IOException - If an error occurs while reading or parsing the input
    • asJson

      public static com.fasterxml.jackson.databind.JsonNode asJson(Object value)
      Convert object to JsonNode
      Parameters:
      value - Json-serializable object
      Returns:
      Object as JsonNode
    • getClaimFromJWT

      public static String getClaimFromJWT(String claim, Object token)
      Get specific claim from token.
      Parameters:
      claim - jq style query where nested names are specified using '.' as separator
      token - parsed object
      Returns:
      Value of the specific claim as String or null if claim not present
    • getClaimFromJWT

      public static String getClaimFromJWT(com.fasterxml.jackson.databind.JsonNode node, String... path)
      Get specific claim from token.
      Parameters:
      node - parsed JWT token payload
      path - name segments where all but last should each point to the next nested object
      Returns:
      Value of the specific claim as String or null if claim not present
    • asListOfString

      public static List<String> asListOfString(com.fasterxml.jackson.databind.JsonNode arrayOrString)
      This method takes a JsonNode representing an array, or a string, and converts it into a List of String items.

      If the passed node is a TextNode, the text is parsed into a list of items by using ' ' (space) as a delimiter. The resulting list can contain empty strings if two delimiters are present next to one another.

      If the JsonNode is neither an ArrayNode, nor a TextNode an IllegalArgumentException is thrown.

      Parameters:
      arrayOrString - A JsonNode to convert into a list of String
      Returns:
      A list of String
    • asListOfString

      public static List<String> asListOfString(com.fasterxml.jackson.databind.JsonNode arrayOrString, String delimiter)
      This method takes a JsonNode representing an array, or a string, and converts it into a List of String items.

      The delimiter parameter is only used if the passed node is a TextNode. It is used to parse the node content as a list of strings. The resulting list can contain empty strings if two delimiters are present next to one another.

      If the JsonNode is neither an ArrayNode, nor a TextNode an IllegalArgumentException is thrown.

      Parameters:
      arrayOrString - A JsonNode to convert into a list of String
      delimiter - A delimiter to use for parsing the TextNode
      Returns:
      A list of String
    • setArrayOfStringsIfNotNull

      public static com.fasterxml.jackson.databind.node.ArrayNode setArrayOfStringsIfNotNull(com.fasterxml.jackson.databind.JsonNode target, String attrName, Collection<String> elements)
      Set an array attribute on a JSON object to a collection of Strings
      Parameters:
      target - The target JSON object
      attrName - An attribute name
      elements - The collection of strings
      Returns:
      Newly created ArrayNode
    • asSetOfNodes

      public static Set<com.fasterxml.jackson.databind.JsonNode> asSetOfNodes(com.fasterxml.jackson.databind.node.ArrayNode list)
      Convert a JSON array to a Set object
      Parameters:
      list - ArrayNode to convert
      Returns:
      Set containing the elements of the ArrayNode