Skip to content

Commit

Permalink
1.0.1 > 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
FiLKoNd committed Nov 9, 2024
1 parent 3697cf3 commit 16655b1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ subprojects {
apply(plugin = "maven-publish")

group = "com.filkond"
version = "1.0.1"
version = "1.0.2"

repositories {
mavenCentral()
Expand Down Expand Up @@ -78,8 +78,8 @@ subprojects {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/FiLKoNd/paperKtLib")
credentials {
username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.filkond.paperktlib.config.serializers

import kotlinx.serialization.KSerializer
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*

private fun Any?.toJsonPrimitive(): JsonPrimitive {
return when (this) {
null -> JsonNull
is JsonPrimitive -> this
is Boolean -> JsonPrimitive(this)
is Number -> JsonPrimitive(this)
is String -> JsonPrimitive(this)
else -> throw Exception("Unsupported type: ${this::class}")
}
}

private fun JsonPrimitive.toAnyValue(): Any? {
val content = this.content
if (this.isString) {
return content
}
if (content.equals("null", ignoreCase = true)) {
return null
}
if (content.equals("true", ignoreCase = true)) {
return true
}
if (content.equals("false", ignoreCase = true)) {
return false
}
content.toIntOrNull()?.let { return it }
content.toLongOrNull()?.let { return it }
content.toDoubleOrNull()?.let { return it }
throw Exception("Unknown value:${content}")
}

object AnyValueSerializer : KSerializer<Any?> {
private val delegateSerializer = JsonPrimitive.serializer()
override val descriptor = delegateSerializer.descriptor
override fun serialize(encoder: Encoder, value: Any?) {
encoder.encodeSerializableValue(delegateSerializer, value.toJsonPrimitive())
}

override fun deserialize(decoder: Decoder): Any? {
val jsonPrimitive = decoder.decodeSerializableValue(delegateSerializer)
return jsonPrimitive.toAnyValue()
}
}

/**
* Convert Any? to JsonElement
*/
private fun Any?.toJsonElement(): JsonElement {
return when (this) {
null -> JsonNull
is JsonElement -> this
is Boolean -> JsonPrimitive(this)
is Number -> JsonPrimitive(this)
is String -> JsonPrimitive(this)
is Iterable<*> -> JsonArray(this.map { it.toJsonElement() })
// !!! key simply converted to string
is Map<*, *> -> JsonObject(this.map { it.key.toString() to it.value.toJsonElement() }.toMap())
// add custom convert
else -> throw Exception("Unknown value ${this::class}=${this}}")
}
}

private fun JsonElement.toAnyOrNull(): Any? {
return when (this) {
is JsonNull -> null
is JsonPrimitive -> toAnyValue()
is JsonObject -> this.map { it.key to it.value.toAnyOrNull() }.toMap()
is JsonArray -> this.map { it.toAnyOrNull() }
}
}

object AnySerializer : KSerializer<Any?> {
private val delegateSerializer = JsonElement.serializer()
override val descriptor = delegateSerializer.descriptor
override fun serialize(encoder: Encoder, value: Any?) {
encoder.encodeSerializableValue(delegateSerializer, value.toJsonElement())
}

override fun deserialize(decoder: Decoder): Any? {
val jsonPrimitive = decoder.decodeSerializableValue(delegateSerializer)
return jsonPrimitive.toAnyOrNull()
}
}
2 changes: 1 addition & 1 deletion paperktlib-plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PaperKtLib
name: paperKtLib
version: "${version}"
main: com.filkond.paperktlib.plugin.PaperKtLib
author: FiLKoNd
Expand Down

0 comments on commit 16655b1

Please sign in to comment.