Create POJO Class for Kotlin
Asked Answered
P

10

44

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?

Pronounced answered 25/5, 2017 at 12:14 Comment(2)
In Kotlin you write data classes instead of POJOs.Myosotis
If you are using Android Studio I think you can create Java classes and then convert it to Kotlin.Pyotr
P
21

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.

Pronounced answered 25/5, 2017 at 12:54 Comment(3)
You did exactly what i described in my answer but i received negative votes instead ^^Anticipate
you can use json2kotlin.com to generate kotlin data classes directly from json instead of needing a java conversion in between.Amends
What about getter and setter in the kotlin case? And cant the variables be private? Please add a note on how to access those variable ( say bar)Zoochemistry
T
77

I think this should be the Plugin what you want

JSON To Kotlin Class Plugin

https://github.com/wuseal/JsonToKotlinClass

Twopence answered 29/8, 2017 at 6:32 Comment(3)
Update: Great plugin,But the studio gets stuck after the conversion(android studio version 2.3.3)Disposure
@Amalp Is that occurs every time ,I have just test ,everything goes well,can you paste your JsonString here?Or you can raise an detail issue here,github.com/wuseal/JsonToKotlinClass/issues ThanksChrystal
@Amalp Thanks for your issue,new released version has fixed this problem,you can try with the new versionChrystal
P
21

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.

Pronounced answered 25/5, 2017 at 12:54 Comment(3)
You did exactly what i described in my answer but i received negative votes instead ^^Anticipate
you can use json2kotlin.com to generate kotlin data classes directly from json instead of needing a java conversion in between.Amends
What about getter and setter in the kotlin case? And cant the variables be private? Please add a note on how to access those variable ( say bar)Zoochemistry
A
10

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

Anticipate answered 25/5, 2017 at 12:20 Comment(2)
Negative votes without explanations, nice job guys ;)Anticipate
maybe they are afraid of revenge downvotes.Baribaric
M
10
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.

Moccasin answered 7/2, 2018 at 11:2 Comment(2)
A data class do not have a default constructor.Formulate
Certain JSON converters require classes to be constructible, but the previous comment is actually correct: one can use data classes with default parametersFormulate
S
6

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

quick look

Sponsor answered 16/2, 2019 at 6:8 Comment(0)
Y
5

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.

Yearlong answered 25/5, 2017 at 16:20 Comment(0)
T
1

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 name it whatever you like) and click OK. enter image description here

  3. Paste this code, this is your 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

Turin answered 31/7, 2018 at 11:56 Comment(0)
C
0

Use the Android Studio or IntelliJ IDEA plugin: JSON To Kotlin Class ​(JsonToKotlinClass)​

Crudity answered 30/12, 2020 at 10:55 Comment(0)
A
0

In my case Code -> Generate doesn't work, it is disabled (see screenshot)

enter image description here

You should install the plugin "JsonToKotlinClass"

Install plugin for Android Studio

Then right-click on the namespace and select

Then right-click on the namespace and select

Paste your JSON here. That's all, profit.

enter image description here

Americanize answered 18/1, 2021 at 18:30 Comment(0)
E
-5
data class VideoGame(val name: String, val publisher: String, var reviewScore: Int)
//Constructor 
val game: VideoGame = VideoGame("Gears of War", "Epic Games", 8)

print(game.name) // "Gears of War"
print(game.publisher) // "Epic Games"
print(game.reviewScore) // 8
game.reviewScore = 7
Efren answered 25/5, 2017 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.