Package dev.iahmadgad.json
Class JSONObject
java.lang.Object
dev.iahmadgad.json.JSONObject
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 Summary
Modifier and TypeFieldDescriptionprotected int
Indentation and space around colon in the String.protected int
Indentation and space around colon in the String. -
Constructor Summary
ConstructorDescriptionThe main Constructor which initialises a new JSONObject.JSONObject
(File file) The Constructor which creates a new JSONObject from a file.JSONObject
(String text) The Constructor which creates a new JSONObject from a text.JSONObject
(T object) The Constructor which creates a new JSONObject from a Java object. -
Method Summary
Modifier and TypeMethodDescriptionentrySet()
Iterates over the whole JSONObjects entries.get
(JSONPointer pointer) Gets object stored in this pointer.Gets Object stored in this key.boolean
getBoolean
(JSONPointer pointer) Gets boolean stored in this pointer.boolean
getBoolean
(String key) Gets boolean stored in this key.double
getDouble
(JSONPointer pointer) Gets double stored in this pointer.double
Gets double stored in this key.getId()
Returns JSONObject id.int
getInt
(JSONPointer pointer) Gets int stored in this pointer.int
Gets int stored in this key.getJSONArray
(JSONPointer pointer) Gets JSONArray stored in this pointer.getJSONArray
(String key) Gets nested JSONArray stored in this key.getJSONObject
(JSONPointer pointer) Gets JSONObject stored in this pointer.getJSONObject
(String key) Gets nested JSONObject stored in this key.getNode()
Returns the main HashMap that stores JSONObject pairs.getString
(JSONPointer pointer) Gets String stored in this pointer.Gets String stored in this key.void
Puts pair into the JSONObject.void
Remove this key and value stored in it.void
Replaces existing key's value in the JSONObject.void
setIndentation
(int i) Sets indentation with some int value.void
Sets indentation with some Enum value.void
setSpaceAroundColon
(int i) Sets space around colon with some int value.void
Sets space around colon with some Enum value.void
Sets all String settings to their defaults.<T> T
Converts the JSONObject to a Java Object.toString()
Returns a string representation of the JSONObject.void
Writes the JSONObject in a file.
-
Field Details
-
indentation
protected int indentationIndentation and space around colon in the String. -
spaceAroundColon
protected int spaceAroundColonIndentation and space around colon in the String.
-
-
Constructor Details
-
JSONObject
public JSONObject()The main Constructor which initialises a new JSONObject. -
JSONObject
The Constructor which creates a new JSONObject from a text.- Parameters:
text
- Source JSON string (text)
-
JSONObject
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
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
Returns a string representation of the JSONObject. -
write
Writes the JSONObject in a file.- Parameters:
file
-
-
setIndentation
public void setIndentation(int i) Sets indentation with some int value.- Parameters:
i
-
-
setIndentation
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
Sets space around colon with some Enum value.- Parameters:
e
-
-
setStringDefault
public void setStringDefault()Sets all String settings to their defaults. -
getNode
Returns the main HashMap that stores JSONObject pairs.- Returns:
node
-
getId
Returns JSONObject id.- Returns:
id
-
entrySet
Iterates over the whole JSONObjects entries.- Returns:
- Set of entries
-
put
Puts pair into the JSONObject.- Parameters:
key
-value
-
-
remove
Remove this key and value stored in it.- Parameters:
key
-
-
replace
Replaces existing key's value in the JSONObject.- Parameters:
key
-value
-
-
get
Gets Object stored in this key.- Parameters:
key
-- Returns:
- Object value
-
getString
Gets String stored in this key.- Parameters:
key
-- Returns:
- String value
-
getBoolean
Gets boolean stored in this key.- Parameters:
key
-- Returns:
- boolean value
-
getInt
Gets int stored in this key.- Parameters:
key
-- Returns:
- int value
-
getDouble
Gets double stored in this key.- Parameters:
key
-- Returns:
- double value
-
getJSONObject
Gets nested JSONObject stored in this key.- Parameters:
key
-- Returns:
- JSONObject value
-
getJSONArray
Gets nested JSONArray stored in this key.- Parameters:
key
-- Returns:
- JSONArray value
-
get
Gets object stored in this pointer.- Parameters:
pointer
-- Returns:
- Object value
-
getString
Gets String stored in this pointer.- Parameters:
pointer
-- Returns:
- String value
-
getBoolean
Gets boolean stored in this pointer.- Parameters:
pointer
-- Returns:
- boolean value
-
getInt
Gets int stored in this pointer.- Parameters:
pointer
-- Returns:
- int value
-
getDouble
Gets double stored in this pointer.- Parameters:
pointer
-- Returns:
- double value
-
getJSONObject
Gets JSONObject stored in this pointer.- Parameters:
pointer
-- Returns:
- JSONObject value
-
getJSONArray
Gets JSONArray stored in this pointer.- Parameters:
pointer
-- Returns:
- JSONArray value
-