score:24
Expecting a better alternative, here my workaround:
val dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
val jodaDateReads = Reads[DateTime](js =>
js.validate[String].map[DateTime](dtString =>
DateTime.parse(dtString, DateTimeFormat.forPattern(dateFormat))
)
)
val jodaDateWrites: Writes[DateTime] = new Writes[DateTime] {
def writes(d: DateTime): JsValue = JsString(d.toString())
}
val userReads: Reads[User] = (
(JsPath \ "name").read[String] and
(JsPath \ "created").read[DateTime](jodaDateReads)
)(User.apply _)
val userWrites: Writes[User] = (
(JsPath \ "name").write[String] and
(JsPath \ "created").write[DateTime](jodaDateWrites)
)(unlift(User.unapply))
implicit val userFormat: Format[User] = Format(userReads, userWrites)
score:1
I think you should set the User
type in Json.toJson
and Json.fromJson
functions. Instead of
println(Json.toJson(new User("user", new DateTime())))
println(Json.fromJson(value))
try:
println(Json.toJson[User](new User("user", new DateTime())))
println(Json.fromJson[User](value))
When you set the type explicitly framework will know what reads/writes to use.
Update:
It is not necessarily to set type for Json.toJson
function because you pass User
object as function argument and framework determines the type in runtime.
But for Json.fromJson[User]
you must set the type, otherwise framework doesn't know type of the object you want to read.
score:6
Try these:
implicit val dateWrites = jodaDateWrites("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
implicit val dateReads = jodaDateReads("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
score:6
I know this question has been answered for a while, but I found a more concise answer
val pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
implicit val dateFormat = Format[DateTime](Reads.jodaDateReads(pattern), Writes.jodaDateWrites(pattern))
implicit val userFormat = Json.format[User]
score:24
In play 2.6, the canonical way to serialize/deserialize joda DateTime json is by using the play-json-joda library. Import the library by updating your build.sbt
. Then create json reader and json writers like this :
import play.api.libs.json.JodaWrites
implicit val dateTimeWriter: Writes[DateTime] = JodaWrites.jodaDateWrites("dd/MM/yyyy HH:mm:ss")
import play.api.libs.json.JodaReads
implicit val dateTimeJsReader = JodaReads.jodaDateReads("yyyyMMddHHmmss")
Source: stackoverflow.com
Related Query
- How to use Joda DateTime with Play Json
- How to use IntelliJ with Play Framework and Scala
- How to load JSON file using Play with Scala
- How to respond with a pretty-printed JSON object using play framework?
- How to write a Play JSON writes converter for a case class with a single nullable member
- In Play 2.4 with DI, how to use a service class in "Secured" trait?
- Play 2.2 JSON Reads with combinators: how to deal with nested optional objects?
- How to use play-plugins-mailer with Play 2.3 and Scala 2.11?
- How to use the Play Framework with Google App Engine with locally installed Java 7?
- Play framework JSON transformers, how to work with recursive paths (jsPath)?
- How to use OutputStreams with chunked response in Play 2.1
- How to use SORM framework with Play Framework?
- How to read and write Anorm object with the new JSON API in Play Framework 2.1-RC2?
- How to read a JSON string with escaped quotes in Play
- How to convert a json with a single value to a case class using play json
- How do I test Play REST API with Json BodyParser?
- How to write a symmetric Play Json formatter for a case class with one field in scala?
- Json Coast to Coast Play framework : Serializing Joda DateTime
- Play JSON: How to use string as proper json
- How to create JSON output from a combined group of composite classes with Play framework
- How to convert Long type in DateTime with joda in scala?
- How do I serialize CharSequence to JSON with Play 2.1 and Scala
- how can i use lift-mongo-record with play framework?
- How can I use Json.reads to deserialize JSON into a case class with optional constructor parameters
- Play Framework: How to serialize raw fields to JSON with nested elements
- How to ignore non existent keys when transforming json with Play Json
- How do I use play framework 2's json path to parse json that has been poorly converted from xml?
- How do I use Zookeeper as "Database" with Play Framework instead of traditional JDBC?
- How to use nullable columns with Anorm and Play Framework?
- How to use spark and mongo with play to calculate prediction?
More Query from same tag
- Getting timeout error in New Year Chaos problem using scala
- Is it possible to run a single test out of Scala's test suite?
- python dataframe or hiveSql update based on predecessor value?
- How to get the first date and last date using current month and year on scala
- How does this Scala function work?
- Send message to parent actor in Akka Typed
- Strange Scala 'Type mismatch' error for tuples
- Scala error Expression of type subtype doesn't conform to expected type T
- akka.actor.ActorLogging does not log the stack trace of exception by logback
- Posting JSON message with no key value pair Spray
- Can I set cookies before returning an action in Play Framework 2?
- Using method with recoverWith
- How to use transactions in a controller in Scala
- Idiomatic use of a Java comparable object
- Get file name of DataStream with Flink
- Scala recover or recoverWith
- Scala - fold - List of tuples (Long, String) to List of Strings to single String
- Scala code parser (not compiler)
- Uploading generated resources to S3 in stage task?
- How to get server name without using Host header from http request
- How to filter out elements in RDD using list of exclusions (similar to isin)?
- JVM language interoperability
- Idiomatic form of dealing with un-initialized var
- Scala's blend of functional with object-oriented
- Trying to extend generic Array
- Shapeless map HList depending on target types
- ScalaPb sbt: Import "data/common/num.proto" was not found or had errors
- Date join and rank within a time period
- How can I obtain the default value of a constructor parameter for a non-case class?
- Functional composition of different types of Tasks - Scala