Skip to content

Usage examples with JSON parsing

Wagner Leonardi edited this page Oct 8, 2019 · 1 revision

In real-world scenarios you will use some library to parse JSON instead of working with JSON strings, we're using moshi in these examples, but feel free to work with any other JSON parsing library that you prefer.

1. Create a class that will represent your Record

We created the class Record.java class to represent our JSON record containing name and age properties.

jsonbox.io will create _id and _createdOn properties when you create a record, that's why our Record.java class also has these properties as well.

The message property is useful when jsonbox.io returns a result message for an update or delete, or even an error. In that way, we can handle unexpected errors checking if that property contains a value.

2. Import jsonbox and JSON parsing library

import io.jsonbox.JsonBoxStorage;
import com.squareup.moshi.*;

3. Create jsonbox and JSON parsing library instances

JsonBoxStorage storage = new JsonBoxStorage("examplebox0000000000");
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Record> adapter = moshi.adapter(Record.class);
JsonAdapter<List<Record>> listAdapter = moshi.adapter(Types.newParameterizedType(List.class, Record.class));

listAdapter is useful because the jsonbox.io response can be a record for create, update and delete or a list of records for read.

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:

Record requestRecord = new Record("john", 28); //record with only name and age properties
String jsonRequestString = adapter.toJson(requestRecord);

String jsonResultString = storage.create(jsonRequestString);
Record resultRecord = adapter.fromJson(jsonResultString); //record with _id and _createdOn properties

Reading all records

String jsonResultString = storage.read();
List<Record> resultRecords = listAdapter.fromJson(jsonResultString);

Reading records with filtering parameters:

String jsonResultString = storage.read(5, 10, "age:<35,name:*i*", "-name");
List<Record> resultRecords = listAdapter.fromJson(jsonResultString);

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");

Creating and updating a record

//creating
Record createRecord = new Record("william", 30);
String jsonCreateResultString = storage.create(adapter.toJson(createRecord));
Record createResultRecord = adapter.fromJson(jsonCreateResultString);
//updating
Record updateRecord = new Record("william", 31);
String result = storage.updateByRecordId(createResultRecord.getId(), adapter.toJson(updateRecord));
String resultMessage = adapter.fromJson(result).getMessage();

Creating and deleting a record

String jsonCreateResultString = storage.create(adapter.toJson(new Record("amy", 19)));
Record createResultRecord = adapter.fromJson(jsonCreateResultString);

String result = storage.deleteByRecordId(createResultRecord.getId());
String resultMessage = adapter.fromJson(result).getMessage();

Deleting multiple records using a filter

String result = storage.deleteByQuery("name:*a*");
String resultMessage = adapter.fromJson(result).getMessage();