JsonDB: a Opensource, Java-based, Database
stores its data as .json files
Small memory footprint, runs embedded within your Java program,
Provides APIs that are very similar in names and semantics to those of Spring Data for MongoDB,
Supports encryption of data, XPath based search/find queries.
Why
Why Jsondb
You have a Java based program (or even Android App) and you:
- Need a light weight database of some kind to store a small amount (a few 1000 rows) of data,
- Need a pure java, easy to use, thread safe database,
- Need some ORM support,
- Need a good query interface,
- Okay with Mongodb like per API level atomicity (okay with eventualy data consistency),
- Want to be able to easily add, edit, update, delete the db data files, even when Jsondb is running,
- Want to be able to move the db files seamlessly, download updated files over network while db is running,
- Want to be able to safegaurd sensitive data (passwords etc) in the db data files, with some high strength encryption,
Why NOT Jsondb
- Jsondb loads the db data files into memory, all API’s return deeply cloned objects so you will need minimum as much memory as the size of data files and some more,
- Jsondb does not have full transaction support (does support Mongodb like atomic API’s)
download
Getting Jsondb
Stable Jsondb builds are uploaded to Maven Central
- You can download the jar, source-jar or javadoc-jar from Maven Central and use it in the traditional way by adding it into your classpath. Jsondb uses quite a few of other opensource java libraries so doing it this way will involve getting all those libraries and adding to your classpath.
- Optionally (Recommended) if you use Maven, Gradle or any such build system you can get it as a dependency using the appropriate dependency code.
- Since March 2019 two different builds are released, Each version is suffixed with -j8 or -j11. First one is built using Jdk version 1.8(LTS) and second one is built using Jdk version 11(LTS). This is to facilitate users who are still on Java 8.
Example:
usage
Using Jsondb
The best way to understand usage of JsonDB is to look at code of the many Junit tests in its source code. Every possible scenario is covered in these junit test classes.
For those of you who need a quick intro:
The main class to use in Jsondb is: io.jsondb.JsonDBTemplate
JsonDBTemplate can be used in a very similar way as Spring Data MongoTemplate class.
Instantiating JsonDBTemplate
Defining a POJO
Every object that you wish to save in JsonDB must be a POJO (Plain Old Java Object) that satisfies the JavaBean programming conventions. Each object must implement either Serializable or Externalizable, must have a no-arg(default) constructor, and each of its properties(attributes) must have public setter and getter methods. The names of these methods must be as per the JavaBean naming conventions.
Note the package in the POJO its the same as the 2nd argument we passed while instantiating the JsonDBTemplate
object.
Also note the 3 annotations in the POJO:
- @Document : this annotation signifies that this POJO is a Jsondb Collection, the attribute
collection
specifies the name of the.json
file on disk, the attributeschemaVersion
specifies schema version this support is not yet completed. - @Id : this annotation signifies that this field is the primary key in the collection. Jsondb supports only one Id field per collection.
- @Secret : this annotation signifies that the value of this field should be encrypted before writing to disk.
Creating a collection if it does not exist
Inserting a document into a collection
Removing a document from a collection
Save a document into a collection
Upsert a document into a collection
Find a document when you know the Id
XPath based Queries in Jsondb
XPATH syntax allows for numerous ways to query and locate document(s) of interest. Below are few examples:
Find a document(s) with equals criteria
Find a document(s) with NOT equals criteria
Find a document(s) with greater than criteria
Find and Remove first document based on query as Atomic operation
Find and Remove all documents based on query as Atomic operation
Find and Modify first document based on query as Atomic operation
Find and Remove all documents based on query as Atomic operation
Sorting and Slicing in Jsondb
Sorting Jsondb allows you to obtain a sorted result with the find() and findAll() methods.
Slicing Jsondb slicing feature is similar to the python/numpy slicing feature. Slice string parameter is expected in format i:j:k and should following all the rules similar to the slicing feature in python/numpy. Slicing can be used in combination with sorting or without sorting
Encryption support in Jsondb
-
If you do not need encryption support simply use a
JsonDBTemplate
constructor that does not require a cipher argument or pass null. Make sure you are not using the@Secret
annotation in any of your POJO’s. -
However If you do need encryption support then as in the examples above instantiate a instance of
io.jsondb.crypto.Default1Cipher
class and pass that when instantiatingJsonDBTemplate
. -
If you need help creating a secure
Symmetric Key
for the above default cipher use the following standalone.
-
If you need a 192bit or 256bit secure
Symmetric Key
ensure that you have the Unlimited Strength JCE Policy jars in yourJRE\lib\security
folder and then write a modifed method similar the theCryptoUtil.generate128BitKey()
. Compile and Execute it and use the key when instantiatingJsonDBTemplate
. -
If you need a custom encryption support say DES Encryption you can implement the interface
io.jsondb.crypto.ICipher
and then use that when instantiatingJsonDBTemplate
. -
At some later time if you need to change the
cipher
and/or thekey
used then use the below api. Make sure you back up the database files in case something goes wrong.