Package com.nimbusds.oauth2.sdk.util
Class JSONObjectUtils
- java.lang.Object
-
- com.nimbusds.oauth2.sdk.util.JSONObjectUtils
-
public final class JSONObjectUtils extends Object
JSON object helper methods for parsing and typed retrieval of member values.
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static 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
getDouble(net.minidev.json.JSONObject o, String key)
Gets a number member of a JSON object asdouble
.static double
getDouble(net.minidev.json.JSONObject o, String key, double def)
Gets a number member of a JSON object asdouble
.static <T extends Enum<T>>
TgetEnum(net.minidev.json.JSONObject o, String key, Class<T> enumClass)
Gets a string member of a JSON object as an enumerated object.static <T extends Enum<T>>
TgetEnum(net.minidev.json.JSONObject o, String key, Class<T> enumClass, T def)
Gets a string member of a JSON object as an enumerated object.static float
getFloat(net.minidev.json.JSONObject o, String key)
Gets a number member of a JSON objectfloat
.static float
getFloat(net.minidev.json.JSONObject o, String key, float def)
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
getInt(net.minidev.json.JSONObject o, String key)
Gets an number member of a JSON object asint
.static int
getInt(net.minidev.json.JSONObject o, String key, int def)
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.static List<Object>
getList(net.minidev.json.JSONObject o, String key)
Gets a list member of a JSON object.static List<Object>
getList(net.minidev.json.JSONObject o, String key, List<Object> def)
Gets a list member of a JSON object.static long
getLong(net.minidev.json.JSONObject o, String key)
Gets a number member of a JSON object aslong
.static long
getLong(net.minidev.json.JSONObject o, String key, long def)
Gets a number member of a JSON object aslong
.static Number
getNumber(net.minidev.json.JSONObject o, String key)
Gets a number member of a JSON object asjava.lang.Number
.static Number
getNumber(net.minidev.json.JSONObject o, String key, Number def)
Gets a number member of a JSON object asjava.lang.Number
.static String
getString(net.minidev.json.JSONObject o, String key)
Gets a string member of a JSON object.static String
getString(net.minidev.json.JSONObject o, String key, String def)
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.static List<String>
getStringList(net.minidev.json.JSONObject o, String key)
Gets a string list member of a JSON object.static List<String>
getStringList(net.minidev.json.JSONObject o, String key, List<String> def)
Gets a string list member of a JSON object.static Set<String>
getStringSet(net.minidev.json.JSONObject o, String key)
Gets a string array member of a JSON object as a string set.static Set<String>
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
getURI(net.minidev.json.JSONObject o, String key)
Gets a string member of a JSON object asjava.net.URI
.static URI
getURI(net.minidev.json.JSONObject o, String key, URI def)
Gets a string member of a JSON object asjava.net.URI
.static URL
getURL(net.minidev.json.JSONObject o, String key)
Gets a string member of a JSON object asjava.net.URL
.static net.minidev.json.JSONObject
parse(String s)
Parses a JSON object.static net.minidev.json.JSONObject
parseJSONObject(String s)
Deprecated.static LinkedHashMap<String,Object>
parseKeepingOrder(String s)
Parses a JSON object while keeping the order of JSON object members.
-
-
-
Method Detail
-
containsKey
public static boolean containsKey(net.minidev.json.JSONObject jsonObject, String key)
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
public static net.minidev.json.JSONObject parse(String s) throws ParseException
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
public static LinkedHashMap<String,Object> parseKeepingOrder(String s) throws ParseException
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
public static boolean getBoolean(net.minidev.json.JSONObject o, String key) 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
.- 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
public static int getInt(net.minidev.json.JSONObject o, String key) throws ParseException
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
public static int getInt(net.minidev.json.JSONObject o, String key, int def) throws ParseException
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
public static long getLong(net.minidev.json.JSONObject o, String key) 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
.- 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
public static float getFloat(net.minidev.json.JSONObject o, String key) 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
.- 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
public static double getDouble(net.minidev.json.JSONObject o, String key) 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
.- 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
public static Number getNumber(net.minidev.json.JSONObject o, String key) 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
.- 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
public static String getString(net.minidev.json.JSONObject o, String key) 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
.- 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
public static URI getURI(net.minidev.json.JSONObject o, String key) throws ParseException
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
public static URI getURI(net.minidev.json.JSONObject o, String key, URI def) throws ParseException
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
public static URL getURL(net.minidev.json.JSONObject o, String key) throws ParseException
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.
-
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
public static List<Object> getList(net.minidev.json.JSONObject o, String key) 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
.- 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.
-
-