Open lucky0604 opened 5 years ago
I'm not sure if there should be forward looking support for joda time, since Java 8's java.time
library is widely considered to be superior and that any new applications developed on Java should be using java.time
. See here.
To get joda time to work with Jsonx, you'll need to include an implicit joda format. Below is a full implementation for both Format[DateTime]
and Format[Option[DateTime]]
:
def jsonWrites(format: String): Writes[DateTime] = { dt =>
JsString(dt.toString(format))
}
def jsonOptionalWrites(format: String): Writes[Option[DateTime]] = { dtOpt =>
dtOpt.map(dt => JsString(dt.toString(format))).getOrElse(JsNull)
}
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
def jsonReads(): Reads[DateTime] = {
case JsNumber(d) => JsSuccess(new DateTime(d.toLong))
case JsString(s) =>
scala.util.control.Exception.nonFatalCatch[DateTime].opt(DateTime.parse(s, DateTimeFormat.forPattern(dateFormat))) match {
case Some(d) => JsSuccess(d)
case _ => JsError(Seq(JsPath() -> Seq(
JsonValidationError("error.expected.jodadate.format")
)))
}
case _ => JsError(Seq(JsPath() -> Seq(JsonValidationError("error.expected.date"))))
}
def jsonOptionalReads(): Reads[Option[DateTime]] = {
case JsNumber(d) => JsSuccess(Some(new DateTime(d.toLong)))
case JsString(s) =>
scala.util.control.Exception.nonFatalCatch[DateTime].opt(DateTime.parse(s, DateTimeFormat.forPattern(dateFormat))) match {
case Some(d) => JsSuccess(Some(d))
case _ => JsError(Seq(JsPath() -> Seq(
JsonValidationError("error.expected.jodadate.format")
)))
}
case _ => JsError(Seq(JsPath() -> Seq(JsonValidationError("error.expected.date"))))
}
implicit val dateTimeFormat: Format[DateTime] = new Format[DateTime] {
override def writes(o: DateTime): JsValue = jsonWrites("yyyy-MM-dd HH-mm-ss").writes(o)
override def reads(json: JsValue): JsResult[DateTime] = jsonReads().reads(json)
}
implicit val dateTimeFormatOption: Format[Option[DateTime]] = new Format[Option[DateTime]] {
override def writes(o: Option[DateTime]): JsValue = jsonOptionalWrites("yyyy-MM-dd HH-mm-ss").writes(o)
override def reads(json: JsValue): JsResult[Option[DateTime]] = jsonOptionalReads().reads(json)
}
When I use jsonx and joda time in a case class.
I also use
slickless
andshapeless
to resolve the 22 params. It throws the error: