I have a Scala application that needs to read in a Yaml file and load it into a Scala case class. I'm using snakeyaml to do the job, but it seems that the load function can't use my case class's constructor. Below is an example of what I'm doing...
Yaml File:
"name": "Jon"
"age": 50
"gender": "male"
Case Class:
final case class Person(
name: String,
age: Int,
gender: String
)
Code to extract and load Yaml:
val text: String = scala.io.Source.fromFile(file).mkString
val yaml = new Yaml(new Constructor(implicitly[ClassTag[T]].runtimeClass))
yaml.load(text).asInstanceOf[T]
I get the following error:
org.yaml.snakeyaml.constructor.ConstructorException: Can't construct a java object for tag:yaml.org,2002:case_classes.Person; exception=java.lang.InstantiationException: NoSuchMethodException:case_classes.Person.<init>()
Please let me know if you see something wrong or if you can recommend a better approach.
Aucun commentaire:
Enregistrer un commentaire