Search code examples
mongodbscalajodatimeimplicit-conversion

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.


Solution

  • 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)
    }