Play2.4 gridfs.save throws"No Json deserializer found for type ReadFile[JSONSerializationPack.type]"

527 views
Skip to first unread message

Mayank Patel

unread,
Oct 13, 2015, 1:31:49 PM10/13/15
to ReactiveMongo - http://reactivemongo.org
I am using Play2.4, ReactiveMongo GridFS. I am getting following error:

No Json deserializer found for type reactivemongo.api.gridfs.ReadFile[play.modules.reactivemongo.json.JSONSerializationPack.type,play.api.libs.json.JsValue]. Try to implement an implicit Reads or Format for this type. Error occurred in an application involving default arguments.

Here is the my code:
def save(gfs: GridFS[JSONSerializationPack.type], info: FlowInfo, enumerator: Enumerator[Array[Byte]], f: File) = {

gfs.save(enumerator, JSONFileToSave( filename = info.resumableFilename, contentType = Some(URLConnection.guessContentTypeFromName(info.resumableFilename)), uploadDate = Some(DateTime.now().getMillis), metadata = Json.obj("md5" -> org.apache.commons.codec.digest.DigestUtils.md5Hex(new FileInputStream(f)))), info.resumableChunkSize)
}



implicit val reader = reactivemongo.api.gridfs.Implicits.DefaultReadFileReader
type JSONReadFile = ReadFile[JSONSerializationPack.type, JsValue]

val collection = db[JSONCollection]("Album.files")
type G = GridFS[JSONSerializationPack.type]
var gridFS = reactiveMongoApi.gridFS

TestService.save(gridFS, info, Enumerator.fromFile(f), f)


Any help would be much appreciated.

Cédric Chantepie

unread,
Oct 13, 2015, 4:33:14 PM10/13/15
to ReactiveMongo - http://reactivemongo.org

Mayank Patel

unread,
Oct 13, 2015, 5:42:06 PM10/13/15
to ReactiveMongo - http://reactivemongo.org
Cédric, I have been using this demo app as reference. After few different trial I'm getting following: type mismatch; found : reactivemongo.api.gridfs.DefaultFileToSave required: reactivemongo.api.gridfs.FileToSave[gfs.pack.type,?]


This is my save call to gridFS: gfs.save(enumerator, new DefaultFileToSave(Some(info.resumableFilename), Some(URLConnection.guessContentTypeFromName(info.resumableFilename)),
  metadata = BSONDocument("md5" -> org.apache.commons.codec.digest.DigestUtils.md5Hex(new FileInputStream(f)))),
info.resumableChunkSize).map {
file => Some(file)
}

Cédric Chantepie

unread,
Oct 15, 2015, 3:32:24 AM10/15/15
to ReactiveMongo - http://reactivemongo.org
As it compile fine with the sample app, there must have a difference in your code.

Mayank Patel

unread,
Oct 15, 2015, 11:20:56 AM10/15/15
to ReactiveMongo - http://reactivemongo.org
I think I know what is the difference. I did fix everything last problem I'm having is:
No Json deserializer found for type reactivemongo.api.gridfs.ReadFile[play.modules.reactivemongo.json.JSONSerializationPack.type,play.api.libs.json.JsValue]. Try to implement an implicit Reads or Format for this type.
I compare my code with the demo project and only difference I have is my use case is to read the file from file system using java.o.File() which doesn't use type JSONReadFile = ReadFile[JSONSerializationPack.type, JsString].

Is there a way I can use that with manual file reading using new File(), etc...?

I think if there is then that will solve my problem. My use case is different so I have to read the file from File System. Appreciate all you help.

In short this what my complete code looks like/ Sample:
Oct-14 3:11 PM

package controllers




import java.io._
import java.net.URLConnection
import javax.inject.Inject


import org.slf4j.{LoggerFactory, Logger}
import play.api.libs.Files.TemporaryFile
import play.api.libs.iteratee.{Iteratee, Enumerator}
import play.api.libs.json.{Json, JsObject, JsValue, JsString}
import play.modules.reactivemongo.json._
import play.modules.reactivemongo.json.collection.JSONCollection
import play.modules.reactivemongo.{JSONFileToSave, ReactiveMongoComponents, MongoController, ReactiveMongoApi}
import reactivemongo.api.BSONSerializationPack
import reactivemongo.api.gridfs.{FileToSave, DefaultFileToSave, GridFS, ReadFile}
import reactivemongo.bson.{BSONObjectID, BSONDocument}
import services.com.patel.album.{TestService, AlbumService}
import scala.concurrent.ExecutionContext.Implicits.global
import play.api.mvc.{MaxSizeExceeded, _}
import utils.{FlowHelper, FlowInfoStorage, FlowInfo}




import scala.concurrent.ExecutionContext.Implicits.global
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import reactivemongo.api.commands.bson.BSONFindAndModifyImplicits._
import reactivemongo.api.gridfs.Implicits._
import ImplicitBSONHandlers._


class Upload @Inject()(val reactiveMongoApi: ReactiveMongoApi)
 
extends Controller with FlowHelper
 
with MongoController
 
with ReactiveMongoComponents {


 
private final val logger: Logger = LoggerFactory.getLogger(classOf[Upload])


  type
JSONReadFile = ReadFile[JSONSerializationPack.type, JsString]



  val collection
= db[JSONCollection]("Album.files")



  val gridFS
= reactiveMongoApi.gridFS


  gridFS
.ensureIndex().onComplete {
   
case index =>
      logger
.info(s"Checked index, result is $index")
 
}




 
def dealWithFile()(implicit request: RequestHeader): Result = {


      val f
: File = new File(base_dir + info.resumableFilename)


      val md5Str
= org.apache.commons.codec.digest.DigestUtils.md5Hex(new FileInputStream(f))
      lazy val contentType
= URLConnection.guessContentTypeFromName(f.getName())
      lazy val metaData
= BSONDocument("md5" -> md5Str, "tags" -> "", "albumName" -> "")


//      val fileToSave = DefaultFileToSave(Some(info.resumableFilename), Some(contentType), metadata = metaData)


     
//      val fileToSave = FileToSave[JSONSerializationPack.type](Some(info.resumableFilename), Some(contentType), metadata = metaData)
            val fileToSave
= new JSONFileToSave(Some(info.resumableFilename), Some(contentType), metadata = Json.obj("md5" -> org.apache.commons.codec.digest.DigestUtils.md5Hex(new FileInputStream(f))))




      val enumerator
= Enumerator.fromFile(f)
      gridFS
.save(enumerator, fileToSave)


 
}


}

Cédric Chantepie

unread,
Oct 16, 2015, 3:08:40 AM10/16/15
to ReactiveMongo - http://reactivemongo.org
I suggest you use the exact same GridFS types as in the sample app. Then we can see what's the exact issue.

Mayank Patel

unread,
Oct 18, 2015, 10:54:41 PM10/18/15
to ReactiveMongo - http://reactivemongo.org
I was able to resolve my problem with following solution. I basically have to put following implementation:
class FileToSaveImpl(newFileName: String, newContentType: String, meta: JsObject)
extends FileToSave[gridFS.pack.type,
gridFS.pack.Value] {
val filename: String = newFileName
val contentType: Option[String] = Some(newContentType)
val uploadDate: Option[Long] = None
val metadata: gridFS.pack.Document = meta
val id: gridFS.pack.Value = JsString(BSONObjectID.generate.stringify)
val pack = gridFS.pack
}

val fileToSave = new FileToSaveImpl(file.getName(), contentType, meta.as[JsObject])

Irfan Ansari

unread,
Apr 28, 2016, 10:23:27 AM4/28/16
to ReactiveMongo - http://reactivemongo.org
I fixed it by instantiating my gridFS with wanted SerializationPack 

 val gridFS = new GridFS[BSONSerializationPack.type](db)


Regards,
Irfan
Reply all
Reply to author
Forward
0 new messages