J2K means "Json sample to Kotlin".
J2K is simple Kotlin Gradle project. You have 2 options.
- Clone this repo and run J2K main method
- Use .jar
J2K have 3 args.
- path to json file
- Kotlin class name you want
- path to out file (optional)
First, make .json file you want to convert.
{
"id": "acb12345",
"users": [
{"id": 1000, "name": "joe"},
{"id": 1001, "name": "smith"}
],
"arrowed_type": [1,2,3],
"transaction_fee": 0.3
}
Next, run J2K as below
java -jar ./build/libs/J2K-1.0-SNAPSHOT.jar sample.json SampleClass
You see Kotlin data class in stdout.
import kotlin.Double
import kotlin.Long
import kotlin.String
import kotlin.collections.List
data class MyClass(
val id: String,
val users: List<User>,
val arrowedType: List<Long>,
val transactionFee: Double
)
data class User(
val id: Long,
val name: String
)
- if json object key is
aa_bb_cc
then class name for object isAaBbCc
. - if json array key is
aa_bb_ccs
then class name for element in array isAaBbCc
. - if json key is
aa_bb_ccs
then property name isaaBbCbc
. - if json value is null then property type is String.
- json number is converted to Long or Double.
- Kotlin
- Moshi
- KotlinPoet
Apache-2.0