Skip to content

Usage examples

Wagner Leonardi edited this page Oct 11, 2019 · 3 revisions

1. Add jsonbox library in your project dependencies.

Example using Maven, add jsonbox in your pom.xml:

<dependency>
  <groupId>io.jsonbox</groupId>
  <artifactId>jsonbox</artifactId>
  <version>1.0.3</version>
</dependency>

2. Import jsonbox in your code:

import io.jsonbox.JsonBoxStorage;

3. Create an instance for JsonBoxStorage:

JsonBoxStorage storage = new JsonBoxStorage("examplebox0000000000");

4. Now you can use any function from the documentation, to create, read, update and delete records in your store or collection, see examples:

Creating an new record:

String result = storage.create("{\"name\": \"john\", \"age\" : 27}");

Reading all records (limit: 1000):

String result = storage.read();

Reading records with pagination:

int pageSize = 10;
String firstPage = storage.read(0, pageSize);
String secondPage = storage.read(pageSize, pageSize);
String thirdPage = storage.read(pageSize * 2, pageSize)

Reading with filters:

String namesStartingWithJ = storage.readFiltering("name:j*");
String namesContainingA = storage.readFiltering("name:*a*");
String ageLessThan35 = storage.readFiltering("age:<35");
String nameAndAgeFiltering = storage.readFiltering("name:*a*,age:<35");

String filteringAndPagination = storage.read(5, 10,"age:<35",age:<35");

Reading with sorting

String sortedResultByName = storage.readSorting("name");
String sortedResultByNameDescending = storage.readSorting("-name");

String paginationAndSorting = storage.read(5, 10, null, "-name");

String paginationAndFilterAndSorting = storage.read(5, 10, "age:<35", "-name");

Updating a record

String recordId = "0000000000";
String newRecord = "{\"name\": \"john\", \"age\" : 27}";
String result = storage.update(recordId, newRecord);

Deleting a record

String recordId = "0000000000";
String result = storage.delete(recordId);

Deleting multiple records using a filter

String recordId = "0000000000";
String result = storage.deleteFiltering("age:<35");

5. Parsing JSON

These are basic examples, to use with a JSON parsing library please check this guide.