Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Created January 18, 2012 12:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bdarfler/1632845 to your computer and use it in GitHub Desktop.
Save bdarfler/1632845 to your computer and use it in GitHub Desktop.
Scala Snake YAML
import scalaj.collection.Imports._
import org.yaml.snakeyaml.Yaml
def parse(str: String) = {
new Yaml().load(str) match {
case obj: ArrayList[_] =>
val seqOfMaps = obj.asScala.collect {
case hashMap: HashMap[_, _] =>
hashMap.asScala.toMap.asInstanceOf[Map[String, Any]]
}.toSeq
if (seqOfMaps.isEmpty) None else Some(seqOfMaps)
case obj: HashMap[_, _] =>
Some(Seq(obj.asScala.toMap.asInstanceOf[Map[String, Any]]))
case obj: Any =>
None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment