Package io.strimzi.kafka.oauth.common
Class JSONUtil
java.lang.Object
io.strimzi.kafka.oauth.common.JSONUtil
Helper methods to work with JSON
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.fasterxml.jackson.databind.ObjectMapperA Jackson DatabindObjectMappersingleton -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.JsonNodeConvert object toJsonNodeasListOfString(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.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 objectstatic StringgetClaimFromJWT(com.fasterxml.jackson.databind.JsonNode node, String... path) Get specific claim from token.static StringgetClaimFromJWT(String claim, Object token) Get specific claim from token.static com.fasterxml.jackson.databind.node.ObjectNodeCreate a new ObjectNodestatic <T> TreadJSON(InputStream is, Class<T> clazz) Parse JSON fromInputStreaminto a specified typestatic <T> TParse JSON from String into a specified typestatic com.fasterxml.jackson.databind.node.ArrayNodesetArrayOfStringsIfNotNull(com.fasterxml.jackson.databind.JsonNode target, String attrName, Collection<String> elements) Set an array attribute on a JSON object to a collection of Strings
-
Field Details
-
MAPPER
public static final com.fasterxml.jackson.databind.ObjectMapper MAPPERA Jackson DatabindObjectMappersingleton
-
-
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
Parse JSON fromInputStreaminto a specified type- Type Parameters:
T- Generic type representing a return type- Parameters:
is- AnInputStreamto read JSON fromclazz- 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
Parse JSON from String into a specified type- Type Parameters:
T- Generic type representing a return type- Parameters:
jsonString- JSON as a Stringclazz- 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
Convert object toJsonNode- Parameters:
value- Json-serializable object- Returns:
- Object as JsonNode
-
getClaimFromJWT
Get specific claim from token.- Parameters:
claim- jq style query where nested names are specified using '.' as separatortoken- parsed object- Returns:
- Value of the specific claim as String or null if claim not present
-
getClaimFromJWT
Get specific claim from token.- Parameters:
node- parsed JWT token payloadpath- 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
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
delimiterparameter 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 Stringdelimiter- 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 objectattrName- An attribute nameelements- 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
-