JsonDB is a fast, light-weight JSON key-value storage engine for Java
Pre-Requisites:
- Java 1.8
- Maven 1.4
- Feel free to use newer versions of these but I
can't promise that everything will work as intented
- Feel free to use newer versions of these but I
Start off by cloning the repository:
git clone https://github.com/AlexStan0/json-db
Then cd into the same directory as pom.xml
and install/update maven dependencies:
mvn clean install -U
Follow up by creating the JAR file:
mvn package
You can run the JAR using java -jar /path/to/json-db-1.0.jar
or
you can import the jar into your maven project.
Add these lines to you pom.xml
<dependency>
<groupId>com.jsondb</groupId>
<artifactId>jsondb</artifactId>
<version>1.0</version>
<systemPath>/path/to/json-db-1.0.jar</systemPath>
</dependency>
import com.jsondb.JsonDB;
JsonDB db = new JsonDB(path)
The path for example is where your JSON file is located.
For example: C:/Users/<user>/path/to/javaProject/database.json
The constructor also checks if the file exists and if the file dosen't exist
it creates it in the the highest folder and writes {}
to it
db.set(key, value);
The provided key
must always have a value
or else the program will
throw an error and not write the data to the JSON file.
db.setObj(objKey, key, value)
This does the exact same thing as the previous set
method but instead
it will create a new JSON Object, write the data, and associate the data with
the objKey
as a nested JSON Object
db.get(key, objKey)
This get method returns an Object, meaning that it can not return
an array. To do that you should use the db.arrGet(key, objKey)
method
db.arrGet(key, objKey)
This method is the same as the get
method but returns an
Object array instead of a simple Object type.
db.has(key, objKey)
If you want to check to see if your JSON file has a certain key you
can use the db.has()
method to check for it.
db.delete(key, objKey)
If a certain key needs to be deleted you can delete it using the db.delete()
method.
This method scans the JSON file to make sure the key exists and the deletes it. The objKey
parameter
is used in case the key
parameter is a nested JSON Object.
If you no longer need to use the database and don't want the IRS to find your data then this method is for you.
When you call db.delete()
it creates a new JSON Object and overwrites the old one in the JSON file. Once this method
has been called all data previously written to the JSON file will be lost.
As you may have noticed both of these methods have a second parameter objKey
.
This parameter is 100% optional and is ment to be use when the key
parameter references a nested JSON Object
Example:
Say you had this JSON file
{
"game-name": "my-game-name",
"player 1": {
"username": "KingSlayer123",
"score": 274,
"health": 95,
"inventory": ["sword", "potion", "bow", "arrows"]
}
}
Say you wanted to check for the existance of game-name
, you could call db.has("game-name")
But what if you wanted to check for the existance of score
in the nested JSON Object player 1
?
This is where the 'objKey' parameter comes in. If the 'key' paramter references a nested JSON Object it fetches or checks
for the existance of objKey
in the nested JSON Object.