Class JSONObjectUtils


  • public class JSONObjectUtils
    extends Object
    JSON object helper methods.
    Version:
    2022-08-19
    Author:
    Vladimir Dzhuvinov
    • Method Detail

      • parse

        public static Map<String,​Objectparse​(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,​Objectparse​(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.
      • 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 be null.
        key - The JSON object member key. 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 key)
                          throws ParseException
        Gets an number member of a JSON object as int.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                            throws ParseException
        Gets a number member of a JSON object as long.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                              throws ParseException
        Gets a number member of a JSON object float.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                                throws ParseException
        Gets a number member of a JSON object as double.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                                throws ParseException
        Gets a string member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                          throws ParseException
        Gets a string member of a JSON object as java.net.URI.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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<ObjectgetJSONArray​(Map<String,​Object> o,
                                                String key)
                                         throws ParseException
        Gets a JSON array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                                       throws ParseException
        Gets a string array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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 key)
                                                             throws ParseException
        Gets a JSON objects array member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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<StringgetStringList​(Map<String,​Object> o,
                                                 String key)
                                          throws ParseException
        Gets a string list member of a JSON object
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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,​ObjectgetJSONObject​(Map<String,​Object> o,
                                                             String key)
                                                      throws ParseException
        Gets a JSON object member of a JSON object.
        Parameters:
        o - The JSON object. Must not be null.
        key - The JSON object member key. 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,​ObjectnewJSONObject()
        Creates a new JSON object (unordered).
        Returns:
        The new empty JSON object.