Fudge
08/13/2019, 10:07 AMNamedValueEncoder
/ NamedValueDecoder
.
class MyClass {
/** **/
}
Doing that in a ElementValueEncoder
is simple enough :
class MyEncoder : ElementValueEncoder(){
fun encodeMyClass(obj : MyClass){
/** **/
}
}
// And then in serializer...
@Serializer(forClass = MyClass)
object MyClassSerializer : KSerializer<MyClass> {
override fun serialize(encoder: Encoder, obj: MyClass) {
if(encoder is MyEncoder){
encoder.encodeMyClass(obj)
}
else error("not supported")
}
}
However, I cannot see how I can do the same thing in a NamedValueEncoder
, as it requires a tag, which I don't know how to generate.
class MyTaggedEncoder : NamedValueEncoder(){
fun encodeTaggedMyClass(tag: String, obj : MyClass){
/** **/
}
}
// And then in serializer...
@Serializer(forClass = MyClass)
object MyClassSerializer : KSerializer<MyClass> {
override fun serialize(encoder: Encoder, obj: MyClass) {
if(encoder is MyTaggedEncoder){
encoder.TaggedMyClass( ??????, obj)
}
else error("not supported")
}
}
I don't have access to the methods that generate the tag, to be used in the area marked as "?????", as they are private in NamedValueEncoder
. There are also no methods that allow Any
class to be encoded in NamedValueEncoder
(of which I don't need to provide a tag myself to) . So how can I encode / decode a tagged MyClass
in this situtation?sandwwraith
08/13/2019, 3:45 PMMyClass
? I think the right way to do it would be to check instance/class descriptor in the encodeSerializableValue
function: https://github.com/Kotlin/kotlinx.serialization/blob/master/runtime/commonMain/src/kotlinx/serialization/protobuf/ProtoBuf.kt#L61Fudge
08/13/2019, 5:40 PM