Skip to content

Latest commit

 

History

History
58 lines (43 loc) · 1.76 KB

README.md

File metadata and controls

58 lines (43 loc) · 1.76 KB

Scala Yaml

Example

Scala Yaml is a dependency-free Scala library that allows you to work with YAML.

The goal of this project is to create an idiomatic library which:

  • provides compile time type safety
  • semiautomatically derives codec instances
  • has explicit error handling and helpful error messages
  • cross compiles to Scala.js and Scala Native (waiting for Scala 3 support in SN)

Please see the guide for more information

Help us create our roadmap!

Take part in our discussions, post your ideas, vote for feature requests, and have a real impact on how our next milestone will look like!

Usage

Add as a dependency:

  • via sbt
libraryDependencies += "org.virtuslab" %% "scala-yaml" % "<version>"
  • via scala-cli
//> using dep org.virtuslab::scala-yaml:<version>

and use it in your code!

import org.virtuslab.yaml.*

case class Address(city: String, zipcode: String) derives YamlCodec
case class Person(name: String, age: Int, address: Address) derives YamlCodec

val yaml = s"""name: John Wick
              |age: 40
              |address:
              |  city: Anywhere
              |  zipcode: 12-345
              |""".stripMargin

val decoded = yaml.as[Person]
// Either[YamlError, Person] = Right(Person(John Wick,40,Address(Anywhere,12-345)))

case class Activity(kind: String, distance: Seq[Double]) derives YamlCodec

val activity = Activity("running", Seq(5.37, 4.98, 5.73))
val encoded  = activity.asYaml
//kind: running
//distance: 
//  - 5.37
//  - 4.98
//  - 5.73