Nikky
09/11/2018, 8:06 AMenleur
09/11/2018, 8:17 AMNikky
09/11/2018, 8:20 AMDeactivated User
09/11/2018, 8:20 AM@Serializable
annotation?
https://github.com/Kotlin/kotlinx.serialization
I would expect that to work out of the box. But since I haven’t tried yet, maybe it is not the case.
Also there is a #serialization channel for doubts/feedback (if the issue is not related to ktor directly)Nikky
09/11/2018, 8:21 AMfun write(data: Any): OutgoingContent
is just not enough type info 😛Nikky
09/11/2018, 8:25 AMDeactivated User
09/11/2018, 8:26 AMkotlinx.serialization
alone but not the client feature.
Looking at the code, I guess that makes sense.Deactivated User
09/11/2018, 8:26 AMDeactivated User
09/11/2018, 8:29 AMdata::class
to get a class instance and then get the type to serialize?Nikky
09/11/2018, 8:30 AMDeactivated User
09/11/2018, 8:31 AMDeactivated User
09/11/2018, 8:31 AMNikky
09/11/2018, 8:31 AMDeactivated User
09/11/2018, 8:31 AM[]
directly? without being wrapped by an object? That’s valid json?Nikky
09/11/2018, 8:32 AMDeactivated User
09/11/2018, 8:32 AMNikky
09/11/2018, 8:33 AMDeactivated User
09/11/2018, 8:34 AMNikky
09/11/2018, 8:37 AM@Serializable class ListWrapper(val list: List<AddonFile>)
val content = client.get<String>(url)
return json.parse<ListWrapper>("{ \"list\": $content }").list
e5l
09/11/2018, 8:38 AMNikky
09/11/2018, 8:41 AMe5l
09/11/2018, 8:43 AMgson
serializer.Nikky
09/11/2018, 8:53 AMDeactivated User
09/11/2018, 8:54 AMclazz.serializer
here and it worked https://github.com/mmo-poc/mmo-poc/blob/2d7d82d9bec0ef95c93bb90447caf20e3a0d6038/mmo/common/src/mmo/protocol/serial.kt#L17
but maybe I’m missing somethingNikky
09/11/2018, 8:58 AMNikky
09/11/2018, 9:20 AMhttps://i.imgur.com/oIooQaC.png▾
Deactivated User
09/11/2018, 9:21 AMDeactivated User
09/11/2018, 9:21 AMNikky
09/11/2018, 9:21 AMNikky
09/11/2018, 9:22 AMDeactivated User
09/11/2018, 9:24 AMDeactivated User
09/11/2018, 9:25 AMfun <T : Any> serializePacket(obj: T, clazz: KClass<T>): String {
//return JSON.stringify(Packet(clazz.serialName, JSON.stringify(clazz.serializer(), obj)))
return serializePacket(obj)
}
fun serializePacket(obj: Any): String {
val clazz: KClass<*> = obj::class
val serializer = clazz.serializer()
@Suppress("UNCHECKED_CAST")
return JSON.stringify(Packet(clazz.serialName, JSON.stringify(serializer as SerializationStrategy<Any>, obj)))
}
it compiles at least. (Though not testest)Deactivated User
09/11/2018, 9:27 AMNikky
09/11/2018, 9:29 AMDeactivated User
09/11/2018, 9:31 AMkotlinx-serialization-runtime
Nikky
09/11/2018, 9:32 AMDeactivated User
09/11/2018, 9:32 AMDeactivated User
09/11/2018, 9:33 AMJSON.stringify
arguments and cast to whatever it requests, it works because of the type erasureDeactivated User
09/11/2018, 9:33 AMNikky
09/11/2018, 9:33 AMKSerializer
works then i hopeDeactivated User
09/11/2018, 9:37 AMval clazz: KClass<Any> = obj::class as KClass<Any>
val serializer = clazz.serializer()
the problem is that obj::class
returns KClass<out T>
and you don’t need an out generic, but the specific one. Since the generic is erased, it is ok.Nikky
09/11/2018, 9:37 AMNikky
09/11/2018, 9:39 AMNikky
09/11/2018, 9:52 AMNikky
09/11/2018, 9:52 AMNikky
09/11/2018, 10:03 AMDeactivated User
09/11/2018, 10:10 AMas KSerializer<Any>
to the second trick: val clazz: KClass<Any> = obj::class as KClass<Any>
since KClass
is way more stable API than KSerializer
. But that’s a minor observationbdawg.io
09/11/2018, 1:31 PM::class
so you would only have a List<*>
which isn't much better than Any IMOsandwwraith
09/11/2018, 2:01 PM::class.serializer()
will likely work for plain user types, but not for generic containers (like List<T>), because there are no information about T to get serializer for it. I suggest to use mentioned TypeInfo in conjuction with this method: https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/jvm/src/main/kotlin/kotlinx/serialization/Resolving.kt#L60Nikky
09/11/2018, 2:04 PMNikky
09/11/2018, 2:11 PMsandwwraith
09/11/2018, 2:22 PM