Scala Parser for YAML File


import java.io.{File, FileInputStream}
import java.text.SimpleDateFormat
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor

object Main {
def main(args: Array[String]): Unit = {
val text = scala.io.Source.fromInputStream(getClass.getResourceAsStream(
"codes.yaml")).mkString
val yaml = new Yaml
val obj = yaml.load(text)
val codes = obj.asInstanceOf[java.util.LinkedHashMap[String,java.util.Map[String, Any]]]
var values = codes.get("duration").get("values").asInstanceOf[java.util.ArrayList
[java.util.Map[String,Any]]]
values.toArray.foreach(c=> println(c.asInstanceOf[java.util.LinkedHashMap[String,Any]]
.get("code")))
}
}

codes.yaml:

duration:
values:
-
code: 1
from: 0
to: 1
-
code: 2
from: 1
to: 3

build.sbt

libraryDependencies += "org.yaml" % "snakeyaml" % "1.8"

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s