Kotlin Data Class from Json using GSON

We Are Going To Discuss About Kotlin Data Class from Json using GSON. So lets Start this Java Article.

Kotlin Data Class from Json using GSON

  1. Kotlin Data Class from Json using GSON

    Data class:
    data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String, @SerializedName("description") val description: String )

  2. Kotlin Data Class from Json using GSON

    Data class:
    data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String, @SerializedName("description") val description: String )

Solution 1

Data class:

data class Topic(
  @SerializedName("id") val id: Long, 
  @SerializedName("name") val name: String, 
  @SerializedName("image") val image: String,
  @SerializedName("description") val description: String
)

to JSON:

val gson = Gson()
val json = gson.toJson(topic)

from JSON:

val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)
Advertisements

Original Author Anton Holovin Of This Content

Solution 2

Based on answer of Anton Golovin

Details

  • Gson version: 2.8.5
  • Android Studio 3.1.4
  • Kotlin version: 1.2.60

Solution

Create any class data and inherit JSONConvertable interface

interface JSONConvertable {
     fun toJSON(): String = Gson().toJson(this)
}

inline fun <reified T: JSONConvertable> String.toObject(): T = Gson().fromJson(this, T::class.java)

Usage

Data class

data class User(
    @SerializedName("id") val id: Int,
    @SerializedName("email") val email: String,
    @SerializedName("authentication_token") val authenticationToken: String) : JSONConvertable

From JSON

val json = "..."
val object = json.toObject<User>()

To JSON

val json = object.toJSON()
Advertisements

Original Author Vasily Bodnarchuk Of This Content

Solution 3

You can use similar in Kotlin class

class InventoryMoveRequest {
    @SerializedName("userEntryStartDate")
    @Expose
    var userEntryStartDate: String? = null
    @SerializedName("userEntryEndDate")
    @Expose
    var userEntryEndDate: String? = null
    @SerializedName("location")
    @Expose
    var location: Location? = null
    @SerializedName("containers")
    @Expose
    var containers: Containers? = null
}

And also for nested class you can use same like if there is nested object. Just provide Serialize name for the Class.

@Entity(tableName = "location")
class Location {

    @SerializedName("rows")
    var rows: List<Row>? = null
    @SerializedName("totalRows")
    var totalRows: Long? = null

}

so if get response from the server each key will map with JOSN.

Alos, convert List to JSON:

val gson = Gson()
val json = gson.toJson(topic)

ndroid convert from JSON to Object:

val json = getJson()
val topic = gson.fromJson(json, Topic::class.java)
Advertisements

Original Author Pawan Soni Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment