RIP http://www.jsonschema2pojo.org

Ramesh Prasad
4 min readJul 31, 2018

--

If you are an Android Developer, you would have definitely come across the site http://www.jsonschema2pojo.org. Its a very useful site for generating Java POJO (Plain Old Java Objects) classes for your data models. It takes JSON or JSON schema and generates Java classes. Its especially useful for generating model classes for Cloud APIs along with frameworks like Retrofit + GSON. Its a very powerful tool and saves lot of time and effort in generating model classes (ask any iOS Developer 😃).

http://www.jsonschema2pojo.org

When I moved over to Kotlin from Java, I still retained the flow. I used to generate the POJO code using the site and paste it in Android Studio which converted it to Kotlin. This turned out to be a very inefficient process. Moreover the pasted code did not generate Kotlin Data classes which have many useful properties. Refer to the Kotlin documentation for more details about Data Classes here.

Another issue of not using Data Classes is that it is very easy and tempting to start adding Business logic in it. As described by Uncle Bob in his book Clean Code, this really not a good idea (Chapter 6 — Clean Code).

Objects hide their data behind abstractions and expose functions that operate on that data. Data structures expose their data and have no meaningful functions. We should be asking objects to do something, we should not be asking it about its internals.

Model classes (Data Transfer Object — DTO, as described in the book) should be a Data structure, not Object.

“The quintessential form of a data structure is a class with public variables and no functions. This is sometimes called a data transfer object, or DTO. DTOs are very useful struc- tures, especially when communicating with databases or parsing messages from sockets, and so on. They often become the first in a series of translation stages that convert raw data in a database into objects in the application code.”

So how do we easily generate Kotlin Data Classes from JSON? In comes the Android Studio Plugin — JsonToKotlinClass

It’s very easy to use. Download the plugin from the link above and install in Android Studio by following these steps (Mac) -

  1. Android Studio -> Preferences
  2. Select Plugins from side panel
  3. Click “Install plugin from disk”
  4. Select the jar file from the downloaded folder

And you are done!

To use the Plugin, create a new Kotlin file in Android Studio and then from the top menu, select “Code -> Generate” or click Command-N in the file, and select “Convert JSON into Kotlin class”

In the dialog box that comes up, provide the class name and paste the JSON in the text area. Click on “Format” to format the JSON

Go to “Settings” and you can select various properties like using val or var, nullable or not etc.

CLick on the “Annotation” tab and you can select the type of JSON parser. I use GSON, so I select that.

Click “OK” and “Make” and voila, the Data classes appear.

Now you can use these Data Classes to create ViewModels for your Views.

Don’t forget to 👏 if you like the artcle. Thanks for reading 😃

--

--