Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json to Kotlin Data class

Is there a way and/or library to automatically create Kotlin Data class from Json like it is works in Scala Json.Spray?

Something like this:

data class User(id: Int, name: String)

class DataClassFactory(val json: String) {
   fun getUser(): User {
      //some reflection
      return User(10, "Kirill")
  }
}

fun main(args: Array<String>): Unit {
  val json = "{id: 10, name: Kirill}"
  val usr = DataClassFactory(json).getUser()
  println(usr)
}
like image 347
Kirill Ozeretskovsky Avatar asked Sep 07 '14 14:09

Kirill Ozeretskovsky


People also ask

How do I create a data class from JSON in Kotlin?

Plugin generates Kotlin data classes from JSON text. It can find inner classes in nested JSON. You can disable undesirable fields in class, change field name, set it's type to optional, specify default value and add annotations for popular json libraries.

Does Kotlin support JSON?

You can use JSON to Kotlin Data class converter plugin in Android Studio for JSON mapping to POJO classes (kotlin data class). This plugin will annotate your Kotlin data class according to JSON. Then you can use GSON converter to convert JSON to Kotlin.


2 Answers

You can use the Jackson module for Kotlin to serialize/deserialize easily from any format that Jackson supports (including JSON). This is the easiest way, and supports Kotlin data classes without annotations. See https://github.com/FasterXML/jackson-module-kotlin for the module which includes the latest information for using from Maven and Gradle (you can infer IVY and download JARs from the Maven repositories as well)

Alternatives exist such as Boon, but it has no specific support for Kotlin (usually a problem with not having a default constructor) and uses some unsafe direct access to internal JVM classes for performance. In doing so, it can crash on some VM's, and in cases where you extend Boon from Kotlin with custom serializer/deserializer it makes assumptions about the classes that do not hold true in Kotlin (the String class is wrapped for example) which I have seen core dump. Boon is lightening fast, just be careful of these issues and test first before using.

(note: I am the creator of the Jackson-Kotlin module)

like image 192
Jayson Minard Avatar answered Sep 28 '22 08:09

Jayson Minard


This is very clean and easy in Kotlin.

import com.fasterxml.jackson.module.kotlin.*

data class User(val id: Int, val name: String)

fun main(args: Array<String>) {
    val mapper = jacksonObjectMapper()
    val json = """{"id": 10, "name": "Kirill"}"""
    val user = mapper.readValue<User>(json)
    println(user)
}

produces this output:

User(id=10, name=Kirill)

you only have to add this to your pom.xml

    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-kotlin</artifactId>
        <version>2.6.3-4</version>
    </dependency>
like image 29
pabl0rg Avatar answered Sep 28 '22 09:09

pabl0rg