Paulius Ruminas
04/03/2019, 7:28 AMCan't locate argument-less serializer for class kotlinx.serialization.json.JsonObject. For generic classes, such as lists, please provide serializer explicitly.
@Serializable
class Foo {
@Serializer(forClass = Foo::class)
companion object : KSerializer<Foo> {
override fun serialize(encoder: Encoder, obj: Foo) {
encoder.encode(JsonObject::class.serializer(), JsonObject(mapOf("foo" to JsonLiteral("bar"))))
}
override fun deserialize(decoder: Decoder): Foo {
TODO()
}
}
}
fun main() {
Json.stringify(Foo.serializer(), Foo())
}
Nikky
04/03/2019, 7:30 AMNikky
04/03/2019, 7:31 AM@Serializable(with=Foo.Companion::class)
Paulius Ruminas
04/03/2019, 7:36 AM@Serializable(with = FooSerializer::class)
class Foo(val bar: String)
@Serializer(forClass = Foo::class)
object FooSerializer : KSerializer<Foo> {
override fun serialize(encoder: Encoder, obj: Foo) {
encoder.encode(JsonObject::class.serializer(), JsonObject(mapOf("bar" to JsonLiteral("value"))))
}
override fun deserialize(decoder: Decoder): Foo {
TODO()
}
}
fun main() {
Json.stringify(FooSerializer, Foo("value"))
}
I'm still getting the same errorNikky
04/03/2019, 8:00 AM{ "bar", "value" }
?
then i think you should use encoder.startStructure
and .encodeString
and finish it with .endStructure
sandwwraith
04/03/2019, 9:07 AMJsonObjectSerializer
instead of JsonObject::class.serializer()
Paulius Ruminas
04/03/2019, 10:06 AMJsonObjectSerializer
is internal i can not use itPaulius Ruminas
04/03/2019, 10:18 AM{"type": "X", "object": {"bar":"value"}}
. I do not want to add any extra classes to achieve this serialization.Nikky
04/03/2019, 12:40 PMval jsonOutput = encoder as JsonOutput
jsonOutput.encodeJson(JsonObject(mapOf("bar" to JsonLiteral("value"))))
Nikky
04/03/2019, 12:41 PM{"bar":"value"}
{
bar: value
}
{bar:value}
Nikky
04/03/2019, 12:41 PMNikky
04/03/2019, 12:41 PMNikky
04/03/2019, 12:53 PMas?
and add some errorhandling in case it get called from something other than Json()
and..
@sandwwraith is JsonOutput
the correct type here ? or would there be something more appropritate ?Nikky
04/03/2019, 12:55 PMval jsonInput: JsonInput = decoder as JsonInput
val element: JsonElement = jsonInput.decodeJson()
Nikky
04/03/2019, 12:57 PMPaulius Ruminas
04/03/2019, 1:05 PMPaulius Ruminas
04/03/2019, 1:41 PM