Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Created July 11, 2011 21:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaytaylor/1076885 to your computer and use it in GitHub Desktop.
Save jaytaylor/1076885 to your computer and use it in GitHub Desktop.
Nice ParaNameReader for the Play! Framework v.1.2.2
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment