Json to Kotlin Data class
Asked Answered
D

5

7

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)
}
Dynamite answered 7/9, 2014 at 14:27 Comment(3)
Maybe you will find it interesting: youtrack.jetbrains.com/issue/KT-5672, Kotlin module for Jackson, supporting immutable objectsStonge
Can you send me a link to jar file without Maven?Dynamite
search.maven.org/…Stonge
C
5

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)

Choppy answered 16/10, 2014 at 10:34 Comment(0)
R
4

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>
Rhoda answered 7/12, 2015 at 18:23 Comment(2)
Current version is 2.6.4-1 for Kotlin beta 4Choppy
Link to the 1-page docs for this module: github.com/FasterXML/jackson-module-kotlinChoppy
A
0

Why not use Jackson or any other serializer? It should work..

Ator answered 7/9, 2014 at 17:15 Comment(0)
S
0

What about this This is a translator that translate JSON string into kotlin data class ,it make it throught a plugin,see next a demo usage https://plugins.jetbrains.com/plugin/9960-jsontokotlinclass

Shock answered 22/9, 2017 at 11:3 Comment(0)
M
0

http://www.json2kotlin.com converts your json response to kotlin data classes online, without the need to install any plugin. Optionally, you can generate gson annotations too. (Disclosure: I created this utility)

Mitsue answered 19/11, 2017 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.