Update 2025-05-26
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
plugins{
|
||||
alias(libs.plugins.kotlin.jvm)
|
||||
alias(libs.plugins.kotlinx.serialization)
|
||||
alias(libs.plugins.ktor)
|
||||
`maven-publish`
|
||||
// `maven-publish`
|
||||
}
|
||||
|
||||
group = "center.sciprog.demo"
|
||||
@@ -19,6 +20,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.kotlinx.html)
|
||||
// implementation(libs.kotlin.css)
|
||||
implementation(libs.logback.classic)
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
[versions]
|
||||
kotlin-version = "2.0.21"
|
||||
kotlin-version = "2.1.20"
|
||||
kotlinx-html-version = "0.11.0"
|
||||
ktor-version = "3.0.1"
|
||||
logback-version = "1.4.14"
|
||||
androidx-lifecycle = "2.8.4"
|
||||
compose-multiplatform = "1.7.3"
|
||||
junit = "4.13.2"
|
||||
kotlin = "2.1.20"
|
||||
kotlinx-coroutines = "1.10.2"
|
||||
|
||||
[libraries]
|
||||
ktor-server-core = { module = "io.ktor:ktor-server-core-jvm", version.ref = "ktor-version" }
|
||||
@@ -23,8 +28,17 @@ ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor-ve
|
||||
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor-version" }
|
||||
ktor-client-apache = { module = "io.ktor:ktor-client-apache", version.ref = "ktor-version" }
|
||||
ktor-client-websockets = { module = "io.ktor:ktor-client-websockets", version.ref = "ktor-version" }
|
||||
kotlinx-serialization-json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1"
|
||||
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
androidx-lifecycle-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel", version.ref = "androidx-lifecycle" }
|
||||
androidx-lifecycle-runtime-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-runtime-compose", version.ref = "androidx-lifecycle" }
|
||||
kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
|
||||
|
||||
[plugins]
|
||||
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-version" }
|
||||
ktor = { id = "io.ktor.plugin", version.ref = "ktor-version" }
|
||||
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin-version" }
|
||||
compose-multiplatform = { id = "org.jetbrains.compose", version.ref = "compose-multiplatform" }
|
||||
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
|
||||
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||
|
||||
33
settings.gradle.kts
Normal file
33
settings.gradle.kts
Normal file
@@ -0,0 +1,33 @@
|
||||
rootProject.name = "idiomaticKotlin"
|
||||
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
|
||||
|
||||
pluginManagement {
|
||||
repositories {
|
||||
google {
|
||||
mavenContent {
|
||||
includeGroupAndSubgroups("androidx")
|
||||
includeGroupAndSubgroups("com.android")
|
||||
includeGroupAndSubgroups("com.google")
|
||||
}
|
||||
}
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositories {
|
||||
google {
|
||||
mavenContent {
|
||||
includeGroupAndSubgroups("androidx")
|
||||
includeGroupAndSubgroups("com.android")
|
||||
includeGroupAndSubgroups("com.google")
|
||||
}
|
||||
}
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
include(":compose")
|
||||
27
src/main/kotlin/serialization/ComplexSerialization.kt
Normal file
27
src/main/kotlin/serialization/ComplexSerialization.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package serialization
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
|
||||
@Serializable
|
||||
class ComplexSerializationModel(
|
||||
val arg1: String,
|
||||
val arg2: Int
|
||||
) {
|
||||
val lazyProp by lazy { arg1.uppercase() }
|
||||
|
||||
@Transient
|
||||
var transientProp: String? = "aaa"
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val serialized = Json {
|
||||
encodeDefaults = true
|
||||
}.encodeToString(
|
||||
ComplexSerializationModel.serializer(),
|
||||
ComplexSerializationModel("a", 1)
|
||||
)
|
||||
println(serialized)
|
||||
}
|
||||
25
src/main/kotlin/serialization/ManualSerializer.kt
Normal file
25
src/main/kotlin/serialization/ManualSerializer.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package serialization
|
||||
|
||||
object ManualSerializer {
|
||||
fun serialize(marks: MarksList): String =
|
||||
marks.marks.entries.joinToString(", ", prefix = "{ ", postfix = "}\n") { (key, value) ->
|
||||
"\"${key}\": $value"
|
||||
}
|
||||
|
||||
fun deserialize(string: String): MarksList {
|
||||
TODO()
|
||||
}
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val markList = MarksList(
|
||||
mapOf(
|
||||
"A A" to 1u,
|
||||
"B B" to 2u
|
||||
)
|
||||
)
|
||||
|
||||
val serialized = ManualSerializer.serialize(markList)
|
||||
|
||||
println(serialized)
|
||||
}
|
||||
14
src/main/kotlin/serialization/StudentGroup.kt
Normal file
14
src/main/kotlin/serialization/StudentGroup.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package serialization
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Student(val name: String, val group: String)
|
||||
|
||||
@Serializable
|
||||
class StudentGroup(val id: String, val students: Set<Student>)
|
||||
|
||||
typealias Mark = UByte
|
||||
|
||||
@Serializable
|
||||
data class MarksList(val marks: Map<String, Mark>)
|
||||
25
src/main/kotlin/serialization/autoSeriualization.kt
Normal file
25
src/main/kotlin/serialization/autoSeriualization.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package serialization
|
||||
|
||||
import kotlinx.serialization.builtins.serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
fun main() {
|
||||
val markList = MarksList(
|
||||
mapOf(
|
||||
"A A" to 1u,
|
||||
"B B" to 2u
|
||||
)
|
||||
)
|
||||
|
||||
val json = Json {
|
||||
prettyPrint = true
|
||||
}
|
||||
|
||||
val string = json.encodeToString(MarksList.serializer(), markList)
|
||||
|
||||
println(string)
|
||||
|
||||
val reconstructed = json.decodeFromString(MarksList.serializer(), string)
|
||||
|
||||
println(reconstructed == markList)
|
||||
}
|
||||
51
src/main/kotlin/serialization/issLocation.kt
Normal file
51
src/main/kotlin/serialization/issLocation.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package serialization
|
||||
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Serializer
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
//http://api.open-notify.org/iss-now.json
|
||||
|
||||
/*
|
||||
{
|
||||
"timestamp": 1744048325,
|
||||
"message": "success",
|
||||
"iss_position": {
|
||||
"longitude": "5.4810",
|
||||
"latitude": "7.7084"
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@Serializable
|
||||
data class ISSPosition(val longitude: Double, val latitude: Double)
|
||||
|
||||
@Serializable
|
||||
class OpenNotifyISSLocation(
|
||||
val timestamp: Long,
|
||||
val message: String,
|
||||
@SerialName("iss_position") val issPosition: ISSPosition,
|
||||
)
|
||||
|
||||
suspend fun main() = coroutineScope {
|
||||
val client = HttpClient(CIO){
|
||||
|
||||
}
|
||||
|
||||
val response = client.get("http://api.open-notify.org/iss-now.json")
|
||||
|
||||
val body = response.bodyAsText()
|
||||
|
||||
val deserialized = Json.decodeFromString<OpenNotifyISSLocation>(body)
|
||||
|
||||
println(deserialized.message)
|
||||
|
||||
println(deserialized.issPosition)
|
||||
}
|
||||
Reference in New Issue
Block a user