Package com.nimbusds.oauth2.sdk.util
Class JSONObjectUtils
java.lang.Object
com.nimbusds.oauth2.sdk.util.JSONObjectUtils
JSON object helper methods for parsing and typed retrieval of member values.
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
containsKey
(net.minidev.json.JSONObject jsonObject, String key) Returnstrue
if the JSON object is defined and contains the specified key.static boolean
getBoolean
(net.minidev.json.JSONObject o, String key) Gets a boolean member of a JSON object.static boolean
getBoolean
(net.minidev.json.JSONObject o, String key, boolean def) Gets a boolean member of a JSON object.static double
Gets a number member of a JSON object asdouble
.static double
Gets a number member of a JSON object asdouble
.static <T extends Enum<T>>
TGets a string member of a JSON object as an enumerated object.static <T extends Enum<T>>
TGets a string member of a JSON object as an enumerated object.static float
Gets a number member of a JSON objectfloat
.static float
Gets a number member of a JSON objectfloat
.static <T> T
getGeneric
(net.minidev.json.JSONObject o, String key, Class<T> clazz) Gets a generic member of a JSON object.static int
Gets an number member of a JSON object asint
.static int
Gets an number member of a JSON object asint
.static net.minidev.json.JSONArray
getJSONArray
(net.minidev.json.JSONObject o, String key) Gets a JSON array member of a JSON object.static net.minidev.json.JSONArray
getJSONArray
(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONArray def) Gets a JSON array member of a JSON object.static net.minidev.json.JSONObject
getJSONObject
(net.minidev.json.JSONObject o, String key) Gets a JSON object member of a JSON object.static net.minidev.json.JSONObject
getJSONObject
(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONObject def) Gets a JSON object member of a JSON object.Gets a list member of a JSON object.Gets a list member of a JSON object.static long
Gets a number member of a JSON object aslong
.static long
Gets a number member of a JSON object aslong
.static Number
Gets a number member of a JSON object asjava.lang.Number
.static Number
Gets a number member of a JSON object asjava.lang.Number
.static String
Gets a string member of a JSON object.static String
Gets a string member of a JSON object.static String[]
getStringArray
(net.minidev.json.JSONObject o, String key) Gets a string array member of a JSON object.static String[]
getStringArray
(net.minidev.json.JSONObject o, String key, String[] def) Gets a string array member of a JSON object.getStringList
(net.minidev.json.JSONObject o, String key) Gets a string list member of a JSON object.getStringList
(net.minidev.json.JSONObject o, String key, List<String> def) Gets a string list member of a JSON object.getStringSet
(net.minidev.json.JSONObject o, String key) Gets a string array member of a JSON object as a string set.getStringSet
(net.minidev.json.JSONObject o, String key, Set<String> def) Gets a string array member of a JSON object as a string set.static URI
Gets a string member of a JSON object asjava.net.URI
.static URI
Gets a string member of a JSON object asjava.net.URI
.static URL
Gets a string member of a JSON object asjava.net.URL
.static URL
Gets a string member of a JSON object asjava.net.URL
.static net.minidev.json.JSONObject
Parses a JSON object.static net.minidev.json.JSONObject
Deprecated.static LinkedHashMap<String,
Object> Parses a JSON object while keeping the order of JSON object members.static net.minidev.json.JSONObject
toJSONObject
(com.nimbusds.jose.jwk.JWKSet jwkSet) Returns the JSON object representation of the specified JWK set.static net.minidev.json.JSONObject
toJSONObject
(com.nimbusds.jwt.JWTClaimsSet jwtClaimsSet) Returns the JSON object representation of the specified JWT claims set.
-
Method Details
-
containsKey
Returnstrue
if the JSON object is defined and contains the specified key.- Parameters:
jsonObject
- The JSON object to check. May benull
.key
- The key to check. Must not benull
.- Returns:
true
if the JSON object is defined and contains the specified key, elsefalse
.
-
parse
Parses a JSON object.Specific JSON to Java entity mapping (as per JSON Simple):
- JSON numbers mapped to
java.lang.Number
. - JSON integer numbers mapped to
long
. - JSON fraction numbers mapped to
double
.
- Parameters:
s
- The JSON object string to parse. Must not benull
.- Returns:
- The JSON object.
- Throws:
ParseException
- If the string cannot be parsed to a JSON object.
- JSON numbers mapped to
-
parseKeepingOrder
Parses a JSON object while keeping the order of JSON object members.Specific JSON to Java entity mapping (as per JSON Simple):
- JSON numbers mapped to
java.lang.Number
. - JSON integer numbers mapped to
long
. - JSON fraction numbers mapped to
double
.
- Parameters:
s
- The JSON object string to parse. Must not benull
.- Returns:
- The JSON object as linked hash map.
- Throws:
ParseException
- If the string cannot be parsed to a JSON object.
- JSON numbers mapped to
-
parseJSONObject
@Deprecated public static net.minidev.json.JSONObject parseJSONObject(String s) throws ParseException Deprecated.Useparse(String)
instead.- Parameters:
s
- The JSON object string to parse. Must not benull
.- Returns:
- The JSON object.
- Throws:
ParseException
- If the string cannot be parsed to a JSON object.
-
getGeneric
public static <T> T getGeneric(net.minidev.json.JSONObject o, String key, Class<T> clazz) throws ParseException Gets a generic member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.clazz
- The expected class of the JSON object member value. Must not benull
.- Returns:
- The JSON object member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getBoolean
Gets a boolean member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getBoolean
public static boolean getBoolean(net.minidev.json.JSONObject o, String key, boolean def) throws ParseException Gets a boolean member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or. the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getInt
Gets an number member of a JSON object asint
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getInt
Gets an number member of a JSON object asint
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getLong
Gets a number member of a JSON object aslong
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getLong
public static long getLong(net.minidev.json.JSONObject o, String key, long def) throws ParseException Gets a number member of a JSON object aslong
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getFloat
Gets a number member of a JSON objectfloat
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getFloat
public static float getFloat(net.minidev.json.JSONObject o, String key, float def) throws ParseException Gets a number member of a JSON objectfloat
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getDouble
Gets a number member of a JSON object asdouble
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getDouble
public static double getDouble(net.minidev.json.JSONObject o, String key, double def) throws ParseException Gets a number member of a JSON object asdouble
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getNumber
Gets a number member of a JSON object asjava.lang.Number
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getNumber
public static Number getNumber(net.minidev.json.JSONObject o, String key, Number def) throws ParseException Gets a number member of a JSON object asjava.lang.Number
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getString
Gets a string member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getString
public static String getString(net.minidev.json.JSONObject o, String key, String def) throws ParseException Gets a string member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getEnum
public static <T extends Enum<T>> T getEnum(net.minidev.json.JSONObject o, String key, Class<T> enumClass) throws ParseException Gets a string member of a JSON object as an enumerated object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.enumClass
- The enumeration class. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getEnum
public static <T extends Enum<T>> T getEnum(net.minidev.json.JSONObject o, String key, Class<T> enumClass, T def) throws ParseException Gets a string member of a JSON object as an enumerated object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.enumClass
- The enumeration class. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getURI
Gets a string member of a JSON object asjava.net.URI
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getURI
Gets a string member of a JSON object asjava.net.URI
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getURL
Gets a string member of a JSON object asjava.net.URL
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getURL
Gets a string member of a JSON object asjava.net.URL
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getJSONArray
public static net.minidev.json.JSONArray getJSONArray(net.minidev.json.JSONObject o, String key) throws ParseException Gets a JSON array member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getJSONArray
public static net.minidev.json.JSONArray getJSONArray(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONArray def) throws ParseException Gets a JSON array member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getList
Gets a list member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getList
public static List<Object> getList(net.minidev.json.JSONObject o, String key, List<Object> def) throws ParseException Gets a list member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getStringArray
public static String[] getStringArray(net.minidev.json.JSONObject o, String key) throws ParseException Gets a string array member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getStringArray
public static String[] getStringArray(net.minidev.json.JSONObject o, String key, String[] def) throws ParseException Gets a string array member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getStringList
public static List<String> getStringList(net.minidev.json.JSONObject o, String key) throws ParseException Gets a string list member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getStringList
public static List<String> getStringList(net.minidev.json.JSONObject o, String key, List<String> def) throws ParseException Gets a string list member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getStringSet
public static Set<String> getStringSet(net.minidev.json.JSONObject o, String key) throws ParseException Gets a string array member of a JSON object as a string set.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getStringSet
public static Set<String> getStringSet(net.minidev.json.JSONObject o, String key, Set<String> def) throws ParseException Gets a string array member of a JSON object as a string set.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
getJSONObject
public static net.minidev.json.JSONObject getJSONObject(net.minidev.json.JSONObject o, String key) throws ParseException Gets a JSON object member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is missing,null
or not of the expected type.
-
getJSONObject
public static net.minidev.json.JSONObject getJSONObject(net.minidev.json.JSONObject o, String key, net.minidev.json.JSONObject def) throws ParseException Gets a JSON object member of a JSON object.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.def
- The default value to return if the key is not present or the value isnull
. May benull
.- Returns:
- The member value.
- Throws:
ParseException
- If the value is not of the expected type.
-
toJSONObject
Returns the JSON object representation of the specified JWT claims set.- Parameters:
jwtClaimsSet
- The JWT claims set,null
if not specified.- Returns:
- The JSON object,
null
if not specified.
-
toJSONObject
Returns the JSON object representation of the specified JWK set.- Parameters:
jwkSet
- The JWK set,null
if not specified.- Returns:
- The JSON object,
null
if not specified.
-