Created
July 11, 2011 21:52
-
-
Save jaytaylor/1076885 to your computer and use it in GitHub Desktop.
Nice ParaNameReader for the Play! Framework v.1.2.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package PlayParameterReader | |
/** | |
* @author Jay Taylor <[email protected]> | |
* | |
* @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