Open In App

Integrating JsonToKotlin Plugin With Android Studio

Last Updated : 18 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Android Studio is the best IDE for android development. Plenty of plugins are available and they can be installed easily via Android Studio itself. In computing, a plug-in is a software component that adds a particular characteristic to an existing computer program. When a program supports plug-ins, it enables customization. Plugins are a great way to increase productivity and overall programming experience.⁣⁣ Some tasks are boring and not fun to do, by using plugins in the android studio you can get more done in less time. One such plugin that we are going to see in this article is “JsonToKotlin Plugin“.

Note: You may refer to the 6 Most Useful Android Studio Plugins to know the most useful android studio plugins.

About JsonToKotlin Plugin

JSON to kotlin Class is a plugin to create Kotlin data class from JSON string, in other words, a plugin that changes JSON string to Kotlin data class. With this, you can generate a Kotlin data class from the JSON string programmatically. Supporting (almost) all kinds of JSON libs’ annotation(Gson, Jackson, Fastjson, MoShi and LoganSquare, kotlinx.serialization(default custom value)). Some of the important features are:

  • Customizing the own annotations
  • Initializing properties with default values
  • Allowing properties to be nullable(?)
  • Determining property nullability automatically
  • Renaming field names to be camelCase style when selecting a target JSON lib annotation.
  • Generating Kotlin class as individual classes
  • Generating Kotlin class as inner classes
  • Formatting any legal JSON string
  • Generating Map Type when JSON key is the primitive type
  • Only create annotation when needed
  • Custom define data class parent class
  • Sort property order by Alphabetical
  • Make keyword property valid
  • Support Loading JSON From Paster/Local File/HTTP URL
  • Support customize your own plugin by Extension Module
  • Normal Class support
  • Dynamic plugin load support
  • Support generating ListClass from JSONArray
  • Complex JSON schema supporting

JSON to kotlin Class is an excellent tool for Kotlin developers and it can convert a JSON string to Kotlin data class. The tool could not only understand the primitive types but also auto-create complex types. It’s simply accessible. We provide shortcut keymap ALT + K for Windows and Option + K for Mac, have a try and you are going to fall in love with it! JsonToKotlinClass just makes programming more delightful.

Installation of JsonToKotlin Plugin

Note: You may refer to How to Install and Uninstall Plugins in Android Studio? to Install and Uninstall Plugins in Android Studio.

Step 1: Go to the File > Settings > Plugins > Search for jsontokotlin as shown in the below image.

Step 2: Press the install button

Step 3: Once you got the plugin, please install it by clicking on the “Install” button. Then select the project location and right-click on that folder, select New > Kotlin data class file from JSON (as shown in the image below)

Step 4: Then in the place of JSON provision, provide the JSON that you want to convert. In our example, let us give  the sample output of JSON that is meant for Exchange Rates as shown in the below image

Once it is given and generate, we can able to see 2 files got generated as shown in the below-attached image. Here two data class are generated.

ExchangeRates” is the data class nothing but the name that we specified  If we check the JSON output and generated data class “ExchangeRates”, all the column names like

  • base -> String datatype,
  • date -> String datatype,
  • rates -> Rates datatype,
  • time_last_updated -> int datatype 

is created very easily.

Kotlin




// JSON to kotlin conversion is 
// easier with JSONToKotlin plugin
data class ExchangeRates(
    val base: String,
    val date: String,
    val rates: Rates,
    val time_last_updated: Int
)


And since many “Rates” are there, which are nothing but the exchange rate value of all the available worldwide currencies, it has got created with another data class.

Kotlin




// In JSON, below are the currencies 
// available and they are presented here
data class Rates(
    val AED: Double,
    val ARS: Double,
    val AUD: Double,
    val BGN: Double,
    val BRL: Double,
    val BSD: Int,
    val CAD: Double,
    val CHF: Double,
    val CLP: Double,
    val CNY: Double,
    val COP: Double,
    val CZK: Double,
    val DKK: Double,
    val DOP: Double,
    val EGP: Double,
    val EUR: Double,
    val FJD: Double,
    val GBP: Double,
    val GTQ: Double,
    val HKD: Double,
    val HRK: Double,
    val HUF: Double,
    val IDR: Double,
    val ILS: Double,
    val INR: Double,
    val ISK: Double,
    val JPY: Double,
    val KRW: Double,
    val KZT: Double,
    val MVR: Double,
    val MXN: Double,
    val MYR: Double,
    val NOK: Double,
    val NZD: Double,
    val PAB: Int,
    val PEN: Double,
    val PHP: Double,
    val PKR: Double,
    val PLN: Double,
    val PYG: Double,
    val RON: Double,
    val RUB: Double,
    val SAR: Double,
    val SEK: Double,
    val SGD: Double,
    val THB: Double,
    val TRY: Double,
    val TWD: Double,
    val UAH: Double,
    val USD: Int,
    val UYU: Double,
    val ZAR: Double
)


Advanced Features

  • One can customize your own annotations

  • Can initialize properties with default values
  • Can allow properties to be nullable
  • Can determine property nullability automatically

  • Can rename property names to be camelCase style when selecting a target JSON lib annotation.
  • Can generate Kotlin class as individual classes
  • Can Generate Kotlin class as inner classes
  • Can format any legal JSON string
  • Can generate Map Type when JSON key is a primitive type.
  • Can only create annotation when needed.
  • Custom define data class parent class
  • Sort property order by Alphabetical
  • Make keyword property valid
  • Support Loading JSON From Paster/Local File/HTTP URL
  • Support customize your own plugin by Extension Module

Let us consider one more example

JSON File:

Check on advanced options:

Resultant Kotlin class:

Hence any valid formatted JSON can be converted into Kotlin data class very easily by using the amazing “JSONToKotlin” plugin. Very helpful for your development. Advanced properties help to convert JSON to kotlin with the available options and hence it is very useful in the development.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads