public class Json extends Object implements FileDataType
Modifier and Type | Field and Description |
---|---|
protected File |
location
The
File location of this FileDataType |
protected org.json.simple.JSONObject |
root
The root
JSONObject |
Constructor and Description |
---|
Json(File location)
Reads and loads a JSON file into memory
|
Modifier and Type | Method and Description |
---|---|
Object |
get(String path)
Gets the object at the specified path
|
Object |
get(String path,
Object def)
Gets the object at the specified path, or returns the passed "default"
value if nothing is found
|
protected org.json.simple.JSONObject |
getContainer(String... ladder)
Gets the
JSONObject above the requested object specified by the
supplied ladder parameter. |
protected org.json.simple.JSONObject |
getContainer(String path)
Gets the
JSONObject above the requested object specified by the
supplied path parameter. |
protected String[] |
getLadder(String path)
Converts a period-delimited string into a String array
|
boolean |
isSet(String path)
Returns whether or not there is an object located at the specified path
|
void |
save()
Saves any information in memory to the file it was loaded from.
|
void |
save(File target)
Saves any information in memory to the file specified.
|
void |
set(String path,
Object value)
Sets the value at the location specified by the passed path
|
protected org.json.simple.JSONObject |
traverse(boolean makePath,
String... ladder)
Traverses a
JSONObject tree from the internal root node. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
newInstance
protected final File location
File
location of this FileDataType
protected final org.json.simple.JSONObject root
JSONObject
public Json(File location) throws org.json.simple.parser.ParseException
location
- The location of the file to parseorg.json.simple.parser.ParseException
- If the file is not in standard JSON formatpublic void set(String path, Object value)
set
in interface FileDataType
path
- The path to set, delimited by '.
'value
- The value to setpublic boolean isSet(String path)
isSet
in interface FileDataType
path
- The path to set, delimited by '.
'true
if a value is found, false
otherwisepublic Object get(String path)
get
in interface FileDataType
path
- The path to set, delimited by '.
'null
if
nothing is foundpublic Object get(String path, Object def)
get
in interface FileDataType
path
- The path to set, delimited by '.
'def
- The default value to return upon not finding a valueprotected org.json.simple.JSONObject getContainer(String... ladder)
JSONObject
above the requested object specified by the
supplied ladder
parameter.ladder
- A string array, already split in order of levels to
traverseJSONObject
above the requested objectprotected org.json.simple.JSONObject getContainer(String path)
JSONObject
above the requested object specified by the
supplied path
parameter.path
- The period-delimited path to the object desiredJSONObject
above the requested objectprotected String[] getLadder(String path)
path
- The path to splitprotected org.json.simple.JSONObject traverse(boolean makePath, String... ladder)
JSONObject
tree from the internal root node. Will
return a JSONObject
container of the relevant element at the end
of the search, or just an empty JSONObject
if nothing exists.makePath
- Whether to fill empty space with JSONObject
sladder
- A String array depicting the location to search inJSONObject
containing the last node in the ladderpublic void save() throws IOException
save
in interface FileDataType
IOException
- Any read/write locks or permission errors on the filepublic void save(File target) throws IOException
save
in interface FileDataType
target
- The file to save toIOException
- Any read/write locks or permission errors on the fileCopyright © 2015. All rights reserved.