Created
October 18, 2022 23:53
-
-
Save jpablo/93fccfd19558680a4ce57c52a0ced13b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> 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