Class JSONObjectUtils


  • public class JSONObjectUtils
    extends java.lang.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​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a string member of a JSON object as Base64URL.
      static boolean getBoolean​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a boolean member of a JSON object.
      static double getDouble​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a number member of a JSON object as double.
      static float getFloat​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a number member of a JSON object float.
      static int getInt​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets an number member of a JSON object as int.
      static java.util.List<java.lang.Object> getJSONArray​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a JSON array member of a JSON object.
      static java.util.Map<java.lang.String,​java.lang.Object> getJSONObject​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a JSON object member of a JSON object.
      static java.util.Map<java.lang.String,​java.lang.Object>[] getJSONObjectArray​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a JSON objects array member of a JSON object.
      static long getLong​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a number member of a JSON object as long.
      static java.lang.String getString​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a string member of a JSON object.
      static java.lang.String[] getStringArray​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a string array member of a JSON object.
      static java.util.List<java.lang.String> getStringList​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a string list member of a JSON object
      static java.net.URI getURI​(java.util.Map<java.lang.String,​java.lang.Object> o, java.lang.String key)
      Gets a string member of a JSON object as java.net.URI.
      static java.util.Map<java.lang.String,​java.lang.Object> newJSONObject()
      Creates a new JSON object (unordered).
      static java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String s)
      Parses a JSON object.
      static java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String s, int sizeLimit)
      Parses a JSON object with the option to limit the input string size.
      static java.util.Map<java.lang.String,​java.lang.Object> parseJSONObject​(java.lang.String s)
      Deprecated.
      static java.lang.String toJSONString​(java.util.Map<java.lang.String,​?> o)
      Serialises the specified map to a JSON object using the entity mapping specified in parse(String).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • parse

        public static java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String s)
                                                                            throws java.text.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:
        java.text.ParseException - If the string cannot be parsed to a valid JSON object.
      • parse

        public static java.util.Map<java.lang.String,​java.lang.Object> parse​(java.lang.String s,
                                                                                   int sizeLimit)
                                                                            throws java.text.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:
        java.text.ParseException - If the string cannot be parsed to a valid JSON object.
      • parseJSONObject

        @Deprecated
        public static java.util.Map<java.lang.String,​java.lang.Object> parseJSONObject​(java.lang.String s)
                                                                                      throws java.text.ParseException
        Deprecated.
        Use parse(String) instead.
        Parameters:
        s - The JSON object string to parse. Must not be null.
        Returns:
        The JSON object.
        Throws:
        java.text.ParseException - If the string cannot be parsed to a valid JSON object.
      • getBoolean

        public static boolean getBoolean​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                         java.lang.String key)
                                  throws java.text.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:
        java.text.ParseException - If the member is missing, the value is null or not of the expected type.
      • getInt

        public static int getInt​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                 java.lang.String key)
                          throws java.text.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:
        java.text.ParseException - If the member is missing, the value is null or not of the expected type.
      • getLong

        public static long getLong​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                   java.lang.String key)
                            throws java.text.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:
        java.text.ParseException - If the member is missing, the value is null or not of the expected type.
      • getFloat

        public static float getFloat​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                     java.lang.String key)
                              throws java.text.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:
        java.text.ParseException - If the member is missing, the value is null or not of the expected type.
      • getDouble

        public static double getDouble​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                       java.lang.String key)
                                throws java.text.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:
        java.text.ParseException - If the member is missing, the value is null or not of the expected type.
      • getString

        public static java.lang.String getString​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                 java.lang.String key)
                                          throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getURI

        public static java.net.URI getURI​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                          java.lang.String key)
                                   throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getJSONArray

        public static java.util.List<java.lang.Object> getJSONArray​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                                    java.lang.String key)
                                                             throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getStringArray

        public static java.lang.String[] getStringArray​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                        java.lang.String key)
                                                 throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getJSONObjectArray

        public static java.util.Map<java.lang.String,​java.lang.Object>[] getJSONObjectArray​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                                                                  java.lang.String key)
                                                                                           throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getStringList

        public static java.util.List<java.lang.String> getStringList​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                                     java.lang.String key)
                                                              throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getJSONObject

        public static java.util.Map<java.lang.String,​java.lang.Object> getJSONObject​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                                                                           java.lang.String key)
                                                                                    throws java.text.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:
        java.text.ParseException - If the value is not of the expected type.
      • getBase64URL

        public static Base64URL getBase64URL​(java.util.Map<java.lang.String,​java.lang.Object> o,
                                             java.lang.String key)
                                      throws java.text.ParseException
        Gets a string member of a JSON object as Base64URL.
        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:
        java.text.ParseException - If the value is not of the expected type.
      • toJSONString

        public static java.lang.String toJSONString​(java.util.Map<java.lang.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 java.util.Map<java.lang.String,​java.lang.Object> newJSONObject()
        Creates a new JSON object (unordered).
        Returns:
        The new empty JSON object.