Last active
May 6, 2023 15:39
Star
You must be signed in to star a gist
ZIO learning - learning&testing zio-schemas features / published by https://github.com/dacr/code-examples-manager #735e9391-0449-4f38-9d67-f4f86105a395/f92831465e23d38b431658ce24140592d04947b7
This file contains 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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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.2.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