jp.nephy:jsonkt

Json bindings for Kotlin.


Keywords
json, json-model, json-parser, kotlin, kotlin-multiplatform
License
MIT

Documentation

Json.kt: Json bindings for Kotlin Multiplatform

Kotlin GitHub release (latest by date) GitHub Workflow Status license issues pull requests

委譲プロパティを使い, 直感的に JSON を Kotlin のクラスに変換できます。
Using delegated properties, you can intuitively convert JSON to Kotlin classes.

現時点では JVM (Android), JS target に対応しています。
At present, it supports Java 8 or later, Android, and JavaScript target.

ドキュメントは こちら で公開しています。
Documentation is available at here.

data class Model(override val json: JsonObject): JsonModel {
    val a by int
    val b by float
    val c by string
    val d by intList
    val e by model { E(it) }
    val f by modelList { E(it) }

    data class E(override val json: JsonObject): JsonModel {
        val x by nullableString
        val y by double
        val z by int
    }
}

private val json = """{
    "a": 1,
    "b": 2.3,
    "c": "hoge",
    "d": [2, 3, 5, 7],
    "e": {
        "x": "1",
        "y": 2.0,
        "z": 3
    },
    "f": [
        {
            "x": "1",
            "y": 2.0001,
            "z": 3
        },
        {
            "x": null,
            "y": 20.00001,
            "z": 30
        }
    ]
}"""

fun main() {
    val model = json.parseObject { Model(it) }

    assertEquals(1, model.a)
}

Get Started

Json.kt はバージョン 6.0.2 から Maven Central で公開されています。以前の Bintray リポジトリはもう利用できません。
Json.kt is now available in the Maven Central since version 6.0.2. The previous Bintray repository is no longer available.

GitHub release (latest by date)

dependencies {
    implementation("blue.starry:jsonkt:$JsonKtVersion")
}

License

Json.kt is provided under the MIT License.

Copyright (c) 2017-2021 StarryBlueSky.