Question

I have a case class like this

case class Wish(_id: Option[String], title : String, text :String, cash: Int, created_at:    Option[DateTime], updated_at : Option[DateTime])

Also I have defined a implicit reads validator as below

implicit val wishFormat = Json.format[Wish]

I am trying to read a Mongodb document into my wish class and I get error like below

scala> val js = "{\"_id\":{\"$oid\":\"5259c384dd8251bb085adfb4\"},\"title\":\"Shrikar\",\"text\":\"test\",\"cash\":12.0,\"created_at\":1381614468235,\"updated_at\":1381614468235}"
js: String = {"_id":{"$oid":"5259c384dd8251bb085adfb4"},"title":"Shrikar","text":"test","cash":12.0,"created_at":1381614468235,"updated_at":1381614468235}

scala> val test = Json.parse(js)
test: play.api.libs.json.JsValue = {"_id":{"$oid":"5259c384dd8251bb085adfb4"},"title":"Shrikar","text":"test","cash":12.0,"created_at":1381614468235,"updated_at":1381614468235}

scala> test.validate[Wish]
res11: play.api.libs.json.JsResult[Wish] =   JsError(List((/_id,List(ValidationError(validate.error.expected.jsstring,WrappedArray())))))

Could you please help me resolve this issue?

Was it helpful?

Solution 2

The reads format converts a play.api.libs.json.JsValue to an object. Reads is expecting a json value that looks like this:

import play.api.libs.json.{Json, JsValue}
val js: JsValue = Json.obj(
  "_id" -> "5259c384dd8251bb085adfb4",
  "title" -> "Shrikar",
  "text" -> "test",
  "cash" -> 12.0,
  "created_at" -> 1381614468235,
  "updated_at" -> 1381614468235
)

You should start by taking a look at the Playframework working with JSON documentation. If you are using a library such as ReactiveMongo, then mongodb queries should return a BSONDocument or JsValue. According to the Reactive Mongo docs:

With Play2-ReactiveMongo, you can use directly the embedded JSON library in Play >= 2.1. There is a specialized collection called JSONCollection that deals naturally with JSValue and JSObject instead of ReactiveMongo's BSONDocument.

If you can only receive mongodb query results as String, then you'll need to create a function to parse it into a JsValue.

I hope this helps!

OTHER TIPS

Your ID must be a BSONObjectID.

case class Wish(
  _id: Option[BSONObjectID],
  title: String,
  text: String,
  cash: Int,
  created_at: Option[DateTime],
  updated_at: Option[DateTime]
)

Then you must import the format from the Play-ReactiveMongo plugin:

import play.modules.reactivemongo.json.BSONFormats._
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top