JsonToKotlinClass

Introduction: A plugin for Kotlin to convert Json String into Kotlin data class code
能将 Json 数据直接映射生成 Kotlin Data Class 代码,免去手动编写数据对象结构声明
More: Author   ReportBugs   
Tags:
kotlin-json-plugin-data class-

JsonToKotlinClass

Build Status GitHub release GitHub stars JetBrains Plugin Download GitHub closed issues license

Kotlin IntelliJ Idea Plugin Android Studio Plugin

Hi, welcome to visit me! I am a plugin for generating Kotlin data class from JSON string, in another word, a plugin that converts JSON string to Kotlin data class (Json to Kotlin)

Overview

This is a very cool tool for Kotlin developers, it can convert a JSON string to Kotlin data class. The tool could not only recognize the primitive types but also auto create complex types. It's easily accessible, we provide shortcut keymap ALT + K for Windows and Option + K for Mac, have a try and you'll fall in love with it! JsonToKotlinClass just makes programming more enjoyable, enjoy coding!

Easy Use

alt text

Want to generate classes code in inner class model? Do like this.

alt text

Want to generate Kotlin data class file directory? Do like this.

alt text

Want to generate Kotlin data classes in single file directory? Do like this.

alt text

Usage

  • Search 'JsonToKotlinClass' in Intellij Idea Plugin Repository Or AndroidStudio Plugin Repository And Install it.

File --> Settings --> Plugins --> Browse Repositories --> Search JsonToKotlinClass

  • Restart your IDE

  • Press ALT + K for Windows or Option + K for Mac or right click on package -> New->Kotlin data clas file from JSON and continue as guided.

Advanced usage

Have a try with the settings dialog :stuck_out_tongue_winking_eye:

Features

  • Generating Kotlin data class from any legal JSON string or any URLs that returns a JSON string as response
  • Generating Kotlin data class from any legal JSON text when right click on directory and select New -> Kotlin data class File from JSON
  • Supporting (almostly) all kinds of JSON libs' annotation(Gson, Jackson, Fastjson, MoShi and LoganSquare, kotlinx.serialization(default custom value))
  • Customizing your own annotations
  • Initializing properties with default values
  • Allowing properties to be nullable(?)
  • Determining property nullability automatically
  • Renaming property names to be camelCase style when selecting a target JSON lib annotation.
  • Generating Kotlin data class as individual classes
  • Generating Kotlin data class as inner classes
  • Formatting any legal JSON string
  • Generating Map Type when json key is primitive type

Generate Example

  • Default
data class FD(
    val programmers: List<Programmer>,
    val authors: List<Author>,
    val musicians: List<Musician>
)

data class Musician(
    val firstName: String, 
    val lastName: String, 
    val instrument: String 
)

data class Author(
    val firstName: String, 
    val lastName: String, 
    val genre: String 
)

data class Programmer(
    val firstName: String, 
    val lastName: String, 
    val email: String 
)
  • Example with gson option on and init with default value option on in settings
data class TestData(
   @SerializedName("ticketInfo") val ticketInfo: TicketInfo = TicketInfo(),
   @SerializedName("trainInfo") val trainInfo: TrainInfo = TrainInfo(),
   @SerializedName("trainScheduleHead") val trainScheduleHead: List<String> = listOf(),
   @SerializedName("extInfo") val extInfo: ExtInfo = ExtInfo(),
   @SerializedName("trainScheduleBody") val trainScheduleBody: List<TrainScheduleBody> = listOf()
)

data class TrainScheduleBody(
   @SerializedName("mxl") val mxl: Long = 0, 
   @SerializedName("content") val content: List<Int> = listOf()
)

data class TrainInfo(
   @SerializedName("T110") val t110: T110 = T110()
)

Build From Source

Want to try out the newest features?

$ git clone https://github.com/wuseal/JsonToKotlinClass
$ cd JsonToKotlinClass
$ ./gradlew buildPlugin

And you're done! Go to directory build/distributions and you'll find JsonToKotlinClass-x.x.zip, which can be installed via Install plugin from disk....

Contribute to This Repo

Find it useful and want to contribute?

$ git clone https://github.com/wuseal/JsonToKotlinClass
$ cd JsonToKotlinClass

Open the build.gradle in IntelliJ, open "Gradle" tool window, expand the project view to "JsonToKotlinClass | Tasks | intellij | runIde", right-click and choose "Debug ...", and you're done! Create your PR here!

Chinese Detail Document (中文文档)

Others

  • Any kind of issues are welcome.
  • Pull Requests are highly appreciated.

Thanks

  • Thank @davidbilik for giving me the first awesome advice.
  • Thank @cgoodroe for opening many awesome issues for me, help me improve myself
  • Thank @wangzhenguang for reminding me of the details of the problem
  • Thank @kezhenxu94 for introducing CI/CD to save me a lot of time :)

Find it useful ? :heart:

  • Support and encourage me by clicking the :star: button on the upper right of this page. :v:
  • Share to others to help more people have a better develope expierience :heart:

Contact Community

Scan to join QQ Group

群   号:341755074

Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea