Skip to content

Store complex Objects

David Kunzler edited this page Sep 9, 2019 · 2 revisions

Esperandro allow the developer to not only store the basic preference types (int, long, float, String, Set) but Objects of arbitrary type. The only condition is that the type must be serializable in a way.

In the context of esperandro "serializable" means the instance of an object can be turned into a String. This String is then saved in the SharedPreference.

Serializers

Custom Serializer

Esperandro provides an interface Serializer that can be implemented to provide a way to turn objects into strings.

This Serializer must be advertised to esperandro via the method Esperandro.setSerializer(Serializer) once. This can for example be done in a custom Application class in the onCreate method to be globally available throughout the app.

Provided Serializers

There are two addons that provide pre-configured serializers. Both of these use json to store and retrieve objects. Which one is chosen depends on the project and other dependencies that are present.

First there is a serializer that uses Google's gson library for object de-/serialization. To use this addon add the following dependency to the project:

implementation 'de.devland.esperandro:esperandro-gson-addon:<insert version>'

Second there is a serializer that uses the jackson-databind library for object de-/serialization. To use this addon add the following dependency to the project:

implementation 'de.devland.esperandro:esperandro-jackson-addon:<insert version>'

Both of these addons are autodetected and don't need to be set via the Esperandro.setSerializer method.

Clone this wiki locally