Skip to content

Commit

Permalink
Support Kotlin 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
SlashNephy committed May 14, 2021
1 parent c3c1222 commit 295ce17
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Json.kt: Json bindings for Kotlin Multiplatform
[![Kotlin](https://img.shields.io/badge/Kotlin-1.4.30-blue.svg)](https://kotlinlang.org)
[![Kotlin](https://img.shields.io/badge/Kotlin-1.5-blue.svg)](https://kotlinlang.org)
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/StarryBlueSky/Json.kt)](https://github.com/StarryBlueSky/Json.kt/releases)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/StarryBlueSky/Json.kt/Check)](https://github.com/StarryBlueSky/Json.kt)
[![license](https://img.shields.io/github/license/StarryBlueSky/Json.kt)](https://github.com/StarryBlueSky/Json.kt/blob/master/LICENSE)
[![issues](https://img.shields.io/github/issues/StarryBlueSky/Json.kt)](https://github.com/StarryBlueSky/Json.kt/issues)
[![pull requests](https://img.shields.io/github/issues-pr/StarryBlueSky/Json.kt)](https://github.com/StarryBlueSky/Json.kt/pulls)

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

現時点では JVM (Android), JS target に対応しています。 (各種 Native target に対応予定です)
JVM (Android) and JS targets are supported for now. (We'll support Native targets later.)
現時点では JVM (Android), JS target に対応しています。
At present, it supports Java 8 or later, Android, and JavaScript target.

ドキュメントは [こちら](https://starrybluesky.github.io/Json.kt/jsonkt/) で公開しています。
Documentations are published at [here](https://starrybluesky.github.io/Json.kt/jsonkt/).
Documentation is available at [here](https://starrybluesky.github.io/Json.kt/jsonkt/).


```kotlin
Expand Down Expand Up @@ -59,13 +59,14 @@ private val json = """{
fun main() {
val model = json.parseObject { Model(it) }

println(model.a == 1) // true
assertEquals(1, model.a)
}
```

# Get Started

We moved the repository to Maven Central. So you can use our library without adding extra repository.
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)](https://img.shields.io/github/v/release/StarryBlueSky/Json.kt)](https://github.com/StarryBlueSky/Json.kt/releases)

Expand All @@ -77,6 +78,6 @@ dependencies {

# License

Json.kt is provided under MIT License.
Json.kt is provided under the MIT License.

Copyright (c) 2017-2021 StarryBlueSky.
14 changes: 10 additions & 4 deletions src/commonMain/kotlin/blue/starry/jsonkt/delegation/JsonModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ private fun JsonModel.defaultJsonKeyConverter(property: KProperty<*>): String {
return when (keyCase) {
JsonKeyCase.Unspecified -> property.name
JsonKeyCase.LowerCamelCase -> {
property.name.decapitalize()
property.name.replaceFirstChar { it.lowercase() }
}
JsonKeyCase.UpperCamelCase -> {
property.name.capitalize()
property.name.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase()
} else {
it.toString()
}
}
}
JsonKeyCase.SnakeCase -> {
buildString {
for (c in property.name) {
if (c == c.toUpperCase()) {
if (c == c.uppercaseChar()) {
if (length > 0) {
append('_')
}
append(c.toLowerCase())
append(c.lowercaseChar())
} else {
append(c)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import blue.starry.jsonkt.cli.property.*
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import java.util.*
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.collections.set
Expand All @@ -39,7 +40,7 @@ fun generateModelClass(): String {
print("Model name? (Optional): ")
val modelName = readLine().orEmpty()
print("Use type strict mode? (Y/n): ")
val printComments = readLine().orEmpty().toLowerCase() == "y"
val printComments = readLine().orEmpty().lowercase(Locale.getDefault()) == "y"
println("Input json string. If blank line is input, quit.")

while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
package blue.starry.jsonkt.cli.property

import blue.starry.jsonkt.JsonElement
import java.util.*

internal abstract class AbstractJsonModelProperty(pair: Map.Entry<String, JsonElement>, printComments: Boolean): AbstractJsonProperty(pair, printComments) {
val modelName = key.toSafeKotlinLiteral().toLowerCamelCase().capitalize()
val modelName = key.toSafeKotlinLiteral().toLowerCamelCase()
.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import blue.starry.jsonkt.JsonElement
import blue.starry.jsonkt.JsonObject
import blue.starry.jsonkt.JsonPrimitive
import kotlinx.serialization.json.*
import java.util.*

internal abstract class AbstractJsonProperty(pair: Map.Entry<String, JsonElement>, private val printComments: Boolean) {
val key = pair.key
Expand Down Expand Up @@ -61,7 +62,7 @@ internal abstract class AbstractJsonProperty(pair: Map.Entry<String, JsonElement
else -> throw IllegalStateException("Unknown type: ${jsonPrimitive::class.qualifiedName}.")
}.let {
if (nullable) {
"nullable${it.capitalize()}"
"nullable${it.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }}"
} else {
it
}
Expand All @@ -70,7 +71,9 @@ internal abstract class AbstractJsonProperty(pair: Map.Entry<String, JsonElement

fun String.toLowerCamelCase(): String {
val part = split("_")
return part.first().decapitalize() + part.drop(1).joinToString("") { it.capitalize() }
return part.first().replaceFirstChar { it.lowercase(Locale.getDefault()) } + part.drop(1).joinToString("") {
it.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}
}

fun String.toSafeKotlinLiteral(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ open class CachingReadOnlyProperty<in R, out T> internal constructor(
actual class JsonDelegateProperty<out T> actual constructor(
internal actual val key: String?,
initializer: PropertyInitializer<T>
): ReadOnlyProperty<Any?, T> by CachingReadOnlyProperty<Any?, T>(initializer)
): ReadOnlyProperty<Any?, T> by CachingReadOnlyProperty(initializer)

0 comments on commit 295ce17

Please sign in to comment.