Auto-Generate TypeConverters for entity classes: Room

Image for post
Image for post
Photo by benjamin lehman on Unsplash

Room is a powerful library that provides an abstraction over standard SQLite for robust database access. Under the hood, for some part, it uses annotation processing to generate `Dao` & `Entity` classes.

Although the library is very useful there is one thing that I find writing/copy-pasting the same code all the time & that is TypeConverters .

What is a Type Converter?

Room supports only primitive data types (String, int, etc) so to access complex (non-primitive) data you need to write a TypeConverter. A Type Converter is a method annotated with @TypeConverter. It must receive exactly one parameter and have a non-void return type. Room can then call such methods if they meet one of these conditions:

  • must be static
  • or must be declared inside Kotlin object
  • or must be declared inside a class that has a no-argument public constructor.

A typical example of TypeConverter is given below.

Automating the process

Like said, automating the creation of these TypeConverter classes can save us a lot of time if you are just serializing the types with a serializer like Moshi, Gson, kotlinx-serialization, etc.

We are going to use an annotation processing library called AutoBindings (supports incremental processing). So, you need to add the following dependencies in your module’s build.gradle file.

So suppose I’ve got a User entity with attributes like name (string), age (int), gender (enum), hobbies (List<T>), etc. and I need a type converter for this type. All I’ve to do is annotate the class with @AutoGenerateConverter and supply using parameter with the serializer I am using.

In the above example, I’m generating a TypeConverter using Moshi serializer. As you can notice there is built-in support for Enum types (in case you want to ignore them annotate it with @IgnoreConverter , more details here). To support List<T> types I have annotated the Hobby data class with @AutoGenerateListConverter . It also supports Map<K,V> & Pair<K,V> (more details here).

The final generated class will be created within the same package where the annotated entity class exists.

Lastly, you can add these classes to the Room database. Make sure to specify fully qualified names (shown below) otherwise you will get a compile-time error.

Conclusion

Using an annotation processor we can save some time writing custom converters by ourselves. Also, this method is only helpful if you just want to serialize the data into JSON.

The library is open-sourced on Github and supports more such annotation processing methods to eliminate some boilerplate codes.

Keep an eye on this wiki page which will be updated if any breaking changes occur within the library.

For any feature request or bug report just file an issue.

Scheduling my day tasks with "WorkManager"

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store