Adapting Gson to Kotlin

David Perez
Livefront
Published in
4 min readOct 14, 2020

Let’s start by discussing Gson; Gson is a Java-based library that converts raw JSON into Java classes–a key piece of functionality for many apps. The library has been widely used in Android app development for many years and can be deeply integrated within an app.

Gson works really well in Java apps with Java classes and this all works out well… in Java. But with the advent of Kotlin, many Android developers don’t use Java as their primary language anymore, Kotlin has replaced it. Luckily for all of us, Kotlin has interoperability with Java so you could convert your entire app to Kotlin and Gson would work fine, right?

Sadly no.

To be clear, you can create a simple Kotlin data class using Gson out-of-the-box with zero modifications. Something like this:

But that’s as simple as it can get. How far do you have to go to start seeing issues? As it turns out, not that far at all, something as basic as adding in a small amount of code in the initialization block will fail.

The issue above is that you are using Kotlin-specific features when defining the additional variables. The initialization block is never called, and because of that, the properties will never be initialized. This will manifest itself in an app with a NullPointerException, which can be difficult to discover since the property is supposed to be non-null.

The Problem

Let’s talk about why these classes don’t have their full functionality. While Gson can create a Kotlin data class, it is doing so using reflection and specifically Java reflection.

Code from the Gson Project

Gson creates an empty instance of the object and populates the various fields with the values found in the JSON. The newly minted object has bypassed all the normal processes that are supposed to occur when creating a new object. In this case, the primary constructor is not being called which is normally required. Under normal circumstances there would be no way to create a new instance of a class without calling the primary constructor, the compiler would stop something like that from happening.

Introducing Gson Kotlin Adapter

This is where the Gson Kotlin Adapter comes in. It is a custom adapter that ensures the primary constructor is invoked when Gson creates a new Kotlin object. This enforces that the initialization block is called, and from there you gain all the great features of Kotlin. This will enable not just your own logic in the init block, but language features like delegates, such as the ones used with the by keyword. Default arguments and delegate properties (such as lazy properties) will also function properly.

So what do you have to do to get this all set up in your app? Attach the Gson Kotlin Adapter to your instance of Gson. Your app may already have several customizations in place:

It is as easy as adding one additional line to ensure that the new adapter is enabled in your project.

That’s it. You’ve set up your project to use the Gson Kotlin Adapter. With this in place, all Kotlin classes deserialized through Gson will pass through the adapter.

It is important to note that much like regular Gson, this uses reflection as the primary means of creating the new instance of the objects. Unlike stock Gson, this also uses Kotlin's reflection. This does have a significant downside, and that is performance. The use of Kotlin's reflection is not as optimized as it is in Java, and Gson Kotlin Adapter can take much longer to initialize the individual adapters.

Sometimes it can be difficult to transition to new tools, especially when the previous one is deeply ingrained in the app. Some apps allow for quick and simple upgrades, others can be extremely difficult and require a great deal of planning. Gson Kotlin Adapter is a great stopgap between Gson and a more time-consuming solution.

A Long Term Solution

Another way you can solve this problem is to use one of the new tools created specifically for use with Kotlin. This would be a complete replacement of Gson and any custom code built on top of it, including the Gson Kotlin Adapter. These tools all work in similar ways but do have some minor differences, so it is important to make sure you choose the right one for you. The two most popular ones are:

  • kotlinx.serialization: This is a library maintained by Jetbrains and uses code generation to properly construct Kotlin classes from JSON.
  • Moshi: A library created by Square and supports both reflection-based and code generation based methods for constructing data classes.

David adapts to new things at Livefront

--

--