Table of Contents
Union Types 🔗
In Scala 3 you can use union types in the form A | B | C.
Use case: Some APIs support "expanding objects". E.g. they either return a
Stringwhich is ID of that object, or full objectTwhen you send a query stringexpand=myObject. For this you can leverage union types in natural way:someKey: String | MyObject.
When parsing, Tupson will try all types (left to right) until it finds one that parses correctly.
When writing, Tupson will use its runtime type to determine how to write the value.
"1".parseJson[Int | String | Boolean]
// 1: Int
""" "bla" """.parseJson[Int | String | Boolean]
// "bla": String