JsonToKotlinClass
Hi, Welcome! This is a plugin to generate Kotlin data class
from JSON string, in another word, a plugin that converts JSON string to Kotlin data class
(Json to Kotlin).
We also have a Java/Kotlin
library. With this you can generate Kotlin data class
from JSON string programmatically.
implementation 'com.sealwu.jsontokotlin:library:3.7.4'
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
Want to generate classes code in inner class model? Do like this.
Want to generate Kotlin data class file? Do like this.
Want to generate Kotlin data classes in single file? Do like this.
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 orOption + 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 advanced dialog :stuck_out_tongue_winking_eye:
Features
- Generating Kotlin data class from any legal JSON string/JSONSchema or any URLs that returns a JSON string/JSONSchema as response or local file that contains JSON string/JSONSchema, for more detail about supported source types, refer to the documentation
- 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
- Android Parcelable support
- Customize parent class declaration
- Generating kotlin data class property order by alphabetical
- Support customizing this plugin by
Extensions
module
Generate Example
This is the example JSON from json.org
{
"glossary":{
"title":"example glossary",
"GlossDiv":{
"title":"S",
"GlossList":{
"GlossEntry":{
"ID":"SGML",
"SortAs":"SGML",
"GlossTerm":"Standard Generalized Markup Language",
"Acronym":"SGML",
"Abbrev":"ISO 8879:1986",
"GlossDef":{
"para":"A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso":[
"GML",
"XML"
]
},
"GlossSee":"markup"
}
}
}
}
}
And with this plugin converting, Kotlin data classes would generate like this by default
data class Example(
val glossary: Glossary
)
data class Glossary(
val GlossDiv: GlossDiv,
val title: String
)
data class GlossDiv(
val GlossList: GlossList,
val title: String
)
data class GlossList(
val GlossEntry: GlossEntry
)
data class GlossEntry(
val Abbrev: String,
val Acronym: String,
val GlossDef: GlossDef,
val GlossSee: String,
val GlossTerm: String,
val ID: String,
val SortAs: String
)
data class GlossDef(
val GlossSeeAlso: List<String>,
val para: String
)
Change Log
- Change log since 3.0.0
- Change log 1.x.x
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? All sorts of contributions are welcome, including but not limited to:
-
Open an issue here if you find a bug;
-
Help test the EAP version and report bugs:
Go to the âPluginsâ settings, click âBrowse repositoriesâŚâ => âManage repositoriesâŚâ and click the â+â button to add the EAP channel repository URL âhttps://plugins.jetbrains.com/plugins/eap/listâ. Optionally, you can also add the Alpha and Beta channel repository URLs âhttps://plugins.jetbrains.com/plugins/alpha/listâ and âhttps://plugins.jetbrains.com/plugins/beta/listâ.
Kindly note that the âEAPâ or âAlphaâ or âBetaâ channel update may be unstable and tend to be buggy, if you want to get back to the stable version, remove the âEAPâ or âAlphaâ or âBetaâ version and reinstall this plugin from the âJetBrains Plugin Repositoryâ channel, which can be filtered by the drop-down menu next to the search input field.
- Contribute your code:
$ 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 (ä¸ćć楣)
- http://blog.csdn.net/wuseal/article/details/77508585
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 :)
- Thank iqbalhood for contributing logo for this project
- Thank akindone for adding
order by alphabetical
feature forJsonToKotlinClass
- Thank rafalbednarczuk for adding
make keyword property valid
feature forJsonToKotlinClass
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:
Authors
This project exists thanks to all the people who contribute.
Acknowledgement
The development of this plugin is powerly driven by JetBrains.
Join the Community
| Letâs talk | | âââ | | Join JsonToKotlinClass Slack| | Join JsonToKotlinClass Telegram |
Noteďź
We analytics anonymous user behavior for improving plugin production, weâve provided a switch case for that.