-
-
Save DSeeLP/c5055f616ad0b7e4337cdadd24d1c4e5 to your computer and use it in GitHub Desktop.
Kotlinx.serialization error
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
package io.github.dseelp.test | |
import kotlinx.serialization.KSerializer | |
import kotlinx.serialization.Serializable | |
import kotlinx.serialization.SerializationException | |
import kotlinx.serialization.builtins.serializer | |
import kotlinx.serialization.decodeFromString | |
import kotlinx.serialization.descriptors.SerialDescriptor | |
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | |
import kotlinx.serialization.encoding.Decoder | |
import kotlinx.serialization.encoding.Encoder | |
import kotlinx.serialization.json.Json | |
fun main() { | |
val json = Json { | |
ignoreUnknownKeys = true | |
coerceInputValues = true | |
encodeDefaults = true | |
} | |
val inputText = """ | |
{ | |
"error": null, | |
"value": { | |
"user": { | |
"id": 123456789101112, | |
"name": "Test123", | |
"discriminator": 1234, | |
"avatarHash": "exampleTest", | |
"permissions": [ | |
] | |
} | |
} | |
} | |
""".trimIndent() | |
println(json.decodeFromString<RestResult<UserResponse>>(inputText)) | |
println(json.decodeFromString<RestResult<Unit>>(inputText)) | |
} | |
@Serializable | |
data class RestResponse<T>(val error: String? = null, val value: T? = null) | |
@Serializable(RestResultSerializer::class) | |
sealed class RestResult<T> | |
@Serializable | |
data class FailedRestResult<T>(val error: String) : RestResult<T>() | |
@Serializable | |
data class FineRestResult<T>(val value: T) : RestResult<T>() | |
class RestResultSerializer<T>(private val dataSerializer: KSerializer<T>): KSerializer<RestResult<T>> { | |
override fun deserialize(decoder: Decoder): RestResult<T> { | |
val response = decoder.decodeSerializableValue(RestResponse.serializer(dataSerializer)) | |
when { | |
response.error == null && response.value == null -> throw SerializationException("") | |
response.value != null && response.error != null -> throw SerializationException("") | |
response.error != null -> return FailedRestResult(response.error!!) | |
response.value != null -> return FineRestResult(response.value!!) | |
} | |
throw SerializationException() | |
} | |
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("RestResult") { | |
element("error", String.serializer().descriptor, isOptional = true) | |
element("response", dataSerializer.descriptor, isOptional = true) | |
} | |
override fun serialize(encoder: Encoder, value: RestResult<T>) { | |
val response = when (value) { | |
is FailedRestResult -> RestResponse<T>(value.error, null) | |
is FineRestResult -> RestResponse(null, value.value) | |
} | |
encoder.encodeSerializableValue(RestResponse.serializer(dataSerializer), response) | |
} | |
} | |
@Serializable | |
data class User( | |
val id: Long, | |
val name: String, | |
val discriminator: Int, | |
val avatarHash: String?, | |
val permissions: Array<Int> | |
) { | |
override fun equals(other: Any?): Boolean { | |
if (this === other) return true | |
if (other !is User) return false | |
if (id != other.id) return false | |
return true | |
} | |
override fun hashCode(): Int { | |
return id.hashCode() | |
} | |
} | |
@Serializable | |
data class UserResponse(val user: io.github.dseelp.framecord.rest.data.objects.User) |
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
FineRestResult(value=UserResponse(user=User(id=123456789101112, name=Test123, discriminator=1234, avatarHash=exampleTest, permissions=[]))) | |
Exception in thread "main" kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 43: Expected end of the object '}', but had ' ' instead | |
JSON input: .....: null, | |
"value": { | |
"user": { | |
"id": 1..... | |
at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24) | |
at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32) | |
at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:514) | |
at kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:219) | |
at kotlinx.serialization.json.internal.AbstractJsonLexer.unexpectedToken(AbstractJsonLexer.kt:202) | |
at kotlinx.serialization.json.internal.StringJsonLexer.consumeNextToken(StringJsonLexer.kt:70) | |
at kotlinx.serialization.json.internal.StreamingJsonDecoder.endStructure(StreamingJsonDecoder.kt:59) | |
at kotlinx.serialization.internal.ObjectSerializer.deserialize(ObjectSerializer.kt:43) | |
at kotlinx.serialization.internal.UnitSerializer.deserialize(Primitives.kt) | |
at kotlinx.serialization.internal.UnitSerializer.deserialize(Primitives.kt:79) | |
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59) | |
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:35) | |
at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43) | |
at kotlinx.serialization.encoding.AbstractDecoder.decodeNullableSerializableElement(AbstractDecoder.kt:79) | |
at io.github.dseelp.framecord.rest.client.jvm.RestResponse$$serializer.deserialize(Debug.kt:39) | |
at io.github.dseelp.framecord.rest.client.jvm.RestResponse$$serializer.deserialize(Debug.kt:39) | |
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59) | |
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:35) | |
at io.github.dseelp.framecord.rest.client.jvm.RestResultSerializer.deserialize(Debug.kt:54) | |
at io.github.dseelp.framecord.rest.client.jvm.RestResultSerializer.deserialize(Debug.kt:52) | |
at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:59) | |
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:35) | |
at kotlinx.serialization.json.Json.decodeFromString(Json.kt:100) | |
at io.github.dseelp.framecord.rest.client.jvm.DebugKt.main(Debug.kt:104) | |
at io.github.dseelp.framecord.rest.client.jvm.DebugKt.main(Debug.kt) | |
Process finished with exit code 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment