Could you provide example how to parse _entire_ object to case class ? #1
Answered
by
alaz
unoexperto
asked this question in
Q&A
-
Could you provide example how to parse entire object to case class ? I couldn't figure out it from documentation. Consider following case class case class MenuItem(id : Int, name: String, desc: Option[String], prices: Option[Array[String]], subitems: Option[Array[MenuItem]])
object MenuItem {
implicit val asBson = BsonWritable[MenuItem](item =>
{
val buf: DBObjectBuffer = DBO("id" -> item.id, "name" -> item.name)
item.desc match { case Some(value) => buf.append("desc" -> value) case None => }
item.prices match { case Some(value) => buf.append("prices" -> value) case None => }
item.subitems match { case Some(value) => buf.append("subitems" -> value) case None => }
buf()
}
)
} and sample code import com.osinka.subset._
val item: MenuItem = MenuItem(2, "Pasta", Option("tasty"), Option(Array("$15")), None)
val buffer = DBO("ymenu" -> item)
val dbo = buffer()
println(dbo)
import DocParser._
implicit val jodaDateTime = Field[MenuItem]({
case item: MenuItem => new MenuItem(item.id, item.name, item.desc, item.prices, item.subitems)
})
val obj: DocParser[MenuItem] = get[MenuItem]("ymenu")
dbo match {
case obj(value) => print(value)
} How exactly Field[MenuItem] should be defined ? |
Beta Was this translation helpful? Give feedback.
Answered by
alaz
May 16, 2013
Replies: 2 comments
-
Duplicate of Stackoverflow question. I'll answer there. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
alaz
-
Hi Alexander ! I just switched to Scala from C++. Thank you for your help. I'll be waiting for an answer on SO. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Duplicate of Stackoverflow question. I'll answer there.
Not a bug.