Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add java.sql.Timestamp support to JSON

  • Loading branch information...
commit a0a981ce310bc6826d2725fe49b937d55e799480 1 parent 006a1ef
Joni Freeman authored
View
2  core/json/src/main/scala/net/liftweb/json/Extraction.scala
@@ -20,6 +20,7 @@ package json
import java.lang.reflect.{Constructor => JConstructor, Type}
import java.lang.{Integer => JavaInteger, Long => JavaLong, Short => JavaShort, Byte => JavaByte, Boolean => JavaBoolean, Double => JavaDouble, Float => JavaFloat}
import java.util.Date
+import java.sql.Timestamp
import scala.reflect.Manifest
/** Function to extract values from JSON AST using case classes.
@@ -392,6 +393,7 @@ object Extraction {
case JString(s) if (targetType == classOf[String]) => s
case JString(s) if (targetType == classOf[Symbol]) => Symbol(s)
case JString(s) if (targetType == classOf[Date]) => formats.dateFormat.parse(s).getOrElse(fail("Invalid date '" + s + "'"))
+ case JString(s) if (targetType == classOf[Timestamp]) => new Timestamp(formats.dateFormat.parse(s).getOrElse(fail("Invalid date '" + s + "'")).getTime)
case JBool(x) if (targetType == classOf[Boolean]) => x
case JBool(x) if (targetType == classOf[JavaBoolean]) => new JavaBoolean(x)
case j: JValue if (targetType == classOf[JValue]) => j
View
3  core/json/src/main/scala/net/liftweb/json/Meta.scala
@@ -19,6 +19,7 @@ package json
import java.lang.reflect.{Constructor => JConstructor, Field, Type, ParameterizedType, GenericArrayType}
import java.util.Date
+import java.sql.Timestamp
case class TypeInfo(clazz: Class[_], parameterizedType: Option[ParameterizedType])
@@ -215,7 +216,7 @@ private[json] object Meta {
classOf[Short], classOf[java.lang.Integer], classOf[java.lang.Long],
classOf[java.lang.Double], classOf[java.lang.Float],
classOf[java.lang.Byte], classOf[java.lang.Boolean], classOf[Number],
- classOf[java.lang.Short], classOf[Date], classOf[Symbol], classOf[JValue],
+ classOf[java.lang.Short], classOf[Date], classOf[Timestamp], classOf[Symbol], classOf[JValue],
classOf[JObject], classOf[JArray]).map((_, ())))
private val primaryConstructors = new Memo[Class[_], List[(String, Type)]]
View
5 core/json/src/test/scala/net/liftweb/json/ExtractionExamplesSpec.scala
@@ -90,6 +90,11 @@ object ExtractionExamples extends Specification("Extraction Examples Specificati
json.extract[Event] mustEqual Event("e1", date("2009-09-04T18:06:22Z"))
}
+ "Timestamp extraction example" in {
+ val json = parse("""{"timestamp":"2009-09-04T18:06:22Z"}""")
+ new Date((json \ "timestamp").extract[java.sql.Timestamp].getTime) mustEqual date("2009-09-04T18:06:22Z")
+ }
+
"Option extraction example" in {
val json = parse("""{ "name": null, "age": 5, "mother":{"name":"Marilyn"}}""")
json.extract[OChild] mustEqual OChild(None, 5, Some(Parent("Marilyn")), None)
Please sign in to comment.
Something went wrong with that request. Please try again.