Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create POJO Class for Kotlin

I want to create POJO class for Kotlin, as we know that www.jsonschema2pojo.org converts JSON to POJO so we can use it with gson.

Anyone know how to create Gson POJO for Kotlin QUICKLY?

Edited:

I know its use Data classes, but is there any simplest way to create it?

like image 986
Pratik Butani Avatar asked May 25 '17 12:05

Pratik Butani


People also ask

Is Kotlin data class POJO?

A data class is specified by the keyword data when declaring the class definition in Kotlin, it is like defining a pojo class in Java. The difference is that Kotlin will take care of all these getter and setter as well as equals and hashCode method for you.

How does Kotlin define POJO?

Kotlin has been developed over JVM and hence it is fully compatible with JVM. Java POJO class stands for Plain Old Java Object (POJO) which is used to hold the data. In Java, along with defining the variables, we need to create different supporting methods in order to access those private members of the class.


7 Answers

I think this should be the Plugin what you want

JSON To Kotlin Class Plugin

https://github.com/wuseal/JsonToKotlinClass

like image 198
吴海豹 Avatar answered Oct 08 '22 18:10

吴海豹


Yes, I got solution

for Example:

{
    "foo": "string",
    "bar": "integer",
    "baz": "boolean"
}

My POJO Class Created using http://www.jsonschema2pojo.org/

Example.java

public class Example {

    @SerializedName("foo")
    @Expose
    private String foo;
    @SerializedName("bar")
    @Expose
    private String bar;
    @SerializedName("baz")
    @Expose
    private String baz;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String getBaz() {
        return baz;
    }

    public void setBaz(String baz) {
        this.baz = baz;
    }
}

Converted Kotlin Class using Code -> Convert Java File to Kotlin File or CTRL + ALT + SHIFT + K

Example.kt

class Example {

    @SerializedName("foo")
    @Expose
    var foo: String? = null
    @SerializedName("bar")
    @Expose
    var bar: String? = null
    @SerializedName("baz")
    @Expose
    var baz: String? = null
}

Thank you all.

like image 26
Pratik Butani Avatar answered Oct 08 '22 19:10

Pratik Butani


A feature request about Kotlin support to auto generate data classes have been filled here in jsonschema2pojo github repository. Currently, there is no jsonschema2kotlin web utility available.

If you don't have any problem installing a new plugin on Android Studio, follow the accepted answer, otherwise the best you can do is to use jsonschema2pojo to convert JSON to Java POJO and the use the Android Studio 3.0+ feature that converts a Java file to a Kotlin one.

enter image description here

like image 22
MatPag Avatar answered Oct 08 '22 19:10

MatPag


data class ModelUser(val imagePath: String,val userName: String)

Unbelievable Right! Its as simple as that. Just use data keyword before class to create Data class in Kotlin.

Data class provides you with everything, getters, setters, hashCode, toString and equals functions. So all you have to do is create an instance and start using the functions.

like image 20
Kishan Solanki Avatar answered Oct 08 '22 17:10

Kishan Solanki


In vs-code there is a plugin named Paste JSON as Code. it supports many languages. Paste Json as code

quick look

like image 24
Manish Gowardipe Avatar answered Oct 08 '22 17:10

Manish Gowardipe


If I got your question, you might be searching some plugin for converting to POJO. So RoboPOJOGenerator may help you. You can use a plugin from File>Setting>Plugin>Browse Repositories and search for RoboPOJOGenerator. To use this plugin you first need to create a separate package like "data", right-click the package and you will see Generate POJO from JSON. Also, you need to include gson library in gradle because this plugin will automatically generate annotation of gson like @SerializedName, etc.

like image 29
Nabin Khatiwada Avatar answered Oct 08 '22 19:10

Nabin Khatiwada


Try this

This is the simple way

  1. Right click on the package name and select New->Kotlin File/Class enter image description here
  2. Name the name, In my case, I am naming this as Model you can whatever you like and click on ok enter image description here
  3. and Paste this code, This is you POJO/Model class

    class Model {
        var uid: String? = null
        var name: String? = null
    }
    

    enter image description here

How to use this

 val model=Model()
 model.name="Sunil"
 Log.e("Model after",model.name)

enter image description here

like image 40
Sunil Avatar answered Oct 08 '22 19:10

Sunil