Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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/f92831465e23d38b431658ce24140592d04947b7
// 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