-
Notifications
You must be signed in to change notification settings - Fork 1
Usage examples with JSON parsing
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.
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.
import io.jsonbox.JsonBoxStorage;
import com.squareup.moshi.*;
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:
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
String jsonResultString = storage.read();
List<Record> resultRecords = listAdapter.fromJson(jsonResultString);
String jsonResultString = storage.read(5, 10, "age:<35,name:*i*", "-name");
List<Record> resultRecords = listAdapter.fromJson(jsonResultString);
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
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();
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();
String result = storage.deleteByQuery("name:*a*");
String resultMessage = adapter.fromJson(result).getMessage();