Skip to content

Commit

Permalink
#7 Change date format to timestamp with milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Dec 29, 2016
1 parent e9a531f commit c5ff02b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/org/elastic4play/JsonFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import scala.util.{ Failure, Success, Try }
import play.api.libs.json.{ Format, JsArray, JsObject, JsString, Json, Reads, Writes }

import org.elastic4play.controllers.JsonFormat.inputValueFormat
import java.util.Date
import play.api.libs.json.JsNumber

object JsonFormat {
val datePattern = "yyyyMMdd'T'HHmmssZ"
implicit val dateFormat = Format(Reads.dateReads(datePattern), Writes.dateWrites(datePattern))
val dateReads = Reads.dateReads(datePattern).orElse(Reads.DefaultDateReads).orElse(Reads.LongReads.map(new Date(_)))
val dateWrites = Writes[Date](d JsNumber(d.getTime))
implicit val dateFormat = Format(dateReads, dateWrites)

val invalidFormatAttributeErrorWrites = Json.writes[InvalidFormatAttributeError]
val unknownAttributeErrorWrites = Json.writes[UnknownAttributeError]
Expand Down
4 changes: 3 additions & 1 deletion app/org/elastic4play/models/Attributes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ object DateAttributeFormat extends AttributeFormat[Date]("date") {
}
override def checkJson(subNames: Seq[String], value: JsValue) = value match {
case JsString(v) if subNames.isEmpty try { parse(v); Good(value) } catch { case _: Throwable Bad(One(InvalidFormatAttributeError("", name, JsonInputValue(value)))) }
case JsNumber(_) if subNames.isEmpty Good(value)
case _ Bad(One(InvalidFormatAttributeError("", name, JsonInputValue(value))))
}
override def fromInputValue(subNames: Seq[String], value: InputValue): Date Or Every[AttributeError] = {
Expand All @@ -96,13 +97,14 @@ object DateAttributeFormat extends AttributeFormat[Date]("date") {
value match {
case StringInputValue(Seq(v)) try { Good(parse(v)) } catch { case _: Throwable Bad(One(InvalidFormatAttributeError("", name, value))) }
case JsonInputValue(JsString(v)) try { Good(parse(v)) } catch { case _: Throwable Bad(One(InvalidFormatAttributeError("", name, value))) }
case JsonInputValue(JsNumber(v)) try { Good(parse(v.toString)) } catch { case _: Throwable Bad(One(InvalidFormatAttributeError("", name, value))) }
case JsonInputValue(JsNumber(v)) try { Good(new Date(v.toLong)) } catch { case _: Throwable Bad(One(InvalidFormatAttributeError("", name, value))) }
case _ Bad(One(InvalidFormatAttributeError("", name, value)))
}
}
}
override def elasticToJson(values: Seq[Any]): Option[JsValue] = values match { // FIXME
case Seq(s: String) Some(JsString(s))
case Seq(n: Number) Some(JsNumber(n.longValue))
case _ None
}
override val swaggerType = Json.obj("type" "dateTime")
Expand Down

0 comments on commit c5ff02b

Please sign in to comment.