-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISODate type #15
Comments
Sorry, but I don't know how I'd help you here without some code example. Could you provide a gist or something? |
Also, which DateTime are you using? Is it org.joda.time.DateTime? Or something else? Sprest uses the ReactiveMongo library behind the scenes. For DateTime support, this means serialization to/from Long using the http://reactivemongo.org/releases/0.10/api/index.html#reactivemongo.bson.BSONDateTime case class, which handles dates as Long values. There is no BSON class provided by ReactiveMongo for ISODate type as you mention. You should do some testing and see if the ISODate values from your database are handled as strings. If so, then you should be able to work around your issue by providing a custom Spray JSON JsonFormat type class. For example: package foo
import spray.json._
import org.joda.time.DateTime
object Formats extends DefaultJsonProtocol {
implicit object DateTimeFormat extends JsonFormat[DateTime] {
override def read(json: JsValue): DateTime = json match {
case JsString(dateString) => new DateTime(dateString)
case invalid => throw new SerializationException(s"Invalid JsValue for DateTime: $invalid")
}
override def write(dateTime: DateTime): JsValue => JsString(dateTime.toString("<some format string>"))
}
} And then you'll need to import your custom formats in your models so they will use it for serialazation: package foo.models
import foo.Formats._
import spray.json._
import org.joda.time.DateTime
case class MyModel(var id: Option[String] = None, date: DateTime)
object MyModel {
implicit val jsFormat = jsonFormat2(MyModel.apply)
} |
hi @markschaake , I've got similar issue. |
I mean, when I wass using vanilla ReactiveMongo mapping it was storing BSONDateTime as ISODate and not Long. |
I am using Joda DateTime
I just need date field to be used for filtering in database and to be readable outside other than that if it is ISO or a long I don't care I will adapt to that as long as I am give the tools to do so. BTW I am having the same issue if id is not string but ObjectID type in the database it also breaks the formating :/ |
Oh I think I see what's going on. You are using the sprest.reactivemongo.typemappers.SprayJsonTypeMapper, which does not handle BSONDateTime (see https://github.com/markschaake/sprest/blob/master/sprest-reactivemongo/src/main/scala/sprest/reactivemongo/typemappers/TypeMappers.scala#L53). I originally did this on purpose as to allow the user to choose his/her own DateTime implementation. It is a glaring omission though to not support the BSONDateTime, so I'll add a mixin trait that you can use for mapping BSONDateTime to joda.time.DateTime. It would work something like this: implicit object MyJsonTypeMapper extends DefaultJsonTypeMapper with JodaTimeMapping |
I have ISODate type dates in mongo database and I tried using DateTime for case class type variables but then I try to load the data in browser it is empty not even an empty array. I tried using Long, Int or String types to no result.
The text was updated successfully, but these errors were encountered: