Class JSONObject

All Implemented Interfaces:
Serializable, Cloneable, InvocationHandler, Map<String,Object>

public class JSONObject extends LinkedHashMap<String,Object> implements InvocationHandler
See Also:
  • Constructor Details

    • 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 Details

    • 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
    • get

      public Object get(Object key)
      Returns the Object of the associated keys in this JSONObject.
      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class LinkedHashMap<String,Object>
      Parameters:
      key - the key whose associated value is to be returned
      Since:
      2.0.2
    • getByPath

      public Object getByPath(String jsonPath)
    • 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
    • getOrDefault

      public Object getOrDefault(Object key, Object defaultValue)
      Specified by:
      getOrDefault in interface Map<String,Object>
      Overrides:
      getOrDefault in class LinkedHashMap<String,Object>
      Parameters:
      key - the key whose associated value is to be returned
      defaultValue - the default mapping of the key
      Since:
      2.0.2
    • 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
    • getList

      public <T> List<T> getList(String key, Class<T> itemClass, JSONReader.Feature... features)
    • getJSONObject

      public JSONObject getJSONObject(String key)
      Returns the JSONObject of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      JSONObject 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
    • getDouble

      public Double getDouble(String key)
      Returns the Double of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Double or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable double
      JSONException - Unsupported type conversion to Double
    • 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
    • getFloat

      public Float getFloat(String key)
      Returns the Float of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Float or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable float
      JSONException - Unsupported type conversion to Float
    • 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
    • getLong

      public Long getLong(String key)
      Returns the Long of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Long or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable long
      JSONException - Unsupported type conversion to Long
    • 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
    • getInteger

      public Integer getInteger(String key)
      Returns the Integer of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Integer or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable int
      JSONException - Unsupported type conversion to Integer
    • 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
    • getShort

      public Short getShort(String key)
      Returns the Short of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Short or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable short
      JSONException - Unsupported type conversion to Short
    • 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
    • getByte

      public Byte getByte(String key)
      Returns the Byte of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Byte or null
      Throws:
      NumberFormatException - If the value of get is String and it contains no parsable byte
      JSONException - Unsupported type conversion to Byte
    • 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
    • getBoolean

      public Boolean getBoolean(String key)
      Returns the Boolean of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      Boolean or null
      Throws:
      JSONException - Unsupported type conversion to Boolean
    • 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
    • getBigInteger

      public BigInteger getBigInteger(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
      Throws:
      JSONException - Unsupported type conversion to BigInteger
      NumberFormatException - If the value of get is String and it is not a valid representation of BigInteger
    • getBigDecimal

      public BigDecimal getBigDecimal(String key)
      Returns the BigDecimal of the associated keys in this JSONObject.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      BigDecimal or null
      Throws:
      JSONException - Unsupported type conversion to BigDecimal
      NumberFormatException - If the value of get is String and it is not a valid representation of BigDecimal
    • 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
    • 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()
      Serialize to JSON String
      Overrides:
      toString in class AbstractMap<String,Object>
      Returns:
      JSON String
    • 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(Function<JSONObject,T> function)
      Since:
      2.0.4
    • 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<?> 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
    • toJavaObject

      public <T> T toJavaObject(Type type, JSONReader.Feature... features)
      Deprecated.
      since 2.0.4, please use to(Type, JSONReader.Feature...)
      Convert this JSONObject to the specified Object
      Parameters:
      type - specify the Type to be converted
      features - features to be enabled in parsing
    • toJavaObject

      public <T> T toJavaObject(TypeReference<?> typeReference, JSONReader.Feature... features)
      Deprecated.
      since 2.0.4, please use to(Type, JSONReader.Feature...)
      Convert this JSONObject to the specified Object
      Parameters:
      typeReference - specify the TypeReference 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<?> 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
    • getObject

      public <T> T getObject(String key, Function<JSONObject,T> creator)
      Since:
      2.0.4
    • invoke

      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
      Specified by:
      invoke in interface InvocationHandler
      Parameters:
      proxy - proxy object, currently useless
      method - methods that need reflection
      args - parameters of invoke
      Throws:
      UnsupportedOperationException - If reflection for this method is not supported
      ArrayIndexOutOfBoundsException - If the length of args does not match the length of the method parameter
      Throwable
    • putArray

      public JSONArray putArray(String name)
    • putObject

      public JSONObject putObject(String name)
    • 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
    • clone

      public JSONObject clone()
      Overrides:
      clone in class HashMap<String,Object>
      See Also:
    • eval

      public Object eval(JSONPath path)
      See Also:
    • 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");
       
      Parameters:
      k1 - first key
      v1 - first value
      k2 - second key
      v2 - second value
      k3 - third key
      v3 - third value
      k4 - third key
      v4 - third value
      Since:
      2.0.8
    • parseObject

      public static <T> T parseObject(String text, Class<T> objectClass)
    • parseObject

      public static <T> T parseObject(String text, Class<T> objectClass, JSONReader.Feature... features)
    • parseObject

      public static <T> T parseObject(String text, Type objectType, JSONReader.Feature... features)
    • parseObject

      public static <T> T parseObject(String text, TypeReference<?> typeReference, JSONReader.Feature... features)
    • parseObject

      public static JSONObject parseObject(String text)
    • parse

      public static JSONObject parse(String text, JSONReader.Feature... features)
      Since:
      2.0.13