JSON serializer for ADT do not allows to compile

52 views
Skip to first unread message

Kilic Ali-Firat

unread,
Jul 19, 2016, 12:04:23 PM7/19/16
to ReactiveMongo - http://reactivemongo.org
Hi guys, 

These last days, I met a problem that I cannot solve. I design the following logical model using ADT :

Record := CRecord | NRecord

A CRecord and NRecord share common properties so I decided to implement like this : 


  sealed trait BiosRecord {
    val key
: String
    val patientId
: String
    val startime
: Long // in microseconds
    val endtime
: Long // in microseconds
    val byteEncoding
: Int // 1, 2, 3 or 4
 
}




 
case class CBiosRecord
   
(
      key
: String,
      patientId
: String,
      startime
: Long, // in microseconds
      endtime
: Long, // in microseconds
      byteEncoding
: Int, // 1, 2, 3 or 4
   
) extends BiosRecord






case class NBiosRecord
   
(
      key
: String,
      patientId
: String,
      startime
: Long, // in microseconds
      endtime
: Long, // in microseconds
      byteEncoding
: Int, // 1, 2, 3 or 4
      channels
: List[NNBiosChannel],
      events
: Option[List[NNBiosEvent]],
      preRecordComment
: String,
      postRecordComment
: String,
   
) extends BiosRecord



Then, I code the JSON reader & writer :

/
******* BiosRecord reads/writes ******/
 
implicit val biosRecordReads = {
    val ccwBiosRecord
= Json.reads[CCWBiosRecord]
    val nnBiosRecord
= Json.reads[NNBiosRecord]
    __
.read[CCWBiosRecord](ccwBiosRecord).map(x => x : BiosRecord) |
    __
.read[NNBiosRecord](nnBiosRecord).map(x => x : BiosRecord)
 
}


 
implicit val biosRecordWrites = Writes[BiosRecord] {
   
case ccwBiosRecord : CCWBiosRecord =>
     
Json.writes[CCWBiosRecord].writes(ccwBiosRecord)
   
case nnBiosRecord : NNBiosRecord =>
     
Json.writes[NNBiosRecord].writes(nnBiosRecord)
 
}


So this code compile and I can also serialize a CRecord to JSON (also the case for a NRecord). Now, I put the lines code giving to me an error :


// ReactiveMongo
import play.modules.reactivemongo.ReactiveMongoApi
import play.modules.reactivemongo.json._, ImplicitBSONHandlers._
import reactivemongo.play.json.collection.{JSONCollection, JsCursor}


// Scala

import scala.concurrent.ExecutionContext.Implicits.global




object MongoDBService {


  val config
= Play.application().configuration()
  val mongoCollectionName
= config.getString("mongodb.collection.name")
 
def reactiveMongoApi = current.injector.instanceOf[ReactiveMongoApi]
  val mongoCollection
= reactiveMongoApi.db.collection[JSONCollection](mongoCollectionName)


 
def updateBiosChannel
   
(
      recordKey
: String
   
) : Unit = {

    val futureRecord  
= mongoCollection.find(Json.obj("key"->recordKey)).one[BiosRecord]
    futureRecord map
{ recordOpt =>
      recordOpt match
{
       
case None => ()
       
case Some(record : BiosRecord) =>
 
          record match
{
           
case cRecord : CBiosRecord =>
              mongoCollection
.update(Json.obj("key"->recordKey), ccwRecord)
           
case nRecord : NBiosRecord =>
              mongoCollection
.update(Json.obj("key"->recordKey), nnRecord)
         
}
     
}
   
}
 
}
}


What I'm getting as error => 
app/services/MongoDBService.scala:33: No Json serializer as JsObject found for type play.api.libs.json.JsObject. Try to implement an implicit OWrites or OFormat for this type.
[error]     val futureRecord  = mongoCollection.find(Json.obj("key"->recordKey)).one[BiosRecord]


I really didn't understand this error because I can serialize a BiosRecord to JSON. Is it because I'm using an ADT to make my models or something else ? 

Thanking you in advance, Ali-Firat. 

Cédric Chantepie

unread,
Jul 19, 2016, 1:28:49 PM7/19/16
to ReactiveMongo - http://reactivemongo.org
First fix the missing serializer for JsObject: http://reactivemongo.org/releases/0.11/documentation/json/overview.html

P.S. Please check the formatting of your code. For now it's quite hard to read there.

Kilic Ali-Firat

unread,
Jul 20, 2016, 3:32:04 AM7/20/16
to ReactiveMongo - http://reactivemongo.org


Le mardi 19 juillet 2016 19:28:49 UTC+2, Cédric Chantepie a écrit :
First fix the missing serializer for JsObject: http://reactivemongo.org/releases/0.11/documentation/json/overview.html
When you said "Fix the missing serializer", you means to add the following import : 
import play.api.libs.json.JsObject

Because it's not working too. 

Cédric Chantepie

unread,
Jul 20, 2016, 4:11:22 AM7/20/16
to ReactiveMongo - http://reactivemongo.org
You are missing the import which is required for the JSON support for ReactiveMongo. Please first read the documentation: http://reactivemongo.org/releases/0.11/documentation/json/overview.html

Kilic Ali-Firat

unread,
Jul 20, 2016, 9:50:26 AM7/20/16
to ReactiveMongo - http://reactivemongo.org
Hi Cedric, after reading the document, I have now the following imports: 

import play.modules.reactivemongo.ReactiveMongoApi
import play.api.libs.json._
import play.modules.reactivemongo.json._, ImplicitBSONHandlers._
import reactivemongo.play.json._ // from the reactivemongo doc
import reactivemongo.play.json.collection.JSONCollection


But I still have the following error :

 No Json serializer as JsObject found for type play.api.libs.json.JsObject. Try to implement an implicit OWrites or OFormat for this type.
[error]     val futureRecord  = mongoCollection.find(Json.obj("key"->recordKey)).one[BiosRecord]



Kilic Ali-Firat

unread,
Jul 20, 2016, 10:03:42 AM7/20/16
to ReactiveMongo - http://reactivemongo.org
Hi Cedric,

It works now. 

I removed the two followings import :


// import play.modules.reactivemongo.json._
// import play.modules.reactivemongo.json.collection._

Thank you for the help !

Cédric Chantepie

unread,
Jul 20, 2016, 8:43:31 PM7/20/16
to ReactiveMongo - http://reactivemongo.org
As indicated in the doc, the import for the JSON pack is: import reactivemongo.play.json._
Reply all
Reply to author
Forward
0 new messages