Skip to content

Instantly share code, notes, and snippets.

@jpablo
Created October 18, 2022 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpablo/93fccfd19558680a4ce57c52a0ced13b to your computer and use it in GitHub Desktop.
Save jpablo/93fccfd19558680a4ce57c52a0ced13b to your computer and use it in GitHub Desktop.
//> using scala "3.2"
//> using lib "dev.zio::zio-json:0.3.0"
import zio.json.*
import zio.json.ast.Json
type T1 = (String, Int)
type T2 = (Int, String)
given JsonDecoder[T1 | T2] =
JsonDecoder[Json].mapOrFail(j => j.as[T1] orElse j.as[T2])
@main def main =
val Right(lst) = """[[1, "a"], ["b", 2]]""".fromJson[List[T1 | T2]]: @unchecked
assert {
lst == List((1, "a"), ("b", 2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment