Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactiveMongo 0.9: Joda Datetime Implicit Conversion for Macros.handler

I have case class with joda Datetime field:

case DomainPositionData(domain: String, position: Int, change: Option[Int], date:DateTime)

Trying to use macro to generate reader&writer:

implicit val domPosFormat = Macros.handler[DomainPositionData]

I got:

Implicit org.joda.time.DateTime for 'value date' not found

But I haven't found info how to implement my own implicit convertor.

like image 873
Alexander Chepurnoy Avatar asked May 21 '13 06:05

Alexander Chepurnoy


2 Answers

Looking at the source code for the existing handlers, you could try to create an implicit conversion like this (not tested):

import org.joda.time.DateTime

implicit object BSONDateTimeHandler extends BSONHandler[BSONDateTime, DateTime] {
  def read(time: BSONDateTime) = new DateTime(time.value)
  def write(jdtime: DateTime) = BSONDateTime(jdtime.getMillis)
}
like image 176
huynhjl Avatar answered Sep 22 '22 21:09

huynhjl


Another approach is to use new BSONReader & BSONWriter(having 2 type parameters in 0.9):

implicit object DatetimeReader extends BSONReader[BSONDateTime, DateTime]{
    def read(bson: BSONDateTime): DateTime = new DateTime(bson.value)
}

implicit object DatetimeWriter extends BSONWriter[DateTime, BSONDateTime]{
    def write(t: DateTime): BSONDateTime = BSONDateTime(t.getMillis)
}
like image 26
Alexander Chepurnoy Avatar answered Sep 22 '22 21:09

Alexander Chepurnoy