-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/serializers/AnySerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters