Android Networking: Beyond the Basics

Sep 8 2022 · Kotlin 1.7.10, Android 12, Android Studio Chipmunk

Part 1: Implement Advanced Retrofit

02. Use Different Parsers

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 01. Introduction Next episode: 03. Implement Logging Interceptors & Error Handling

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 02. Use Different Parsers

There are many different parsers, and each is good at what it does, in different ways. For this reason, you’ll learn how to parse data using Kotlin Serialization.

Moshi requires a bit of code to set up, and it has many cool features, but most of the time, you don’t need all of its features. And another popular thing that’s been going around in the Kotlin world is multiplatform! Moshi works for the JVM, but not so much for the Kotlin/Native initiative.

However, Kotlin Serialization has most of what makes up Moshi, in terms of being a performant and simple way to parse data, but it also can work with pure Kotlin, the native initiative, and as such, for multiplatform. For this reason, you can use it for JVM, Native, and JavaScript versions of Kotlin. Let’s see how to implement it! :]

Demo

Kotlin serialization has a few different steps to integrate it into your project. Head over to the project-level build.gradle and add the following code:

classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"

You need to add the Kotlin serialization dependency for the project, to be able to apply the Kotlin serialization plugin to your modules. That being said, head over to the app level build.gradle, and add the following plugin:

apply plugin: 'kotlinx-serialization'

The plugin will enable the use of Kotlin serialization for your models and will generate appropriate helper methods and classes. Finally, add the following dependencies, and remove the Moshi dependency:

implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3"
implementation 'com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0'

These dependencies are for Kotlin Serialization and the Retrofit converter, which works with Kotlin serialization. Now sync the project, and things should be working fine, but because you removed Moshi, there are some errors in the models. Let’s move on to adding Kotlin serialization to Retrofit.

Open RetrofitBuilders, and change the code as follows. First, add an import that you’ll use to build the content type for the parser.

import okhttp3.MediaType.Companion.toMediaType

Then build the parser and replace Moshi:

val contentType = "application/json".toMediaType()
val json = Json {
  ignoreUnknownKeys = true
}
...

.addConverterFactory(json.asConverterFactory(contentType))

The toMediaType function is a helper to create a MediaType object, from a String. The Json converter factory is the important part, which will parse everything automatically. You use the ignoreUnknownKeys mode, similar to how lenient worked in Moshi, to have a more forgiving parser.

And that’s it for the Retrofit part. The only thing you now have to do is change all the models to conform to Kotlin serialization. Let’s do this. Open AddTaskRequest.kt. Remove the field annotations, and add the Serializable annotation at the top of the class.

@Serializable

Also, remove the @field:Json annotation. And that’s it for the migration! Now do this for all the remaining models!

Much cleaner and simpler, right? Run the project, and everything should work, like before! :]

This is why Kotlin serialization is a popular choice nowadays, because not only is it simple to use, but it can be used for Kotlin/Native projects too.