Class JSONObject

    • Constructor Detail

      • JSONObject

        public JSONObject()
        default
      • JSONObject

        public JSONObject​(int initialCapacity)
        Parameters:
        initialCapacity - the initial capacity = (number of elements to store / load factor) + 1
        Throws:
        IllegalArgumentException - If the initial capacity is negative
      • JSONObject

        public JSONObject​(int initialCapacity,
                          float loadFactor)
        Parameters:
        initialCapacity - the initial capacity = (number of elements to store / load factor) + 1
        loadFactor - the load factor
        Throws:
        IllegalArgumentException - If the initial capacity is negative or the load factor is negative
        Since:
        2.0.2
      • JSONObject

        public JSONObject​(int initialCapacity,
                          float loadFactor,
                          boolean accessOrder)
        Parameters:
        initialCapacity - the initial capacity = (number of elements to store / load factor) + 1
        loadFactor - the load factor
        accessOrder - the ordering mode - true for access-order, false for insertion-order
        Throws:
        IllegalArgumentException - If the initial capacity is negative or the load factor is negative
        Since:
        2.0.2
      • JSONObject

        public JSONObject​(Map map)
        Parameters:
        map - the map whose mappings are to be placed in this map
        Throws:
        NullPointerException - If the specified map is null
    • Method Detail

      • get

        public Object get​(String key)
        Returns the Object of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
      • containsKey

        public boolean containsKey​(String key)
        Returns true if this map contains a mapping for the specified key
        Parameters:
        key - the key whose presence in this map is to be tested
      • containsKey

        public boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key
        Specified by:
        containsKey in interface Map<String,​Object>
        Overrides:
        containsKey in class HashMap<String,​Object>
        Parameters:
        key - the key whose presence in this map is to be tested
      • getOrDefault

        public Object getOrDefault​(String key,
                                   Object defaultValue)
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
      • forEchArrayObject

        public void forEchArrayObject​(String key,
                                      Consumer<JSONObject> action)
        Parameters:
        key -
        action -
        Since:
        2.0.52
      • getJSONArray

        public JSONArray getJSONArray​(String key)
        Returns the JSONArray of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        JSONArray or null
      • getString

        public String getString​(String key)
        Returns the String of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        String or null
      • getDoubleValue

        public double getDoubleValue​(String key)
        Returns a double value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        double
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable double
        JSONException - Unsupported type conversion to double value
      • getFloatValue

        public float getFloatValue​(String key)
        Returns a float value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        float
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable float
        JSONException - Unsupported type conversion to float value
      • getLongValue

        public long getLongValue​(String key)
        Returns a long value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        long
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable long
        JSONException - Unsupported type conversion to long value
      • getLongValue

        public long getLongValue​(String key,
                                 long defaultValue)
        Returns a long value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
        Returns:
        long
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable long
        JSONException - Unsupported type conversion to long value
      • getIntValue

        public int getIntValue​(String key)
        Returns an int value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        int
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable int
        JSONException - Unsupported type conversion to int value
      • getIntValue

        public int getIntValue​(String key,
                               int defaultValue)
        Returns an int value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
        Returns:
        int
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable int
        JSONException - Unsupported type conversion to int value
      • getShortValue

        public short getShortValue​(String key)
        Returns a short value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        short
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable short
        JSONException - Unsupported type conversion to short value
      • getByteValue

        public byte getByteValue​(String key)
        Returns a byte value of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        byte
        Throws:
        NumberFormatException - If the value of get is String and it contains no parsable byte
        JSONException - Unsupported type conversion to byte value
      • getBytes

        public byte[] getBytes​(String key)
      • getBooleanValue

        public boolean getBooleanValue​(String key)
        Returns a boolean value of the associated key in this object.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        boolean
        Throws:
        JSONException - Unsupported type conversion to boolean value
      • getBooleanValue

        public boolean getBooleanValue​(String key,
                                       boolean defaultValue)
        Returns a boolean value of the associated key in this object.
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
        Returns:
        boolean
        Throws:
        JSONException - Unsupported type conversion to boolean value
      • getDate

        public Date getDate​(String key)
        Returns the Date of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        Date or null
      • getDate

        public Date getDate​(String key,
                            Date defaultValue)
        Since:
        2.0.27
      • getInstant

        public Instant getInstant​(String key)
        Returns the BigInteger of the associated keys in this JSONObject.
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        BigInteger or null
      • toString

        public String toString​(JSONWriter.Feature... features)
        Serialize to JSON String
        Parameters:
        features - features to be enabled in serialization
        Returns:
        JSON String
      • toJSONString

        public String toJSONString​(JSONWriter.Feature... features)
        Serialize to JSON String
        Parameters:
        features - features to be enabled in serialization
        Returns:
        JSON String
      • toJSONString

        public static String toJSONString​(Object object,
                                          JSONWriter.Feature... features)
        Serialize Java Object to JSON String with specified JSONReader.Features enabled
        Parameters:
        object - Java Object to be serialized into JSON String
        features - features to be enabled in serialization
        Since:
        2.0.6
      • toJSONBBytes

        public byte[] toJSONBBytes​(JSONWriter.Feature... features)
        Serialize to JSONB bytes
        Parameters:
        features - features to be enabled in serialization
        Returns:
        JSONB bytes
      • to

        public <T> T to​(Type type,
                        JSONReader.Feature... features)
        Convert this JSONObject to the specified Object
        
         JSONObject obj = ...
         Map<String, User> users = obj.to(new TypeReference<HashMap<String, User>>(){}.getType());
         
        Parameters:
        type - specify the Type to be converted
        features - features to be enabled in parsing
        Since:
        2.0.4
      • to

        public <T> T to​(TypeReference<T> typeReference,
                        JSONReader.Feature... features)
        Convert this JSONObject to the specified Object
        
         JSONObject obj = ...
         Map<String, User> users = obj.to(new TypeReference<HashMap<String, User>>(){});
         
        Parameters:
        typeReference - specify the TypeReference to be converted
        features - features to be enabled in parsing
        Since:
        2.0.7
      • to

        public <T> T to​(Class<T> clazz,
                        JSONReader.Feature... features)
        Convert this JSONObject to the specified Object
        
         JSONObject obj = ...
         User user = obj.to(User.class);
         
        Parameters:
        clazz - specify the Class<T> to be converted
        features - features to be enabled in parsing
        Since:
        2.0.4
      • toJavaObject

        public <T> T toJavaObject​(Class<T> clazz,
                                  JSONReader.Feature... features)
        Convert this JSONObject to the specified Object
        Parameters:
        clazz - specify the Class<T> to be converted
        features - features to be enabled in parsing
      • getObject

        public <T> T getObject​(String key,
                               Class<T> type,
                               JSONReader.Feature... features)
        Returns the result of the Type converter conversion of the associated value in this JSONObject.

        User user = jsonObject.getObject("user", User.class);

        Parameters:
        key - the key whose associated value is to be returned
        type - specify the Class to be converted
        Returns:
        <T> or null
        Throws:
        JSONException - If no suitable conversion method is found
      • getObject

        public <T> T getObject​(String key,
                               Type type,
                               JSONReader.Feature... features)
        Returns the result of the Type converter conversion of the associated value in this JSONObject.

        User user = jsonObject.getObject("user", User.class);

        Parameters:
        key - the key whose associated value is to be returned
        type - specify the Type to be converted
        features - features to be enabled in parsing
        Returns:
        <T> or null
        Throws:
        JSONException - If no suitable conversion method is found
      • getObject

        public <T> T getObject​(String key,
                               TypeReference<T> typeReference,
                               JSONReader.Feature... features)
        Returns the result of the Type converter conversion of the associated value in this JSONObject.

        User user = jsonObject.getObject("user", User.class);

        Parameters:
        key - the key whose associated value is to be returned
        typeReference - specify the TypeReference to be converted
        features - features to be enabled in parsing
        Returns:
        <T> or null
        Throws:
        JSONException - If no suitable conversion method is found
        Since:
        2.0.3
      • fluentPut

        public JSONObject fluentPut​(String key,
                                    Object value)
        Chained addition of elements
         JSONObject object = new JSONObject().fluentPut("a", 1).fluentPut("b", 2).fluentPut("c", 3);
         
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
      • isValid

        public boolean isValid​(JSONSchema schema)
        Since:
        2.0.4
      • valueFilter

        public void valueFilter​(ValueFilter valueFilter)
        Since:
        2.0.3
      • nameFilter

        public void nameFilter​(NameFilter nameFilter)
        Since:
        2.0.3
      • getSize

        public int getSize​(String key)
        if value instance of Map or Collection, return size, other return 0
        Parameters:
        key -
        Since:
        2.0.24
      • of

        public static JSONObject of()
         JSONObject jsonObject = JSONObject.of();
         
      • of

        public static JSONObject of​(String key,
                                    Object value)
        Pack a pair of key-values as JSONObject
         JSONObject jsonObject = JSONObject.of("name", "fastjson2");
         
        Parameters:
        key - the key of the element
        value - the value of the element
      • of

        public static JSONObject of​(String k1,
                                    Object v1,
                                    String k2,
                                    Object v2)
        Pack two key-value pairs as JSONObject
         JSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2");
         
        Parameters:
        k1 - first key
        v1 - first value
        k2 - second key
        v2 - second value
        Since:
        2.0.2
      • of

        public static JSONObject of​(String k1,
                                    Object v1,
                                    String k2,
                                    Object v2,
                                    String k3,
                                    Object v3)
        Pack three key-value pairs as JSONObject
         JSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3");
         
        Parameters:
        k1 - first key
        v1 - first value
        k2 - second key
        v2 - second value
        k3 - third key
        v3 - third value
        Since:
        2.0.2
      • of

        public static JSONObject of​(String k1,
                                    Object v1,
                                    String k2,
                                    Object v2,
                                    String k3,
                                    Object v3,
                                    String k4,
                                    Object v4)
        Pack three key-value pairs as JSONObject
         JSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4");
         
        Parameters:
        k1 - first key
        v1 - first value
        k2 - second key
        v2 - second value
        k3 - third key
        v3 - third value
        k4 - four key
        v4 - four value
        Since:
        2.0.8
      • of

        public static JSONObject of​(String k1,
                                    Object v1,
                                    String k2,
                                    Object v2,
                                    String k3,
                                    Object v3,
                                    String k4,
                                    Object v4,
                                    String k5,
                                    Object v5)
        Pack three key-value pairs as JSONObject
         JSONObject jsonObject = JSONObject.of("key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5");
         
        Parameters:
        k1 - first key
        v1 - first value
        k2 - second key
        v2 - second value
        k3 - third key
        v3 - third value
        k4 - four key
        v4 - four value
        k5 - five key
        v5 - five value
        Since:
        2.0.21