Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 28, 2023 06:45
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 dacr/d601ebc41a0b1c828969532964a6872c to your computer and use it in GitHub Desktop.
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/ce13a78cb527b443c646a6838d329619a55c43d3
// 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.2.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