Gson Kotlin AdapterAn open source library to get the most out of Gson when coding in Kotlin.
Why we built it.
Gson is a widely-used tool, created by Google, for converting Java Objects into their JSON representation and vice versa. It's built for Java–the language of choice for Android developers for years. When Kotlin arrived, it quickly became the new language of choice, and newer tools like Moshi and KotlinX Serialization have been introduced to replace Gson. In many cases, for older apps, it's not a trivial task to migrate from one tool to the next. For apps using Gson, it's difficult to take advantage of all the benefits of Kotlin data classes; the root issue is that Gson does not call the primary constructor of the Kotlin data class.
Gson Kotlin Adapter is a TypeAdapterFactory that solves the root problem by ensuring that the primary constructor of the Kotlin data class is invoked when deserializing JSON into a data class. This allows the app to utilize all of the benefits of Kotlin data classes.
How does it work?
Normally when deserializing JSON to Kotlin, Gson hands the JSON to a reflection-based TypeAdapter which populates an empty class with the data. Gson Kotlin Adapter will step in before that happens, parse the JSON itself, and then pass those properties into the Kotlin class's primary constructor.
How do I use it?
Gson Kotlin Adapter is simple to use: just add it to your instance of Gson.
See our GitHub project for more details.
We are Livefront.
Our team creates beautiful mobile apps for people in motion. We work every day with Fortune 500 companies and startups alike to build custom software for phones, tablets, mobile payment wallets, and wearables like watches and glasses. We care deeply about quality, and we love what we do.