Package com.nimbusds.jose.util
Class JSONObjectUtils
- java.lang.Object
-
- com.nimbusds.jose.util.JSONObjectUtils
-
public class JSONObjectUtils extends Object
JSON object helper methods.- Version:
- 2022-08-19
- Author:
- Vladimir Dzhuvinov
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Base64URL
getBase64URL(Map<String,Object> o, String key)
Gets a string member of a JSON object asBase64URL
.static boolean
getBoolean(Map<String,Object> o, String key)
Gets a boolean member of a JSON object.static double
getDouble(Map<String,Object> o, String key)
Gets a number member of a JSON object asdouble
.static float
getFloat(Map<String,Object> o, String key)
Gets a number member of a JSON objectfloat
.static int
getInt(Map<String,Object> o, String key)
Gets an number member of a JSON object asint
.static List<Object>
getJSONArray(Map<String,Object> o, String key)
Gets a JSON array member of a JSON object.static Map<String,Object>
getJSONObject(Map<String,Object> o, String key)
Gets a JSON object member of a JSON object.static Map<String,Object>[]
getJSONObjectArray(Map<String,Object> o, String key)
Gets a JSON objects array member of a JSON object.static long
getLong(Map<String,Object> o, String key)
Gets a number member of a JSON object aslong
.static String
getString(Map<String,Object> o, String key)
Gets a string member of a JSON object.static String[]
getStringArray(Map<String,Object> o, String key)
Gets a string array member of a JSON object.static List<String>
getStringList(Map<String,Object> o, String key)
Gets a string list member of a JSON objectstatic URI
getURI(Map<String,Object> o, String key)
Gets a string member of a JSON object asjava.net.URI
.static Map<String,Object>
newJSONObject()
Creates a new JSON object (unordered).static Map<String,Object>
parse(String s)
Parses a JSON object.static Map<String,Object>
parse(String s, int sizeLimit)
Parses a JSON object with the option to limit the input string size.static Map<String,Object>
parseJSONObject(String s)
Deprecated.static String
toJSONString(Map<String,?> o)
Serialises the specified map to a JSON object using the entity mapping specified inparse(String)
.
-
-
-
Method Detail
-
parse
public static Map<String,Object> parse(String s) throws ParseException
Parses a JSON object.Specific JSON to Java entity mapping (as per JSON Smart):
- JSON true|false map to
java.lang.Boolean
. - JSON numbers map to
java.lang.Number
.- JSON integer numbers map to
long
. - JSON fraction numbers map to
double
.
- JSON integer numbers map to
- JSON strings map to
java.lang.String
. - JSON arrays map to
java.util.List<Object>
. - JSON objects map to
java.util.Map<String,Object>
.
- 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 valid JSON object.
- JSON true|false map to
-
parse
public static Map<String,Object> parse(String s, int sizeLimit) throws ParseException
Parses a JSON object with the option to limit the input string size.Specific JSON to Java entity mapping (as per JSON Smart):
- JSON true|false map to
java.lang.Boolean
. - JSON numbers map to
java.lang.Number
.- JSON integer numbers map to
long
. - JSON fraction numbers map to
double
.
- JSON integer numbers map to
- JSON strings map to
java.lang.String
. - JSON arrays map to
java.util.List<Object>
. - JSON objects map to
java.util.Map<String,Object>
.
- Parameters:
s
- The JSON object string to parse. Must not benull
.sizeLimit
- The max allowed size of the string to parse. A negative integer means no limit.- Returns:
- The JSON object.
- Throws:
ParseException
- If the string cannot be parsed to a valid JSON object.
- JSON true|false map to
-
parseJSONObject
@Deprecated public static Map<String,Object> 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 valid JSON object.
-
getBoolean
public static boolean getBoolean(Map<String,Object> 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 JSON object member value.
- Throws:
ParseException
- If the member is missing, the value isnull
or not of the expected type.
-
getInt
public static int getInt(Map<String,Object> 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 JSON object member value.
- Throws:
ParseException
- If the member is missing, the value isnull
or not of the expected type.
-
getLong
public static long getLong(Map<String,Object> 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 JSON object member value.
- Throws:
ParseException
- If the member is missing, the value isnull
or not of the expected type.
-
getFloat
public static float getFloat(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the member is missing, the value isnull
or not of the expected type.
-
getDouble
public static double getDouble(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the member is missing, the value isnull
or not of the expected type.
-
getString
public static String getString(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getURI
public static URI getURI(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getJSONArray
public static List<Object> getJSONArray(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getStringArray
public static String[] getStringArray(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getJSONObjectArray
public static Map<String,Object>[] getJSONObjectArray(Map<String,Object> o, String key) throws ParseException
Gets a JSON objects 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getStringList
public static List<String> getStringList(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getJSONObject
public static Map<String,Object> getJSONObject(Map<String,Object> 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 JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
getBase64URL
public static Base64URL getBase64URL(Map<String,Object> o, String key) throws ParseException
Gets a string member of a JSON object asBase64URL
.- Parameters:
o
- The JSON object. Must not benull
.key
- The JSON object member key. Must not benull
.- Returns:
- The JSON object member value, may be
null
. - Throws:
ParseException
- If the value is not of the expected type.
-
toJSONString
public static String toJSONString(Map<String,?> o)
Serialises the specified map to a JSON object using the entity mapping specified inparse(String)
.- Parameters:
o
- The map. Must not benull
.- Returns:
- The JSON object as string.
-
newJSONObject
public static Map<String,Object> newJSONObject()
Creates a new JSON object (unordered).- Returns:
- The new empty JSON object.
-
-