Skip to content

Instantly share code, notes, and snippets.

@mzafer
Created September 13, 2012 03:21
Show Gist options
  • Save mzafer/3711637 to your computer and use it in GitHub Desktop.
Save mzafer/3711637 to your computer and use it in GitHub Desktop.
Salat DateTime json deserialization issue
import play.api._
object Global extends GlobalSettings {
override def onStart(app: Application) {
com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()
/*import com.mongodb.casbah.commons.conversions.scala._
RegisterJodaTimeConversionHelpers()
RegisterConversionHelpers()*/
Logger.info("Application has started with JodaTime registered")
}
}
package object salat_context {
implicit val ctx = {
val context = new Context {
val name = "CustomCtx"
override val typeHintStrategy = StringTypeHintStrategy(when = TypeHintFrequency.WhenNecessary, typeHint = "_t")
override val jsonConfig = JSONConfig(objectIdStrategy = StringObjectIdStrategy,
dateStrategy = TimestampDateStrategy())
}
//context.registerGlobalKeyOverride(remapThis = "id", toThisInstead = "_id1")
com.mongodb.casbah.commons.conversions.scala.RegisterJodaTimeConversionHelpers()
context.registerClassLoader(Play.classloader)
context
}
}
package model
import play.api.Play.current
import util.salat_context._
import se.radley.plugin.salat._
import com.novus.salat.dao._
import com.novus.salat.annotations._
import com.mongodb.casbah.Imports._
import org.scala_tools.time.Imports.DateTime
case class TestItem(
@Key("_id") id: ObjectId = new ObjectId,
name:String,
date:Option[DateTime] = None
)
object TestItem extends ModelCompanion[TestItem,ObjectId] {
val collection = mongoCollection("TestItem")
val dao = new SalatDAO[TestItem,ObjectId](collection = collection) {}
}
package test
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
import play.api.test.FakeApplication
import play.api.test.Helpers._
import org.bson.types.ObjectId
import model.TestItem
import org.specs2.mutable.Tags
import org.scala_tools.time.Imports.DateTime
import util.salat_context._
class TestSpec extends Specification with Tags{
object FakeApp extends FakeApplication()
"Test Item" should {
"be converted to and from Json " in {
running (FakeApp) {
var testItem = TestItem(name="Test Item",date=Some(DateTime.now))
val jsonStr:String = TestItem.toCompactJson(testItem)
println(jsonStr)
//jsonStr must not be null
val newItem = TestItem.fromJSON(jsonStr)
newItem must not be null
}
} tag("testItem")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment