You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a endpoint that streams a json-array (via akka json-streaming) (array, starting with [, then the elements, then ])
Usually, the stream returns elements of Type A (happy path). But the stream could also fail at one point and then return a single B as the last element and then the closing ].
I'm using Scala 2.13.
In Scala 3 I could use a union type to express this as List[A | B] ..
However, I'm currently stuck with scala 2.13, so as I cannot make A and B have the same supertype,
I thought about encoding this as a List[Either[A,B]] but this expects that the elements then
have a type field, e.g. its serializing the Either and I'm getting this error:
Is there any way to get the union type functionality in scala 2.13 ? e.g. a codec that first tries the JsonValueCodec[A] and if that fails tries JsonValueCodec[B] ?
Thanks!
The text was updated successfully, but these errors were encountered:
domdorn
changed the title
How to implement union types / List[Either[A,B]]
How to implement union types / List[Either[A,B]] in Scala 2.13 ?
Apr 26, 2024
Unfortunately, the current version of jsoniter-scala-macros doesn't support union types for Scala 3 and their simulation with Either[A, B] for Scala 2.
But you can write a custom codec that would be suitable for you case. The main challenge here is how to distinguish different types of A and B during JSON parsing efficiently. There are could be a couple of approaches with own pros and cons:
If A types have the same required field that should not exists in B types then we can mark current position with in.setMark(), scan for that discriminating field using in.skipToKey("field-name"), rollback to the marked position with in.rollbackToMark() and then decode with provided codecs for A or B type depending on the result of the skipToKey call.
If A types are less frequent than B then you can grab a raw values to a byte array with in.readRawValAsBytes() and then try to parse them as B in a try block catching a parsing error with subsequent re-parsing of that byte array as A type.
If JSON values of A and B types start from different tokens like " and { for for String and some case class accordingly then the 1st value token could be used as a discriminator to parse with the custom codec in one pass, like here.
Hello!
I have a endpoint that streams a json-array (via akka json-streaming) (array, starting with
[
, then the elements, then]
)Usually, the stream returns elements of Type
A
(happy path). But the stream could also fail at one point and then return a singleB
as the last element and then the closing]
.I'm using Scala 2.13.
In Scala 3 I could use a union type to express this as
List[A | B]
..However, I'm currently stuck with scala 2.13, so as I cannot make
A
andB
have the same supertype,I thought about encoding this as a
List[Either[A,B]]
but this expects that the elements thenhave a
type
field, e.g. its serializing theEither
and I'm getting this error:I created a small sample which would encode this and expectedly its encoding the either like this:
returns this json
Is there any way to get the union type functionality in scala 2.13 ? e.g. a codec that first tries the
JsonValueCodec[A]
and if that fails triesJsonValueCodec[B]
?Thanks!
The text was updated successfully, but these errors were encountered: