Class JSONObject

java.lang.Object
dev.iahmadgad.json.JSONObject

public class JSONObject extends Object
JSONObject Class.

JSONObject is an object contains (key - value) pairs, keys are always Strings, while values can only be integers, doubles, booleans, or nested JSONObjects and JSONArrays, but it can't be chars or any other type.

Since:
0.3
  • Field Details

    • indentation

      protected int indentation
      Indentation and space around colon in the String.
    • spaceAroundColon

      protected int spaceAroundColon
      Indentation and space around colon in the String.
  • Constructor Details

    • JSONObject

      public JSONObject()
      The main Constructor which initialises a new JSONObject.
    • JSONObject

      public JSONObject(String text)
      The Constructor which creates a new JSONObject from a text.
      Parameters:
      text - Source JSON string (text)
    • JSONObject

      public JSONObject(File file)
      The Constructor which creates a new JSONObject from a file.
      Parameters:
      file - Source JSON file
    • JSONObject

      public JSONObject(T object)
      The Constructor which creates a new JSONObject from a Java object.

      it creates pairs from the object's fields as variables, arrays and lists (with valid values only as mentioned above).

      Parameters:
      object - Source Java object
  • Method Details

    • toClass

      public <T> T toClass(Class<T> c)
      Converts the JSONObject to a Java Object.

      it takes JSONObject keys and assign their values to the object fields which the same name as the key.

      Parameters:
      c - Java object class
      Returns:
      An Object of the same class
    • toString

      public String toString()
      Returns a string representation of the JSONObject.
      Overrides:
      toString in class Object
    • write

      public void write(File file)
      Writes the JSONObject in a file.
      Parameters:
      file -
    • setIndentation

      public void setIndentation(int i)
      Sets indentation with some int value.
      Parameters:
      i -
    • setIndentation

      public void setIndentation(JSONEnum e)
      Sets indentation with some Enum value.
      Parameters:
      e -
    • setSpaceAroundColon

      public void setSpaceAroundColon(int i)
      Sets space around colon with some int value.
      Parameters:
      i -
    • setSpaceAroundColon

      public void setSpaceAroundColon(JSONEnum e)
      Sets space around colon with some Enum value.
      Parameters:
      e -
    • setStringDefault

      public void setStringDefault()
      Sets all String settings to their defaults.
    • getNode

      public HashMap<String,Object> getNode()
      Returns the main HashMap that stores JSONObject pairs.
      Returns:
      node
    • getId

      public String getId()
      Returns JSONObject id.
      Returns:
      id
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Iterates over the whole JSONObjects entries.
      Returns:
      Set of entries
    • put

      public void put(String key, Object value)
      Puts pair into the JSONObject.
      Parameters:
      key -
      value -
    • remove

      public void remove(String key)
      Remove this key and value stored in it.
      Parameters:
      key -
    • replace

      public void replace(String key, Object value)
      Replaces existing key's value in the JSONObject.
      Parameters:
      key -
      value -
    • get

      public Object get(String key)
      Gets Object stored in this key.
      Parameters:
      key -
      Returns:
      Object value
    • getString

      public String getString(String key)
      Gets String stored in this key.
      Parameters:
      key -
      Returns:
      String value
    • getBoolean

      public boolean getBoolean(String key)
      Gets boolean stored in this key.
      Parameters:
      key -
      Returns:
      boolean value
    • getInt

      public int getInt(String key)
      Gets int stored in this key.
      Parameters:
      key -
      Returns:
      int value
    • getDouble

      public double getDouble(String key)
      Gets double stored in this key.
      Parameters:
      key -
      Returns:
      double value
    • getJSONObject

      public JSONObject getJSONObject(String key)
      Gets nested JSONObject stored in this key.
      Parameters:
      key -
      Returns:
      JSONObject value
    • getJSONArray

      public JSONArray getJSONArray(String key)
      Gets nested JSONArray stored in this key.
      Parameters:
      key -
      Returns:
      JSONArray value
    • get

      public Object get(JSONPointer pointer)
      Gets object stored in this pointer.
      Parameters:
      pointer -
      Returns:
      Object value
    • getString

      public String getString(JSONPointer pointer)
      Gets String stored in this pointer.
      Parameters:
      pointer -
      Returns:
      String value
    • getBoolean

      public boolean getBoolean(JSONPointer pointer)
      Gets boolean stored in this pointer.
      Parameters:
      pointer -
      Returns:
      boolean value
    • getInt

      public int getInt(JSONPointer pointer)
      Gets int stored in this pointer.
      Parameters:
      pointer -
      Returns:
      int value
    • getDouble

      public double getDouble(JSONPointer pointer)
      Gets double stored in this pointer.
      Parameters:
      pointer -
      Returns:
      double value
    • getJSONObject

      public JSONObject getJSONObject(JSONPointer pointer)
      Gets JSONObject stored in this pointer.
      Parameters:
      pointer -
      Returns:
      JSONObject value
    • getJSONArray

      public JSONArray getJSONArray(JSONPointer pointer)
      Gets JSONArray stored in this pointer.
      Parameters:
      pointer -
      Returns:
      JSONArray value