Class JSONObjectUtils

java.lang.Object
com.nimbusds.jose.util.JSONObjectUtils

public class JSONObjectUtils extends Object
JSON object helper methods.
Version:
2024-05-10
Author:
Vladimir Dzhuvinov
  • Method Details

    • 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 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 be null.
      Returns:
      The JSON object.
      Throws:
      ParseException - If the string cannot be parsed to a valid JSON object.
    • 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 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 be null.
      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.
    • parseJSONObject

      Deprecated.
      Use parse(String) instead.
      Parameters:
      s - The JSON object string to parse. Must not be null.
      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 name) throws ParseException
      Gets a boolean member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value.
      Throws:
      ParseException - If the member is missing, the value is null or not of the expected type.
    • getInt

      public static int getInt(Map<String,Object> o, String name) throws ParseException
      Gets a number member of a JSON object as int.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value.
      Throws:
      ParseException - If the member is missing, the value is null or not of the expected type.
    • getLong

      public static long getLong(Map<String,Object> o, String name) throws ParseException
      Gets a number member of a JSON object as long.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value.
      Throws:
      ParseException - If the member is missing, the value is null or not of the expected type.
    • getFloat

      public static float getFloat(Map<String,Object> o, String name) throws ParseException
      Gets a number member of a JSON object float.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value, may be null.
      Throws:
      ParseException - If the member is missing, the value is null or not of the expected type.
    • getDouble

      public static double getDouble(Map<String,Object> o, String name) throws ParseException
      Gets a number member of a JSON object as double.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value, may be null.
      Throws:
      ParseException - If the member is missing, the value is null or not of the expected type.
    • getString

      public static String getString(Map<String,Object> o, String name) throws ParseException
      Gets a string member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a string member of a JSON object as java.net.URI.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a JSON array member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a string array member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a JSON objects array member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a string list member of a JSON object
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a JSON object member of a JSON object.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 name) throws ParseException
      Gets a string member of a JSON object as Base64URL.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      Returns:
      The JSON object member value, may be null.
      Throws:
      ParseException - If the value is not of the expected type.
    • getEpochSecondAsDate

      public static Date getEpochSecondAsDate(Map<String,Object> o, String name) throws ParseException
      Gets a number member of a JSON object as a Date expressed in seconds since the Unix epoch.
      Parameters:
      o - The JSON object. Must not be null.
      name - The JSON object member name. Must not be null.
      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 in parse(String).
      Parameters:
      o - The map. Must not be null.
      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.