Intermittently can't get CBOR decoding to compile....
# serialization
t
Intermittently can't get CBOR decoding to compile. I get this error a lot
Type mismatch: inferred type is ByteArray but DeserializationStrategy<TypeVariable(T)> was expected
on this line of code
Cbor.decodeFromByteArray<InBoundMsg>( bb )
where bb is a byte array
but other places in my code I do the exact same thing and it works fine.
also sometimes if I just copy and pate & dleate the line a few times the compiler is ok again
WTF?
v
I guess you miss some import when it does not work, where an extension function with that signature is imported. Maybe with deleting and readding your ide inserts the missing import
t
yeah. I think your right.
Does the CBOR stuff work at all?
I just did a simple test where I encode and decode a simple message and the decoder blows up. 1-RC
v
I'd guess so as it is one of the officially supported formats, but it is experimental afair, but personally I only used JSON and YAML so far
t
@TwoClocks what do you mean by "blows up"? There are unit tests in
kotlinx.serialization
for simple CBOR messages that verify it does work.
t
I have a some core that serializes to a byte array, and then decodes the same byte array. the decoder throws a exception.
t
Just caught up on the channel discussion, it looks like you figured it out?
t
yeah. it's kinda a gotcha and not discussed in the docs or sample code at all.
it's sorta understandable, but not really.
I mean, it knows the subtype is part of a sealed class (cuz it throws a exception if it's not), yet it won't serialize to the base type unless cast.
t
I'm not super proficient with the polymorphism features but the code snippets you pasted show you encoding one type then trying to decode to a different type. That doesn't seem right?
t
it's pretty much the common way to send different types of messages down the same wire.
the only other way I know to do it is to have a another message that tells you the type of the next message... and that's kinda ugly.
t
Ya. I see your point. That is strange it doesn't include the type info for a single element. Just throwing ideas at the wall here, but does it work to specify
BaseClass
as the type when encoding?
t
yeah. if pass
TypeOne("yay") as BaseClass
to the encoder, it works fine. or have a list of more than one type of
BaseClass
t
Was thinking
encodeToByteArray<BaseClass>(TypeOne("yay"))
seems cleaner?
t
It would, but I can't get that to compile.
t
What about?
Copy code
val obj: BaseClass = TypeOne("yay")
encodeToByteArray(obj)
t
strange, the
<>
works for the cbor encoder, but I had problems w/ the json one... odd.
t
¯\_(ツ)_/¯
t
the <> works too (just ran it)... yeah, that's better. it's also clearer when I'm trying to do.
thanks!
the imports are really fluky. If I don't import
serialization.cbor.*
things get weyrd. like if export the encode & decode individually. I get strange errors.