json5k icon indicating copy to clipboard operation
json5k copied to clipboard

how to convert json5String to JsonElement

Open lisonge opened this issue 1 year ago • 3 comments

Thank you for your open source work

But I encountered some problems

val name = "kotlinx.serialization.json.JsonElementSerializer"
val JsonElementSerializer = Class.forName(name).kotlin.objectInstance as KSerializer<JsonElement>
Json5.decodeFromString(JsonElementSerializer, "{a:1}")

but I get error

java.lang.IllegalStateException: This serializer can be used only with Json format.Expected Decoder to be JsonDecoder, got class io.github.xn32.json5k.deserialization.MainDecoder

lisonge avatar Apr 16 '24 11:04 lisonge

The JSON element serializer only supports the KxS JSON encoder/decoder. It doesn't support json5

KP2048 avatar May 21 '24 20:05 KP2048

Also either use the inline versions of the functions or use JsonElement.serializer(), don't use reflection here

KP2048 avatar May 21 '24 20:05 KP2048

I need to convert a JSON5 string to a JSON string, and then convert this JSON string to a JsonElement.

So is it possible to use this library to directly convert a JSON5 string to a JsonElement?

lisonge avatar May 22 '24 02:05 lisonge

https://github.com/lisonge/kotlin-json5

lisonge avatar Aug 14 '24 04:08 lisonge