1

これは、単純なアプローチによる簡単な作業です。条件に一致するすべてのファイルを取得します。ファイルとは、バイナリではなく、ファイルのメタデータを意味します。

for {
    files <- gfs.find(BSONDocument("metadata.ideaId" -> BSONObjectID(ideaId))).collect[Seq]()
} yield {

    Ok(Json.toJson(files))
}

スコープに reactmongo.api.gridfs.Implicits._ がありますが、取得します

No Json deserializer found for type Seq[reactivemongo.api.gridfs.ReadFile[reactivemongo.bson.BSONValue]]
4

2 に答える 2

4

このタスクには、非常に単純な手作りのシリアライザーを使用します。

import play.api.libs.json._
import play.api.libs.json.Json._
import reactivemongo.api.gridfs.ReadFile
import reactivemongo.bson.{BSONValue, BSONObjectID}

implicit val fileWrites = new Writes[ReadFile[BSONValue]] {
  def writes(file: ReadFile[BSONValue]): JsValue = {
    val id = file.id.asInstanceOf[BSONObjectID].stringify
      obj(
        "_id" -> id, // need it with underscore
        "name" -> file.filename,
        "url" -> routes.Files.serveFile(id).url, // app specific
        "thumbnailUrl" -> routes.Files.serveFile(id).url, // app specific
        "extension" -> file.filename.split('.').last,
        "contentType" -> file.contentType,
        "size" -> file.length,
        "uploadDate" -> file.uploadDate,
        "deleteUrl" -> "", // app specific
        "deleteType" -> "DELETE"
    )
  }
}

次のように使用できます。

import helpers.Formats.fileWrites // your package here

def all = Action.async { request =
  gfs.find(query).collect[List]() map {
    case files: List[ReadFile[BSONValue]] => Ok(prettyPrint(obj("files" -> toJson(files))))
}
于 2014-04-07T09:55:03.803 に答える
1

Seq[reactivemongo.api.gridfs.ReadFile[reactivemongo.bson.BSONValue]] である「ファイル」を JSON にシリアル化しようとしますが、これはそのままでは不可能です。暗黙の JSON シリアライザーを (play.api.libs.json.Writes[Seq[ReadFile[BSONValue]]] のインスタンスとして) 提供する必要があります。

http://www.playframework.com/documentation/2.1.3/ScalaJsonを参照してください。

詳細については、「Scala 値を JsValue に変換する」というセクションがあります。

于 2014-04-04T15:07:27.800 に答える