score:16
Accepted answer
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)
}
score:0
For version 1.0.x
implicit object DateTimeReader extends BSONReader[DateTime] {
def readTry(bson: BSONValue): Try[DateTime] = bson match {
case time: BSONDateTime => Success(new DateTime(time.value))
case _ => Failure(new IllegalArgumentException())
}
}
implicit object DateTimeWriter extends BSONWriter[DateTime]{
override def writeTry(t: DateTime): Try[BSONValue] = Try{
BSONDateTime(t.getMillis)
}
}
or with val
implicit val dateTimeReader: BSONReader[DateTime] = BSONReader[DateTime]{
case t: BSONDateTime => new DateTime(t.value)
}
implicit val dateTimeWriter: BSONWriter[DateTime] = BSONWriter[DateTime]{ time =>
BSONDateTime(time.getMillis)
}
score:2
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)
}
Source: stackoverflow.com
Related Query
- ReactiveMongo 0.9: Joda Datetime Implicit Conversion for Macros.handler
- Scala: Making implicit conversion A->B work for Option[A] -> Option[B]
- an expression of type Null is ineligible for implicit conversion
- Nested implicit macros seems not working for type parameters?
- Implicit conversion classes for type-aliased Function types fail to compile in Scala
- Unable to provide implicit conversion from DateTime to Ordered using implicit conversion to Comparable
- Implicit conversion for generic type?
- Error: "could not find implicit value for parameter readFileReader" trying to save a file using GridFS with reactivemongo
- Implicit conversion of a generic container for an implicit parameter in Scala
- Implicit conversion method in companion object needs to be imported? Contradiction with "Scala for the impatient" book
- Implicit conversion works for symbol but not string
- Scala implicit conversion for type aliases
- Implicit value not found for BSON Writer in reactivemongo
- Could not find implicit value for parameter write eror, yet I defined the handler using the macro
- Implicit conversion in monadic for comprehension in Scala
- Defining An Implicit Conversion For A Recursive And Nested Type Structure
- implicit conversion of RESULTSET for queries
- How do i write an implicit Function conversion for a map call in Scala
- Implicit conversion for class hierarchy with parameterized types in Scala?
- Can not find implicit conversion for a parameter of method with lower type bound
- Reactivemongo parseURI is failing when loading from config saying it can't find implicit value for parameter loader MongoConnection.ParsedURI
- Scala Implicit conversion for Lambdas
- Scala Type Variance for Implicit Conversion of Collection
- Implicit conversion for multiple parameters
- Implicit conversion not taking place for Play Framework Lang in controller
- implicit conversion for a list of tuple
- scala implicit conversion for case object
- Scala implicit conversion for object
- Scala Implicit Conversion for companion object of extended class
- Where/how Scala looks for implicit conversion methods?
More Query from same tag
- Sbt download repository Forbidden
- How to mock scala readLine
- Play framework redirect to custom schemes
- How do I unit test a controller in play framework 2 scala
- Scala, combining filters on XML node
- How does $(objectName) work in Scala Spark?
- How to use return value from function returning Future(Either[A, B]) in scala?
- Scala Enumeration Data As String?
- Getting Swagger to work in Play2 Scala
- How to get the matched instance of case class inside match pattern
- Update date format in spark dataframe for multiple spark columns
- Compile scala files from a sbt plugin
- Scala accessing members of runtime types using reflection of a class that extends a trait
- What do user-defined value classes look like from Java?
- Does it matter where a shift stands in a reset block?
- No Json serializer as JsObject found for type reactivemongo.play.json.JSONSerializationPack.type
- RMI server finds scala.Option when run from IDE but cannot do it when run from sbt
- Optimize solution for the given coding problem
- General comprehensions in Scala
- Run a scala project using Jupyter Notebook
- Scala: how to avoid var and return in map with special case
- map foldLeft type mismatch found Int required String
- Yammer's Experience w/ Scala
- In akka-stream how to create a unordered Source from a futures collection
- How can I convert a Java Iterable to a Scala Iterable?
- How to create a caching layer on top of slick that could be applied globally?
- How to require typesafe constant-size array in scala?
- Slick 3.2 CodeGenerator Tool hardcodes the database name in the autogenerated code
- How Static allocation in spark works?
- How to update the schema of a Spark DataFrame (methods like Dataset.withColumn and Datset.select don't work in my case)