1

I'm receiving an JSON graph (payload) from the client, so at the web api I get a JsValue. I simply want to take that, decorate it with a couple of fields and store it in Mongo. Something like this:

case class Plan(_id: ObjectId, name: String, payload: JsValue)

{
  "_id" = 12345,
  "name" : "test model",
  "payload" : {a JSON graph}
}

From JsValue to database...

builder += "payload" -> JSON.parse(Json.stringify(model.payload))

From database back to a JsValue...

payload = Json.parse(dbo.as[MongoDBList]("payload").toString))

While it works to go from JsValue -> String -> MongoDBObject, I have two valid typed objects and I have to use an untyped intermediate format to go from one to another.

andyczerwonka
  • 4,230
  • 5
  • 34
  • 57
  • Very related, but ends up in same place as you: http://stackoverflow.com/questions/11987071/how-to-convert-casbah-mongodb-list-to-json-in-scala-play – Ed Staub Apr 01 '15 at 15:49

1 Answers1

0

If you just want to store the graph as a string "payload", you can of course do that.

You may want to consider using ReactiveMongo instead of Casbah, along with Play-ReactiveMongo, which provides direct-to-JSON capability. I have not used Play-ReactiveMongo.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91