Hi, I'm trying to deserialize a generic type `Eith...
# serialization
m
Hi, I'm trying to deserialize a generic type
Either<L, R>
(which means it contains either
L
or
R
) with the following logic:
Copy code
override fun deserialize(decoder: Decoder): Either<L, R> =
        try {
            rightSerializer.deserialize(decoder).right()
        } catch (e: Exception) {
            leftSerializer.deserialize(decoder).left()
        }
• If the given JSON payload is meant for type
R
, everything works well. • If the given JSON payload is meant for type
L
, the exception is caught, however the cursor in the decoder has already progressed (and thus fails to deserialize) Is there a way to reset the decoder back to its original cursor position? Am I approaching this the wrong way? Are there alternatives? Thank you in advance.
e
untested but something along these lines may work
💯 2
m
It works! ❤️
j
@ephemient is it possible to do this one without using a
Json
? that implies that default properties provided outside here are missing
I reply myself, yes, it is possible:
Copy code
decoder.json.decodeFromJsonElement(serializer, decoder.decodeJsonElement())
should be great a way to do this without forcing the
Json
usage and allowing all implementations to work
e
good call, edited
j
do you know any way to get the previous cursor?
e
not sure what you mean by that
j
I would like to resolve this issue without forcing the consumer to use Json format
e
can't be done in other formats
well, it should be possible with hocon by treating it like json, but cbor, properties, and protobuf do not provide any way to grab potential elements without also parsing and potentially failing at the same time
👍 1