Skip to content
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

Add DateTimeSerializer for MongoDB #1358

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait JsonFormats {

implicit lazy val _formats: Formats = formats

lazy val allFormats = DefaultFormats.lossless + new ObjectIdSerializer + new DateSerializer + new PatternSerializer + new UUIDSerializer
lazy val allFormats = DefaultFormats.lossless + new ObjectIdSerializer + new DateSerializer + new DateTimeSerializer + new PatternSerializer + new UUIDSerializer
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import java.util.regex.{Pattern, PatternSyntaxException}

import org.bson.types.ObjectId

import org.joda.time.DateTime

/*
* Provides a way to serialize/de-serialize ObjectIds.
*
Expand Down Expand Up @@ -90,6 +92,28 @@ class DateSerializer extends Serializer[Date] {
}
}

/*
* Provides a way to serialize/de-serialize joda time DateTimes.
*
* Queries for a Date (dt) using the lift-json DSL look like:
* ("dt" -> ("$dt" -> formats.dateFormat.format(dt)))
*/
class DateTimeSerializer extends Serializer[DateTime] {
private val DateTimeClass = classOf[DateTime]

def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), DateTime] = {
case (TypeInfo(DateTimeClass, _), json) => json match {
case JObject(JField("$dt", JString(s)) :: Nil) =>
new DateTime(format.dateFormat.parse(s).getOrElse(throw new MappingException("Can't parse "+ s + " to DateTime")))
case x => throw new MappingException("Can't convert " + x + " to Date")
}
}

def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: DateTime => Meta.dateAsJValue(x.toDate, format)
}
}

/*
* Provides a way to serialize/de-serialize UUIDs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,77 @@
package net.liftweb
package mongodb

import java.util.{Calendar, Date, TimeZone}
import java.util.{Calendar, Date, TimeZone, UUID}
import java.util.regex.Pattern

import org.bson.types.ObjectId

import org.joda.time.{Instant, DateTime}

import org.specs2.mutable.Specification


package customserializersspecs {

/*
* Date as String
* ObjectId as String
*/
case class Person(_id: String, birthDate: Date)
case class Person(_id: String)
extends MongoDocument[Person]
{
def meta = Person
}
object Person extends MongoDocumentMeta[Person]

/*
* ObjectId as ObjectId
*/
case class PersonWithObjectId(_id: ObjectId)
extends MongoDocument[PersonWithObjectId]
{
def meta = PersonWithObjectId
}
object PersonWithObjectId extends MongoDocumentMeta[PersonWithObjectId] {
override def formats = allFormats
}

/*
* Pattern as Pattern
*/
case class PersonWithPattern(_id:ObjectId, pattern: Pattern) extends MongoDocument[PersonWithPattern] {
def meta = PersonWithPattern
}
object PersonWithPattern extends MongoDocumentMeta[PersonWithPattern] {
override def formats = allFormats
}

/*
* Date as Date
*/
case class Person2(_id: ObjectId, birthDate: Date) extends MongoDocument[Person2] {
def meta = Person2
case class PersonWithDate(_id: ObjectId, birthDate: Date) extends MongoDocument[PersonWithDate] {
def meta = PersonWithDate
}
object PersonWithDate extends MongoDocumentMeta[PersonWithDate] {
override def formats = allFormats
}

/*
* DateTime as DateTime
*/
case class PersonWithDateTime(_id: ObjectId, birthDate: DateTime) extends MongoDocument[PersonWithDateTime] {
def meta = PersonWithDateTime
}
object PersonWithDateTime extends MongoDocumentMeta[PersonWithDateTime] {
override def formats = allFormats
}
object Person2 extends MongoDocumentMeta[Person2] {

/*
* UUID as UUID
*/
case class PersonWithUUID(_id: UUID) extends MongoDocument[PersonWithUUID] {
def meta = PersonWithUUID
}
object PersonWithUUID extends MongoDocumentMeta[PersonWithUUID] {
override def formats = allFormats
}
}
Expand All @@ -59,14 +104,11 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
val utc = TimeZone.getTimeZone("UTC")

"CustomSerializers" should {
"handle Date as String value" in {
"handle ObjectId as String value" in {
checkMongoIsRunning

// test data
val bdjack = Calendar.getInstance
bdjack.setTimeZone(utc)
bdjack.setTimeInMillis(1288742280000L)
val jack = Person(ObjectId.get.toString, bdjack.getTime)
val jack = Person(ObjectId.get.toString)

// save the Person document
jack.save
Expand All @@ -76,6 +118,59 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
jack2.isDefined must_== true
jack2.toList map { j =>
j._id mustEqual jack._id
}
}

"handle ObjectId as ObjectId value using ObjectIdSerializer" in {
checkMongoIsRunning

// test data
val jack = PersonWithObjectId(ObjectId.get)

// save the PersonWithObjectId document
jack.save

// retrieve it and compare
val jack2 = PersonWithObjectId.find(jack._id)
jack2.isDefined must_== true
jack2.toList map { j =>
j._id mustEqual jack._id
}
}

"handle Pattern as Pattern value using PatternSerializer" in {
checkMongoIsRunning

// test data
val pattern = Pattern.compile("(?idmsux-idmsux)m(a)gi(?:ic)?[a-zA-Z]+boom")
val jack = PersonWithPattern(ObjectId.get, pattern)

// save the PersonWithPattern document
jack.save

// retrieve it and compare
val jack2 = PersonWithPattern.find(jack._id)
jack2.isDefined must_== true
jack2.toList map { j =>
j.pattern.pattern mustEqual jack.pattern.pattern
j.pattern.flags mustEqual jack.pattern.flags
}
}

"handle DateTime as DateTime value using DateTimeSerializer" in {
checkMongoIsRunning

// test data
val birthday = (new Instant(1288742280000L)).toDateTime
val jack = PersonWithDateTime(ObjectId.get, birthday)

// save the Person document
jack.save

// retrieve it and compare
val findJack = PersonWithDateTime.find(jack._id)
findJack.isDefined must_== true
findJack.toList map { j =>
j.birthDate mustEqual jack.birthDate
}
}
Expand All @@ -87,18 +182,35 @@ class CustomSerializersSpec extends Specification with MongoTestKit {
val bdjack = Calendar.getInstance
bdjack.setTimeZone(utc)
bdjack.setTimeInMillis(1288742280000L)
val jack = Person2(ObjectId.get, bdjack.getTime)
val jack = PersonWithDate(ObjectId.get, bdjack.getTime)

// save the Person document
jack.save

// retrieve it and compare
val findJack = Person2.find(jack._id)
val findJack = PersonWithDate.find(jack._id)
findJack.isDefined must_== true
findJack.toList map { j =>
j._id mustEqual jack._id
j.birthDate mustEqual jack.birthDate
}
}

"handle UUID as UUID value using UUIDSerializer" in {
checkMongoIsRunning

// test data
val uuid = UUID.randomUUID
val jack = PersonWithUUID(uuid)

// save the Person document
jack.save

// retrieve it and compare
val findJack = PersonWithUUID.find(jack._id)
findJack.isDefined must_== true
findJack.toList map { j =>
j._id mustEqual jack._id
}
}
}
}