0

i am new in scala and play framework. Trying to create an sample application by using mongo reactive and play-framework with scala. But i face an issue. When i trying to map my case class, to format for JSON, i am getting an following compile time error in my json formatter:

Multiple markers at this line: No unapply function found

I was also create an simple example before, but that example run successfully. this create a problem. following is my case class code:

case class Video (
var _id: Option[BSONObjectID],
var title: Option[String],
var alias: Option[String],
var categoryIds: Option[List[BSONObjectID]],
var tags: Option[List[String]],
var thumbnailImg: Option[String],
var videoCoverImg: Option[String],
var videoUrl: Option[String],
var isRemote: Option[Boolean],
var userId: Option[BSONObjectID],
var videoEmbedCode: Option[String],
var isPublic: Option[Boolean],
var description: Option[String],
var isPublished: Option[Boolean],
var isRecommended: Option[Boolean],
var access: Option[String],
var uploadedOn: Option[BSONDateTime],
var modifiedOn: Option[BSONDateTime],
var likeCount: Option[Int],
var dislikeCount: Option[Int],
var hitCount: Option[Int],
var metaDesc: Option[String],
var metaKeywords: Option[List[String]],
var author: Option[String]
)

object VideoJsonFormatter {
   implicit val videoJsonFormat = Json.format[Video]
}

this implicit val videoJsonFormat = Json.format[Video] expression create an compile time error :

 Multiple markers at this line: No unapply function found

My controller code:

def dashboard = Action.async{
  logger.info("In dashboard controller method");

  var cursor: Cursor[Video] = videosCollection.find(Json.obj()).cursor[Video];
  val videosList : Future[List[Video]] = cursor.collect[List](10, true);
  videosList.map { videos => Ok(Json.toJson(videos)) } 
 }

This var cursor: Cursor[Video] = videosCollection.find(Json.obj()).cursor[Video]; expression generate an compile time error:

No Json deserializer found for type models.Video. Try to implement an implicit Reads or Format for this type.

In case class when we comment some of the properties, this is work successfully. These properties are as follow:

var tags: Option[List[String]]
var userId: Option[BSONObjectID]
var metaKeywords: Option[List[String]]

But i need all properties. how could i resolve this ?

Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126

1 Answers1

0

Your case class contains more than 21 fields, thats why the error and You have to provide implicit format for BSONObjectID and BSONDateTime also.

so modify your case class as,

case class Video (
                   var _id: Option[BSONObjectID],
                   var title: Option[String],
                   var alias: Option[String],
                   var categoryIds: Option[List[BSONObjectID]],
                   var tags: Option[List[String]],
                   var thumbnailImg: Option[String],
                   var videoCoverImg: Option[String],
                   var videoUrl: Option[String],
                   var isRemote: Option[Boolean],
                   var userId: Option[BSONObjectID],
                   var videoEmbedCode: Option[String],
                   var isPublic: Option[Boolean],
                   var description: Option[String],
                   var isPublished: Option[Boolean],
                   var isRecommended: Option[Boolean],
                   var access: Option[String],
                   var additional : Additional
                   )

case class Additional(                   var uploadedOn: Option[BSONDateTime],
                                         var modifiedOn: Option[BSONDateTime],
                                         var likeCount: Option[Int],
                                         var dislikeCount: Option[Int],
                                         var hitCount: Option[Int],
                                         var metaDesc: Option[String],
                                         var metaKeywords: Option[List[String]],
                                         var author: Option[String])

object AdditionalForm{
  implicit val xJsonFormat = Json.format[Additional]
}

object VideoJsonFormatter {
  import AdditionalForm._
  implicit val videoJsonFormat = Json.format[Video]
}
S.Karthik
  • 1,389
  • 9
  • 21
  • hello @Skarthik, my compiler errors are remove, but when i run the project, i got an exception `[info] play.api.Application$$anon$1: Execution exception[[RuntimeException: JsEr ror(List((/additional,List(ValidationError(error.path.missing,WrappedArray())))) )]] [info] at play.api.Application$class.handleError(Application.scala:296) ~[play_ 2.11-2.3.8.jar:2.3.8] [info] at play.api.DefaultApplication.handleError(Application.scala:402) [play_`. And how many fields are allowed in case class ? – Harmeet Singh Taara Mar 12 '15 at 13:26
  • Check is your Json string is correct. In scala 2.10, case class can not have more than 22 fields, but in scala 2.11, we can have more than 22 fields, but that bit of hack provided by the language. There is No Function 23 trait defined, so apply function in case class can not have more than 22 fields. – S.Karthik Mar 12 '15 at 13:53
  • but i receive an error, when instead of JSON @SKarthik – Harmeet Singh Taara Mar 12 '15 at 13:54
  • how i show you json, in result i got an error. Please check my controller code above – Harmeet Singh Taara Mar 12 '15 at 13:57
  • in front end, i got the following exception : `RuntimeException: JsError(List((/additional,List(ValidationError(error.path.missing,WrappedArray())))` – Harmeet Singh Taara Mar 12 '15 at 13:59
  • It seems `additional` field is returned as array. – S.Karthik Mar 12 '15 at 14:02
  • refer this http://stackoverflow.com/questions/21737897/play-scalajson-readst-parsing-validationerrorerror-path-missing-wrappedarray – S.Karthik Mar 12 '15 at 14:33