public
Created

Nice ParaNameReader for the Play! Framework v.1.2.2

  • Download Gist
PlayParameterReader.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
package PlayParameterReader
 
/**
* @author Jay Taylor <outtatime@gmail.com>
*
* @date 2011-05-23
*/
 
import scala.collection.JavaConversions._
 
import java.lang.reflect.Constructor
import java.sql.Timestamp
import java.text.SimpleDateFormat
 
import play.classloading.enhancers.LocalvariablesNamesEnhancer
 
import net.liftweb.json._
import net.liftweb.json.JsonAST
import net.liftweb.json.JsonDSL._
 
 
object PlayParameterNameReader extends ParameterNameReader {
def lookupParameterNames(constructor: Constructor[_]) =
LocalvariablesNamesEnhancer.lookupParameterNames(constructor)
}
 
 
class TimestampSerializer extends Serializer[Timestamp] {
 
private val TimestampClass = classOf[Timestamp]
 
def deserialize(implicit format: Formats):
PartialFunction[(TypeInfo, JValue), Timestamp] = {
case (TypeInfo(TimestampClass, _), json) => json match {
case JString(s) => new Timestamp((new SimpleDateFormat).parse(s).getTime)
case x => throw new MappingException("Can't convert " + x + " to Timestamp")
}
}
 
def serialize(implicit format: Formats): PartialFunction[Any, JValue] = {
case x: Timestamp => JString((new SimpleDateFormat).format(x))
}
}
 
 
object Formats {
implicit val formats = new DefaultFormats {
override val parameterNameReader = PlayParameterNameReader
} + (new TimestampSerializer)
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.