Skip to content

Instantly share code, notes, and snippets.

@TJC
Last active January 20, 2022 00:36
Show Gist options
  • Select an option

  • Save TJC/31819eb5464856d687f6 to your computer and use it in GitHub Desktop.

Select an option

Save TJC/31819eb5464856d687f6 to your computer and use it in GitHub Desktop.
non-working snakeyaml in scala with beanproperty
lazy val root = (project in file(".")).
settings(
name := "items",
version := "0.1",
scalaVersion := "2.11.7"
)
libraryDependencies += "org.yaml" % "snakeyaml" % "1.16"
javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-Xlint")
---
Name: Toby
Title: Hacker
---
Name: John
Title: Sir
---
Name: Adam
Title: Peon
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import collection.JavaConverters._
import beans.BeanProperty
/*
* https://bitbucket.org/asomov/snakeyaml/wiki/Documentation
*/
class Item {
@BeanProperty var Title: String = ""
@BeanProperty var Name: String = ""
override def toString = s"Title: $Title, Name: $Name"
}
object Foo extends App {
val reader = new java.io.FileReader("items.yaml")
val yaml = new Yaml(new Constructor(classOf[Item]))
val docs = yaml.loadAll(reader)// .asScala
val item = docs.iterator.next.asInstanceOf[Item]
println(item.getTitle)
}
@TJC
Copy link
Author

TJC commented Feb 18, 2016

Finally figured this one out.
I just needed to put @BeanInfo at the start of the class definition.
Nowhere, NOWHERE, mentioned this, and it was not present in any example code I found.

@pmuntianu
Copy link

Thank you a lot! Already spent couple of hours of searching.

@ledovsky
Copy link

Man, thanks you a lot! Also spent couple of hours figuring it out

@TJC
Copy link
Author

TJC commented Jan 20, 2022

For anyone coming to this recently -- I would now recommend using Circe and it's YAML parser rather than SnakeYaml.

import io.circe.yaml.parser
// plus the rest of regular Circe imports

  @ConfiguredJsonCodec
  case class FooBar(
      @JsonKey("blah_blah") blahBlah: String,
      otherThing: Option[Int]     
  )

  object FooBar { 
    implicit val circeConfig = CirceConfiguration.default
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment