Last active
January 20, 2022 00:36
-
-
Save TJC/31819eb5464856d687f6 to your computer and use it in GitHub Desktop.
non-working snakeyaml in scala with beanproperty
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| Name: Toby | |
| Title: Hacker | |
| --- | |
| Name: John | |
| Title: Sir | |
| --- | |
| Name: Adam | |
| Title: Peon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
Thank you a lot! Already spent couple of hours of searching.
Man, thanks you a lot! Also spent couple of hours figuring it out
Author
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
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.