Last active
February 3, 2026 20:18
-
-
Save dacr/e6425a4dba3d5106ebfa33f80720fb98 to your computer and use it in GitHub Desktop.
ZIO learning - learning&testing zio-schemas features / published by https://github.com/dacr/code-examples-manager #735e9391-0449-4f38-9d67-f4f86105a395/7a1db98d29d4d5b25b2ed22cf1366a56a7022b92
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-schemas features | |
| // keywords : scala, zio, learning, schema, pure-functional, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 735e9391-0449-4f38-9d67-f4f86105a395 | |
| // created-on : 2022-11-25T15:46:03+01:00 | |
| // 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-schema:0.4.11" | |
| //> using dep "dev.zio::zio-schema-derivation:0.4.11" | |
| //> using dep "dev.zio::zio-schema-protobuf:0.4.11" | |
| //> using dep "dev.zio::zio-schema-json:0.4.11" | |
| // --------------------- | |
| import zio.* | |
| import zio.ZIO.* | |
| import zio.schema.{DeriveSchema, Schema} | |
| import zio.schema.codec.JsonCodec.* | |
| import zio.schema.syntax.* | |
| //import zio.prelude.* | |
| import java.time.Instant | |
| import java.nio.charset.Charset | |
| case class Address(town: String, country: String, planet: Option[String]) | |
| case class Person(name: String, age: Int, birthDate: Instant, addresses: List[Address]) | |
| object Person { | |
| given schema: Schema[Person] = DeriveSchema.gen | |
| } | |
| 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 { | |
| _ <- logInfo(s"experimenting...") | |
| decoder = jsonDecoder[Person](Person.schema) | |
| person <- from(decoder.decodeJson(json)) | |
| _ <- Console.printLine(person.toString) | |
| } yield () | |
| } | |
| Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment