Last active
May 25, 2024 10:19
-
-
Save dacr/d601ebc41a0b1c828969532964a6872c to your computer and use it in GitHub Desktop.
ZIO learning - learning&testing zio-json features / published by https://github.com/dacr/code-examples-manager #3cfd0c94-0e36-4ddf-848d-5de9dba757d5/3696b90ddf21d4c5dc1693a0b5bb51d46df159a9
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
// summary : ZIO learning - learning&testing zio-json features | |
// keywords : scala, zio, learning, json, pure-functional, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 3cfd0c94-0e36-4ddf-848d-5de9dba757d5 | |
// created-on : 2021-04-09T17:24:04Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "dev.zio::zio:2.0.13" | |
//> using dep "dev.zio::zio-json:0.5.0" | |
//> using options "-Yretain-trees" // When case classes are using default values | |
// --------------------- | |
import zio.* | |
import zio.json.* | |
import java.time.Instant | |
case class Address( | |
town: String, | |
country: String, | |
planet: Option[String] | |
) derives JsonCodec | |
case class Person( | |
name: String, | |
age: Int, | |
birthDate: Instant, | |
addresses: List[Address] | |
) derives JsonCodec | |
object Encapsulated extends ZIOAppDefault: | |
val json = | |
"""{ | |
| "name":"joe", | |
| "age":42, | |
| "birthDate":"2021-04-09T17:19:17.000Z", | |
| "addresses":[ {"town":"london","country":"uk"} ] | |
|}""".stripMargin | |
def run = | |
for | |
person <- ZIO.fromEither(json.fromJson[Person]) | |
_ <- Console.printLine(person.toJsonPretty) | |
yield () | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment