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.
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)
}
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With