problem mapping nested object in reactivemongo/Play

45 views
Skip to first unread message

Stefano Tondo

unread,
Sep 28, 2016, 9:21:36 AM9/28/16
to ReactiveMongo - http://reactivemongo.org
Hi all,

I'm having an hard time figuring out how to properly map this model:

package models

import reactivemongo.bson.{
  BSONDateTime,
  BSONDocument,
  BSONObjectID
}

case class PixelDictModel(k: List[Int], v: Int)

object PixelDictModel {
  import play.api.libs.json._

  implicit object PixelDictWrites extends OWrites[PixelDictModel] {
    def writes(pixelDictModel: PixelDictModel): JsObject = Json.obj(
      "k" -> pixelDictModel.k,
      "v" -> pixelDictModel.v)
  }

  implicit object PixelDictModelReads extends Reads[PixelDictModel] {
    def reads(json: JsValue): JsResult[PixelDictModel] = json match {
      case obj: JsObject => try {
        val k = (obj \ "k").as[List[Int]]
        val v = (obj \ "v").as[Int]

        JsSuccess(PixelDictModel(k, v))

      } catch {
        case cause: Throwable => JsError(cause.getMessage)
      }

      case _ => JsError("expected.jsobject")
    }
  }
}

In my Play controller I'm parsing it correctly: val pixelDict = (request.body \ PixelDict).as[List[PixelDictModel]]

but when I pass it to my repository like that:

frameRepo.save(BSONDocument(
      Name -> name,
      Description -> description,
      Size -> BSONDocument("width" -> size.width, "height" -> size.height),
      Palette -> palette,
      Theater -> theater,
      Thumbnail -> thumbnail,
      PixelDict -> BSONArray(pixelDict) )).map(result => Created) 
  }

I'm getting this error:

  • Read from stdout: Compiling 3 Scala sources to /home/stondo/dev/scala/play-scala-mongo/target/scala-2.11/classes...
  • Compiling 3 Scala sources to /home/stondo/dev/scala/play-scala-mongo/target/scala-2.11/classes...
  • Read from stdout: /home/stondo/dev/scala/play-scala-mongo/app/controllers/FrameController.scala:56: overloaded method value apply with alternatives:
  • Read from stdout: (stream: Stream[scala.util.Try[reactivemongo.bson.BSONValue]])reactivemongo.bson.BSONArray <and>
  • Read from stdout: (elements: Traversable[reactivemongo.bson.BSONValue])reactivemongo.bson.BSONArray <and>
  • Read from stdout: (elements: reactivemongo.bson.Producer[reactivemongo.bson.BSONValue]*)reactivemongo.bson.BSONArray
  • Read from stdout: cannot be applied to (List[models.PixelDictModel])
  • /home/stondo/dev/scala/play-scala-mongo/app/controllers/FrameController.scala:56: overloaded method value apply with alternatives: (stream: Stream[scala.util.Try[reactivemongo.bson.BSONValue]])reactivemongo.bson.BSONArray <and> (elements: Traversable[reactivemongo.bson.BSONValue])reactivemongo.bson.BSONArray <and> (elements: reactivemongo.bson.Producer[reactivemongo.bson.BSONValue]*)reactivemongo.bson.BSONArray cannot be applied to (List[models.PixelDictModel])
  • Read from stdout: PixelDict -> BSONArray(newFrame.pixelDict) )).map(result => Created)
  • PixelDict -> BSONArray(newFrame.pixelDict) )).map(result => Created)
  • Read from stdout: ^
  • ^
  • Read from stdout: one error found
  • one error found
  • Read from stdout: (compile:compileIncremental) Compilation failed
  • (compile:compileIncremental) Compilation failed
  • Total time: 1 s, completed Sep 28, 2016 3:19:01 PM
 
Tried everything I could think of, but couldn't get it to work.
Any clues would be very much appreciated.
Thanks,

Stefano Tondo 

Cédric Chantepie

unread,
Sep 28, 2016, 2:31:52 PM9/28/16
to ReactiveMongo - http://reactivemongo.org
Hi,

As indicated in the error message, the BSONArray factory "cannot be applied to (List[models.PixelDictModel])" (and the indicated code is not the one causing this error).

It's recommanded to have a look at the API documentation.

Either you first convert the element of the list to BSONValues, to create a BSONArray from a Traversable[BSONValue], or call the vararg factory BSONArray(values: _*).

Stefano Tondo

unread,
Sep 29, 2016, 8:55:27 AM9/29/16
to reacti...@googlegroups.com
Thanks for your answer and explanation Cédric.

I ended up changing the whole thing, using import play.api.libs.json.Reads._ instead, but I'll also try what u suggested.

--
You received this message because you are subscribed to a topic in the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reactivemongo/VKyhm89Z0BI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reactivemongo+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages